mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
ShowHabitPresenter: Listen to commands and auto-update
This commit is contained in:
@@ -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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user