Implemented zoom listener to dynamically change span count

This commit is contained in:
merkost 2023-07-12 18:30:03 +10:00
parent c0037073d1
commit 2e889f2093
2 changed files with 83 additions and 34 deletions

View File

@ -7,13 +7,11 @@ import android.graphics.drawable.Icon
import android.net.Uri
import android.text.TextUtils
import android.util.TypedValue
import android.view.Menu
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.*
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
@ -42,21 +40,27 @@ class ContactsAdapter(
recyclerView: MyRecyclerView,
highlightText: String = "",
private val refreshItemsListener: RefreshItemsListener? = null,
private val viewType: Int = VIEW_TYPE_LIST,
var viewType: Int = VIEW_TYPE_LIST,
private val showDeleteButton: Boolean = true,
private val enableDrag: Boolean = false,
itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), ItemTouchHelperContract {
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick),
ItemTouchHelperContract, MyRecyclerView.MyZoomListener {
private var textToHighlight = highlightText
private var fontSize = activity.getTextSize()
private var touchHelper: ItemTouchHelper? = null
private var startReorderDragListener: StartReorderDragListener? = null
var onDragEndListener: (() -> Unit)? = null
var onSpanCountListener: (Int) -> Unit = {}
init {
setupDragListener(true)
if (recyclerView.layoutManager is GridLayoutManager) {
setupZoomListener(this)
}
if (enableDrag) {
touchHelper = ItemTouchHelper(ItemMoveCallback(this, viewType == VIEW_TYPE_GRID))
touchHelper!!.attachToRecyclerView(recyclerView)
@ -340,4 +344,33 @@ class ContactsAdapter(
override fun onRowClear(myViewHolder: ViewHolder?) {
onDragEndListener?.invoke()
}
override fun zoomIn() {
val layoutManager = recyclerView.layoutManager
if (layoutManager is GridLayoutManager) {
val currentSpanCount = layoutManager.spanCount
val newSpanCount = (currentSpanCount - 1).coerceIn(MIN_COLUMNS, MAX_COLUMNS)
layoutManager.spanCount = newSpanCount
recyclerView.requestLayout()
onSpanCountListener(newSpanCount)
}
}
override fun zoomOut() {
val layoutManager = recyclerView.layoutManager
if (layoutManager is GridLayoutManager) {
val currentSpanCount = layoutManager.spanCount
val newSpanCount = (currentSpanCount + 1).coerceIn(MIN_COLUMNS, MAX_COLUMNS)
layoutManager.spanCount = newSpanCount
recyclerView.requestLayout()
onSpanCountListener(newSpanCount)
}
}
companion object {
private const val MIN_COLUMNS = 2
private const val MAX_COLUMNS = 6
}
}

View File

@ -91,42 +91,56 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
val viewType = context.config.viewType
setViewType(viewType)
ContactsAdapter(
activity = activity as SimpleActivity,
contacts = allContacts,
recyclerView = fragment_list,
refreshItemsListener = this,
viewType = viewType,
showDeleteButton = false,
enableDrag = true,
) {
if (context.config.showCallConfirmation) {
CallConfirmationDialog(activity as SimpleActivity, (it as Contact).getNameToDisplay()) {
val currAdapter = fragment_list.adapter as ContactsAdapter?
if (currAdapter == null) {
ContactsAdapter(
activity = activity as SimpleActivity,
contacts = allContacts,
recyclerView = fragment_list,
refreshItemsListener = this,
viewType = viewType,
showDeleteButton = false,
enableDrag = true,
) {
if (context.config.showCallConfirmation) {
CallConfirmationDialog(activity as SimpleActivity, (it as Contact).getNameToDisplay()) {
activity?.apply {
initiateCall(it) { launchCallIntent(it) }
}
}
} else {
activity?.apply {
initiateCall(it) { launchCallIntent(it) }
initiateCall(it as Contact) { launchCallIntent(it) }
}
}
} else {
activity?.apply {
initiateCall(it as Contact) { launchCallIntent(it) }
}.apply {
fragment_list.adapter = this
onDragEndListener = {
val adapter = fragment_list?.adapter
if (adapter is ContactsAdapter) {
val items = adapter.contacts
saveCustomOrderToPrefs(items)
setupLetterFastScroller(items)
}
}
onSpanCountListener = { newSpanCount ->
context.config.gridLayoutSpanCount = newSpanCount
}
}
}.apply {
fragment_list.adapter = this
onDragEndListener = {
val adapter = fragment_list?.adapter
if (adapter is ContactsAdapter) {
val items = adapter.contacts
saveCustomOrderToPrefs(items)
setupLetterFastScroller(items)
}
if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation()
}
} else {
currAdapter.updateItems(allContacts)
currAdapter.viewType = viewType
currAdapter.recyclerView.requestLayout()
}
if (context.areSystemAnimationsEnabled) {
fragment_list.scheduleLayoutAnimation()
}
}
private fun sortByCustomOrder(favorites: List<Contact>): ArrayList<Contact> {
@ -182,9 +196,11 @@ class FavoritesFragment(context: Context, attributeSet: AttributeSet) : MyViewPa
}
private fun setViewType(viewType: Int) {
val spanCount = context.config.gridLayoutSpanCount
val layoutManager = if (viewType == VIEW_TYPE_GRID) {
letter_fastscroller.beGone()
MyGridLayoutManager(context, 3)
MyGridLayoutManager(context, spanCount)
} else {
letter_fastscroller.beVisible()
MyLinearLayoutManager(context)