Removed cab updating animation

main
Prathamesh More 2021-09-11 12:03:19 +05:30
parent 2c380b9844
commit 162e8ba2dc
1 changed files with 103 additions and 171 deletions

View File

@ -1,182 +1,114 @@
package code.name.monkey.retromusic.adapter.base; package code.name.monkey.retromusic.adapter.base
import android.annotation.SuppressLint; import android.content.Context
import android.content.Context; import android.view.Menu
import android.view.Menu; import android.view.MenuItem
import android.view.MenuItem; import androidx.annotation.MenuRes
import androidx.recyclerview.widget.RecyclerView
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.interfaces.ICabHolder
import com.afollestad.materialcab.MaterialCab
import java.util.*
import androidx.annotation.MenuRes; abstract class AbsMultiSelectAdapter<V : RecyclerView.ViewHolder?, I>(
import androidx.annotation.NonNull; context: Context, private val ICabHolder: ICabHolder?, @MenuRes menuRes: Int
import androidx.annotation.Nullable; ) : RecyclerView.Adapter<V>(), MaterialCab.Callback {
import androidx.appcompat.widget.AppCompatTextView; private val context: Context
import androidx.appcompat.widget.Toolbar; private var cab: MaterialCab? = null
import androidx.recyclerview.widget.RecyclerView; private val checked: MutableList<I>
private var menuRes: Int
import com.afollestad.materialcab.MaterialCab; override fun onCabCreated(materialCab: MaterialCab, menu: Menu): Boolean {
return true
import java.util.ArrayList;
import java.util.List;
import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.interfaces.ICabHolder;
public abstract class AbsMultiSelectAdapter<V extends RecyclerView.ViewHolder, I>
extends RecyclerView.Adapter<V> implements MaterialCab.Callback {
@Nullable
private final ICabHolder ICabHolder;
private final Context context;
private MaterialCab cab;
private final List<I> checked;
private int menuRes;
private AppCompatTextView dummyText;
private int oldSize = 0;
public AbsMultiSelectAdapter(
@NonNull Context context, @Nullable ICabHolder ICabHolder, @MenuRes int menuRes) {
this.ICabHolder = ICabHolder;
checked = new ArrayList<>();
this.menuRes = menuRes;
this.context = context;
}
@Override
public boolean onCabCreated(MaterialCab materialCab, Menu menu) {
playCreateAnim(materialCab);
createDummyTextView();
return true;
}
@Override
public boolean onCabFinished(MaterialCab materialCab) {
clearChecked();
cab.getToolbar().removeView(dummyText);
oldSize = 0;
return true;
}
@Override
public boolean onCabItemClicked(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.action_multi_select_adapter_check_all) {
checkAll();
} else {
onMultipleItemAction(menuItem, new ArrayList<>(checked));
cab.finish();
clearChecked();
} }
return true;
}
protected void checkAll() { override fun onCabFinished(materialCab: MaterialCab): Boolean {
if (ICabHolder != null) { clearChecked()
checked.clear(); return true
for (int i = 0; i < getItemCount(); i++) {
I identifier = getIdentifier(i);
if (identifier != null) {
checked.add(identifier);
}
}
notifyDataSetChanged();
updateCab();
} }
}
@Nullable override fun onCabItemClicked(menuItem: MenuItem): Boolean {
protected abstract I getIdentifier(int position); if (menuItem.itemId == R.id.action_multi_select_adapter_check_all) {
checkAll()
protected String getName(I object) {
return object.toString();
}
protected boolean isChecked(I identifier) {
return checked.contains(identifier);
}
protected boolean isInQuickSelectMode() {
return cab != null && cab.isActive();
}
protected abstract void onMultipleItemAction(MenuItem menuItem, List<I> selection);
protected void setMultiSelectMenuRes(@MenuRes int menuRes) {
this.menuRes = menuRes;
}
protected boolean toggleChecked(final int position) {
if (ICabHolder != null) {
I identifier = getIdentifier(position);
if (identifier == null) {
return false;
}
if (!checked.remove(identifier)) {
checked.add(identifier);
}
notifyItemChanged(position);
updateCab();
return true;
}
return false;
}
private void clearChecked() {
checked.clear();
notifyDataSetChanged();
}
@SuppressLint({"StringFormatInvalid", "StringFormatMatches"})
private void updateCab() {
if (ICabHolder != null) {
if (cab == null || !cab.isActive()) {
cab = ICabHolder.openCab(menuRes, this);
}
final int size = checked.size();
if (size <= 0) {
cab.finish();
} else if (size == 1) {
cab.setTitle(context.getString(R.string.x_selected, size));
if (oldSize == 0) {
cab.getToolbar().addView(dummyText);
}
} else {
AppCompatTextView title = (AppCompatTextView) cab.getToolbar().getChildAt(2);
dummyText.setText(title.getText());
title.setAlpha(0);
cab.setTitle(context.getString(R.string.x_selected, size));
dummyText.setTranslationX(title.getLeft() - dummyText.getLeft());
dummyText.setAlpha(1);
dummyText.setTranslationY(0);
if (oldSize > size) {
title.setTranslationY(40);
dummyText.animate().translationY(-40).alpha(0.0F).setDuration(300).start();
} else { } else {
title.setTranslationY(-40); onMultipleItemAction(menuItem, ArrayList(checked))
dummyText.animate().translationY(40).alpha(0.0F).setDuration(300).start(); cab!!.finish()
clearChecked()
} }
title.animate().translationY(0).alpha(1.0F).setDuration(300).start(); return true
}
oldSize = size;
} }
}
private void playCreateAnim(MaterialCab materialCab) { private fun checkAll() {
Toolbar cabToolbar = materialCab.getToolbar(); if (ICabHolder != null) {
int height = context.getResources().getDimensionPixelSize(R.dimen.toolbar_height); checked.clear()
cabToolbar.setTranslationY(-height); for (i in 0 until itemCount) {
cabToolbar.animate().translationYBy(height).setDuration(300).start(); val identifier = getIdentifier(i)
} if (identifier != null) {
checked.add(identifier)
}
}
notifyDataSetChanged()
updateCab()
}
}
private void createDummyTextView() { protected abstract fun getIdentifier(position: Int): I?
if (dummyText != null) return; protected open fun getName(`object`: I): String? {
dummyText = new AppCompatTextView(context); return `object`.toString()
dummyText.setSingleLine(); }
dummyText.setTextAppearance(context, R.style.ToolbarTextAppearanceNormal);
Toolbar.LayoutParams l1 = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT); protected fun isChecked(identifier: I): Boolean {
dummyText.setLayoutParams(l1); return checked.contains(identifier)
} }
protected val isInQuickSelectMode: Boolean
get() = cab != null && cab!!.isActive
protected abstract fun onMultipleItemAction(menuItem: MenuItem?, selection: List<I>?)
protected fun setMultiSelectMenuRes(@MenuRes menuRes: Int) {
this.menuRes = menuRes
}
protected fun toggleChecked(position: Int): Boolean {
if (ICabHolder != null) {
val identifier = getIdentifier(position) ?: return false
if (!checked.remove(identifier)) {
checked.add(identifier)
}
notifyItemChanged(position)
updateCab()
return true
}
return false
}
private fun clearChecked() {
checked.clear()
notifyDataSetChanged()
}
private fun updateCab() {
if (ICabHolder != null) {
if (cab == null || !cab!!.isActive) {
cab = ICabHolder.openCab(menuRes, this)
}
val size = checked.size
when {
size <= 0 -> {
cab!!.finish()
}
size == 1 -> {
cab!!.setTitle(getName(checked[0]))
}
else -> {
cab!!.setTitle(context.getString(R.string.x_selected, size))
}
}
}
}
init {
checked = ArrayList()
this.menuRes = menuRes
this.context = context
}
} }