Skip to content

Commit

Permalink
Use ListAdapter for diffing
Browse files Browse the repository at this point in the history
  • Loading branch information
premnirmal committed Jun 28, 2022
1 parent b241ae2 commit a9d3e2a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,20 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(), ChildFragment, Portfol
attemptingFetch = true
viewModel.fetch().observe(viewLifecycleOwner) { success ->
attemptingFetch = false
binding.swipeContainer?.isRefreshing = false
binding.swipeContainer.isRefreshing = false
if (success) {
update()
}
}
} else {
attemptingFetch = false
InAppMessage.showMessage(requireActivity(), R.string.refresh_failed, error = true)
binding.swipeContainer?.isRefreshing = false
binding.swipeContainer.isRefreshing = false
}
} else {
attemptingFetch = false
InAppMessage.showMessage(requireActivity(), R.string.no_network_message, error = true)
binding.swipeContainer?.isRefreshing = false
binding.swipeContainer.isRefreshing = false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.github.premnirmal.ticker.portfolio
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.github.premnirmal.ticker.network.data.Quote
import com.github.premnirmal.ticker.portfolio.PortfolioVH.PositionVH
import com.github.premnirmal.ticker.portfolio.PortfolioVH.StockVH
Expand All @@ -20,8 +21,7 @@ class StocksAdapter constructor(
private val widgetData: WidgetData,
private val listener: QuoteClickListener,
private val dragStartListener: OnStartDragListener
) :
RecyclerView.Adapter<PortfolioVH>(), ItemTouchHelperAdapter {
) : ListAdapter<Quote, PortfolioVH>(DiffCallback()), ItemTouchHelperAdapter {

interface QuoteClickListener {
fun onClickQuoteOptions(
Expand All @@ -42,14 +42,8 @@ class StocksAdapter constructor(
const val TYPE_POSITION = 2
}

private val quoteList: MutableList<Quote>

init {
quoteList = ArrayList()
quoteList.addAll(widgetData.getStocks())
}

fun remove(quote: Quote) {
val quoteList = currentList
val index = quoteList.indexOf(quote)
val removed = quoteList.remove(quote)
if (index >= 0 && removed) {
Expand All @@ -58,13 +52,11 @@ class StocksAdapter constructor(
}

fun refresh() {
quoteList.clear()
quoteList.addAll(widgetData.getStocks())
notifyDataSetChanged()
submitList(widgetData.getStocks())
}

override fun getItemViewType(position: Int): Int {
val stock = quoteList[position]
val stock = currentList[position]
return when {
stock.hasPositions() -> TYPE_POSITION
else -> TYPE_STOCK
Expand All @@ -90,7 +82,7 @@ class StocksAdapter constructor(
holder: PortfolioVH,
position: Int
) {
holder.update(quoteList[position], listener)
holder.update(currentList[position], listener)
holder.itemView.setOnLongClickListener {
dragStartListener.onStartDrag(holder)
true
Expand All @@ -99,14 +91,13 @@ class StocksAdapter constructor(

override fun getItemId(position: Int): Long = position.toLong()

override fun getItemCount(): Int = quoteList.size

override fun onItemMove(
fromPosition: Int,
toPosition: Int
): Boolean {
quoteList.add(toPosition, quoteList.removeAt(fromPosition))
val newTickerList = quoteList.mapTo(ArrayList()) { it.symbol }
val data = ArrayList(widgetData.getStocks())
data.add(toPosition, data.removeAt(fromPosition))
val newTickerList = data.mapTo(ArrayList()) { it.symbol }
widgetData.rearrange(newTickerList)
notifyItemMoved(fromPosition, toPosition)
return true
Expand All @@ -115,4 +106,20 @@ class StocksAdapter constructor(
override fun onItemDismiss(position: Int) {
dragStartListener.onStopDrag()
}

class DiffCallback : DiffUtil.ItemCallback<Quote>() {
override fun areItemsTheSame(
oldItem: Quote,
newItem: Quote
): Boolean {
return oldItem.symbol == newItem.symbol
}

override fun areContentsTheSame(
oldItem: Quote,
newItem: Quote
): Boolean {
return oldItem == newItem
}
}
}
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# this file is purely for f-droid because it cannot infer the version name/code from the git tag
versionName=3.9.791
versionCode=300900791
versionName=3.9.792
versionCode=300900792

0 comments on commit a9d3e2a

Please sign in to comment.