Commit 43e47471 authored by Joey's avatar Joey

app: overview: add ShiftedAdapter

This is a PagedListAdapter clone which supports
shifting the list to make room for one or more
header items
Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
Change-Id: Ie63a8cd5f43e950d373ffed2663fd9aa9268d1ba
parent 94889d42
......@@ -16,7 +16,7 @@ import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.DiffUtil
import it.diab.R
import it.diab.core.util.PreferencesUtil
......@@ -27,6 +27,7 @@ import it.diab.holders.GlucoseHolder
import it.diab.holders.GlucoseHolderCallbacks
import it.diab.holders.HeaderHolder
import it.diab.holders.MainHolder
import it.diab.ui.ShiftedAdapter
import it.diab.ui.models.DataSetsModel
import it.diab.ui.models.LastGlucoseModel
import it.diab.util.UIUtils
......@@ -37,7 +38,7 @@ import java.util.Locale
class MainAdapter(
private val context: Context,
private val callbacks: Callbacks
) : PagedListAdapter<Glucose, MainHolder>(CALLBACK), GlucoseHolderCallbacks {
) : ShiftedAdapter<Glucose, MainHolder>(CONFIG, 1), GlucoseHolderCallbacks {
private val _openGlucose = MutableLiveData<Event<Long>>()
val openGlucose: LiveData<Event<Long>> = _openGlucose
......@@ -66,7 +67,7 @@ class MainAdapter(
if (holder is HeaderHolder) {
bindHeader(holder)
} else if (holder is GlucoseHolder) {
bindGlucose(holder, position - 1)
bindGlucose(holder, position)
}
}
......@@ -104,11 +105,6 @@ class MainAdapter(
override fun getItemViewType(position: Int) = if (position == 0) VIEW_HEADER else VIEW_GLUCOSE
override fun getItemCount(): Int {
// First item is for the header
return super.getItemCount() + 1
}
private fun buildIndicator(@ColorRes colorId: Int): Drawable? {
val resources = context.resources
val color = ContextCompat.getColor(context, colorId)
......@@ -123,13 +119,13 @@ class MainAdapter(
}
companion object {
private val CALLBACK = object : DiffUtil.ItemCallback<Glucose>() {
private val CONFIG = AsyncDifferConfig.Builder(object : DiffUtil.ItemCallback<Glucose>() {
override fun areContentsTheSame(oldItem: Glucose, newItem: Glucose) =
oldItem == newItem
override fun areItemsTheSame(oldItem: Glucose, newItem: Glucose) =
oldItem.uid == newItem.uid
}
}).build()
private const val VIEW_HEADER = 0
private const val VIEW_GLUCOSE = 1
......
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.ui
import androidx.paging.AsyncPagedListDiffer
import androidx.paging.PagedList
import androidx.recyclerview.widget.AdapterListUpdateCallback
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.ListUpdateCallback
import androidx.recyclerview.widget.RecyclerView
abstract class ShiftedAdapter<T, VH : RecyclerView.ViewHolder>(
config: AsyncDifferConfig<T>,
private val shift: Int = 0
) : RecyclerView.Adapter<VH>() {
@Suppress("LeakingThis")
private val callback = AdapterListUpdateCallback(this)
private val differ = AsyncPagedListDiffer(ShiftedListUpdateCallback(), config)
override fun getItemCount() = differ.itemCount + shift
fun submitList(pagedList: PagedList<T>?) {
differ.submitList(pagedList)
}
/**
* If [position] is less than [shift], we expect the adapter
* to properly handle the situation by using the [getItemViewType]
* in advance and behave accordingly
*/
fun getItem(position: Int): T? =
if (position < shift) null // We expect the Adapter to handle this case by using the
else differ.getItem(position - shift)
private inner class ShiftedListUpdateCallback : ListUpdateCallback {
override fun onChanged(position: Int, count: Int, payload: Any?) {
callback.onChanged(position + shift, count, payload)
}
override fun onMoved(fromPosition: Int, toPosition: Int) {
callback.onMoved(fromPosition + shift, toPosition + shift)
}
override fun onInserted(position: Int, count: Int) {
callback.onInserted(position + shift, count)
}
override fun onRemoved(position: Int, count: Int) {
callback.onRemoved(position + shift, count)
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment