ShowHabitPresenter: Listen to commands and auto-update

This commit is contained in:
2020-12-22 10:23:07 -06:00
parent c84c618ef0
commit 45574753c7
2 changed files with 53 additions and 20 deletions

View File

@@ -26,4 +26,14 @@ class ShowHabitActivity : HabitsActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setScreen(component.showHabitScreen) setScreen(component.showHabitScreen)
} }
override fun onResume() {
super.onResume()
component.showHabitPresenter.onResume()
}
override fun onPause() {
component.showHabitPresenter.onPause()
super.onPause()
}
} }

View File

@@ -20,6 +20,7 @@
package org.isoron.uhabits.activities.habits.show package org.isoron.uhabits.activities.habits.show
import org.isoron.androidbase.activities.* import org.isoron.androidbase.activities.*
import org.isoron.uhabits.core.commands.*
import org.isoron.uhabits.core.models.* import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.utils.* import org.isoron.uhabits.core.utils.*
import javax.inject.* import javax.inject.*
@@ -28,26 +29,20 @@ import javax.inject.*
class ShowHabitPresenter class ShowHabitPresenter
@Inject constructor( @Inject constructor(
val habit: Habit, val habit: Habit,
) { val commandRunner: CommandRunner,
private val listeners = mutableListOf<Listener>() ) : CommandRunner.Listener {
private fun build(): ShowHabitViewModel { private val listeners = mutableListOf<Listener>()
val scores = habit.scores private var data = ShowHabitViewModel()
val today = DateUtils.getTodayWithOffset()
val lastMonth = today.minus(30) fun onResume() {
val lastYear = today.minus(365) commandRunner.addListener(this)
val scoreToday = scores.todayValue.toFloat() refresh()
val scoreLastMonth = scores.getValue(lastMonth).toFloat() notifyListeners()
val scoreLastYear = scores.getValue(lastYear).toFloat() }
return ShowHabitViewModel(
title = habit.name, fun onPause() {
color = habit.color, commandRunner.removeListener(this)
isNumerical = habit.isNumerical,
scoreToday = scoreToday,
scoreMonthDiff = scoreToday - scoreLastMonth,
scoreYearDiff = scoreToday - scoreLastYear,
totalCount = habit.repetitions.totalCount,
)
} }
fun addListener(listener: Listener) { fun addListener(listener: Listener) {
@@ -59,7 +54,35 @@ class ShowHabitPresenter
} }
fun requestData(listener: Listener) { fun requestData(listener: Listener) {
listener.onData(build()) listener.onData(data)
}
override fun onCommandExecuted(command: Command?, refreshKey: Long?) {
refresh()
notifyListeners()
}
private fun notifyListeners() {
for (l in listeners) l.onData(data)
}
private fun refresh() {
val scores = habit.scores
val today = DateUtils.getTodayWithOffset()
val lastMonth = today.minus(30)
val lastYear = today.minus(365)
val scoreToday = scores.todayValue.toFloat()
val scoreLastMonth = scores.getValue(lastMonth).toFloat()
val scoreLastYear = scores.getValue(lastYear).toFloat()
data = ShowHabitViewModel(
title = habit.name,
color = habit.color,
isNumerical = habit.isNumerical,
scoreToday = scoreToday,
scoreMonthDiff = scoreToday - scoreLastMonth,
scoreYearDiff = scoreToday - scoreLastYear,
totalCount = habit.repetitions.totalCount,
)
} }
interface Listener { interface Listener {