Skip to content

Commit

Permalink
Move EntryListViewAdapter callbacks to an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
albertvaka committed Nov 24, 2023
1 parent bf0f322 commit 6355a65
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/org/kde/bettercounter/ChartsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class ChartsAdapter(

private var numCharts : Int = countNumCharts(counter)
override fun getItemCount(): Int = numCharts
fun getCounterName(): String = counter.name

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChartHolder {
val binding = FragmentChartBinding.inflate(inflater, parent, false)
Expand Down
33 changes: 19 additions & 14 deletions app/src/main/java/org/kde/bettercounter/EntryListViewAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ import java.util.*
class EntryListViewAdapter(
private var activity: AppCompatActivity,
private var viewModel: ViewModel,
private var listObserver: EntryListObserver,

) : RecyclerView.Adapter<EntryViewHolder>(), DragAndSwipeTouchHelper.ListGesturesCallback
{
var onItemSelected: ((Int, CounterSummary) -> Unit)? = null // Gets called again if the last selected item gets updated
var onItemAdded: ((Int) -> Unit)? = null
interface EntryListObserver {
fun onItemSelected(position : Int, counter : CounterSummary)
fun onSelectedItemUpdated(position : Int, counter : CounterSummary)
fun onItemAdded(position : Int)
}

var lastSelectedCounterName : String? = null
fun clearItemSelected() { lastSelectedCounterName = null }
var currentSelectedCounterName : String? = null
fun clearItemSelected() { currentSelectedCounterName = null }

private val inflater: LayoutInflater = LayoutInflater.from(activity)
private var counters: MutableList<String> = mutableListOf()
Expand All @@ -49,8 +54,8 @@ class EntryListViewAdapter(
fun observeNewCounter(counterName : String) {
viewModel.getCounterSummary(counterName).observe(activity) {
notifyItemChanged(counters.indexOf(it.name), Unit)
if (lastSelectedCounterName == it.name) {
onItemSelected?.invoke(counters.indexOf(it.name), it)
if (currentSelectedCounterName == it.name) {
listObserver.onSelectedItemUpdated(counters.indexOf(it.name), it)
}
}
}
Expand All @@ -72,15 +77,15 @@ class EntryListViewAdapter(
val position = counters.size - 1
notifyItemInserted(position)
observeNewCounter(counterName)
onItemAdded?.invoke(position)
listObserver.onItemAdded(position)
}
}

override fun onCounterRemoved(counterName: String) {
val position = counters.indexOf(counterName)
counters.removeAt(position)
if (lastSelectedCounterName == counterName) {
lastSelectedCounterName = null
if (currentSelectedCounterName == counterName) {
currentSelectedCounterName = null
}
activity.runOnUiThread {
notifyItemRemoved(position)
Expand All @@ -90,9 +95,9 @@ class EntryListViewAdapter(
override fun onCounterRenamed(oldName : String, newName: String) {
val position = counters.indexOf(oldName)
counters[position] = newName
if (lastSelectedCounterName == oldName) {
lastSelectedCounterName = newName
onItemSelected?.invoke(position,viewModel.getCounterSummary(newName).value!!)
if (currentSelectedCounterName == oldName) {
currentSelectedCounterName = newName
listObserver.onSelectedItemUpdated(position,viewModel.getCounterSummary(newName).value!!)
}
activity.runOnUiThread {
// passing a second parameter disables the disappear+appear animation
Expand All @@ -113,8 +118,8 @@ class EntryListViewAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EntryViewHolder {
val binding = FragmentEntryBinding.inflate(inflater, parent, false)
val holder = EntryViewHolder(activity, binding, viewModel, touchHelper) { counter ->
lastSelectedCounterName = counter.name
onItemSelected?.invoke(counters.indexOf(counter.name), counter)
currentSelectedCounterName = counter.name
listObserver.onItemSelected(counters.indexOf(counter.name), counter)
}
return holder
}
Expand Down
47 changes: 24 additions & 23 deletions app/src/main/java/org/kde/bettercounter/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,36 @@ class MainActivity : AppCompatActivity() {

// Counter list
// ------------
entryViewAdapter = EntryListViewAdapter(this, viewModel)
entryViewAdapter.onItemAdded = { pos -> binding.recycler.smoothScrollToPosition(pos) }
entryViewAdapter.onItemSelected = { position: Int, counter: CounterSummary ->
if (sheetBehavior.state == BottomSheetBehavior.STATE_HIDDEN) {
sheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
onBackPressedCloseSheetCallback.isEnabled = true
sheetIsExpanding = true
entryViewAdapter = EntryListViewAdapter(this, viewModel, object : EntryListViewAdapter.EntryListObserver {
override fun onItemAdded(position: Int) {
binding.recycler.smoothScrollToPosition(position)
}
binding.recycler.setPadding(0, 0, 0, sheetUnfoldedPadding)
binding.recycler.smoothScrollToPosition(position)

setFabToEdit(counter)
override fun onSelectedItemUpdated(position: Int, counter: CounterSummary) {
binding.detailsTitle.text = counter.name
val interval = intervalOverride ?: counter.intervalForChart
val adapter = ChartsAdapter(this@MainActivity, viewModel, counter, interval) { newInterval ->
intervalOverride = newInterval
onSelectedItemUpdated(position, counter)
}
binding.charts.swapAdapter(adapter, true)
binding.charts.scrollToPosition(adapter.itemCount-1) // Select the latest chart
}
override fun onItemSelected(position: Int, counter: CounterSummary) {
if (sheetBehavior.state == BottomSheetBehavior.STATE_HIDDEN) {
sheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
onBackPressedCloseSheetCallback.isEnabled = true
sheetIsExpanding = true
}
binding.recycler.setPadding(0, 0, 0, sheetUnfoldedPadding)
binding.recycler.smoothScrollToPosition(position)

binding.detailsTitle.text = counter.name
setFabToEdit(counter)

val currentAdapter = (binding.charts.adapter as ChartsAdapter)
if (currentAdapter.getCounterName() != counter.name) {
intervalOverride = null
}

val interval = intervalOverride ?: counter.intervalForChart
val adapter = ChartsAdapter(this, viewModel, counter, interval) { newInterval ->
intervalOverride = newInterval
entryViewAdapter.onItemSelected?.invoke(position, counter)
onSelectedItemUpdated(position, counter)
}
binding.charts.swapAdapter(adapter, true)
binding.charts.scrollToPosition(adapter.itemCount-1)
}

})
binding.recycler.adapter = entryViewAdapter
binding.recycler.layoutManager = LinearLayoutManager(this)

Expand Down

0 comments on commit 6355a65

Please sign in to comment.