ShowHabitPresenter: Listen to commands and auto-update

pull/699/head
Alinson S. Xavier 5 years ago
parent c84c618ef0
commit 45574753c7

@ -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,10 +29,44 @@ import javax.inject.*
class ShowHabitPresenter class ShowHabitPresenter
@Inject constructor( @Inject constructor(
val habit: Habit, val habit: Habit,
) { val commandRunner: CommandRunner,
) : CommandRunner.Listener {
private val listeners = mutableListOf<Listener>() private val listeners = mutableListOf<Listener>()
private var data = ShowHabitViewModel()
fun onResume() {
commandRunner.addListener(this)
refresh()
notifyListeners()
}
fun onPause() {
commandRunner.removeListener(this)
}
fun addListener(listener: Listener) {
listeners.add(listener)
}
fun removeListener(listener: Listener) {
listeners.remove(listener)
}
fun requestData(listener: Listener) {
listener.onData(data)
}
private fun build(): ShowHabitViewModel { 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 scores = habit.scores
val today = DateUtils.getTodayWithOffset() val today = DateUtils.getTodayWithOffset()
val lastMonth = today.minus(30) val lastMonth = today.minus(30)
@ -39,7 +74,7 @@ class ShowHabitPresenter
val scoreToday = scores.todayValue.toFloat() val scoreToday = scores.todayValue.toFloat()
val scoreLastMonth = scores.getValue(lastMonth).toFloat() val scoreLastMonth = scores.getValue(lastMonth).toFloat()
val scoreLastYear = scores.getValue(lastYear).toFloat() val scoreLastYear = scores.getValue(lastYear).toFloat()
return ShowHabitViewModel( data = ShowHabitViewModel(
title = habit.name, title = habit.name,
color = habit.color, color = habit.color,
isNumerical = habit.isNumerical, isNumerical = habit.isNumerical,
@ -50,18 +85,6 @@ class ShowHabitPresenter
) )
} }
fun addListener(listener: Listener) {
listeners.add(listener)
}
fun removeListener(listener: Listener) {
listeners.remove(listener)
}
fun requestData(listener: Listener) {
listener.onData(build())
}
interface Listener { interface Listener {
fun onData(data: ShowHabitViewModel) fun onData(data: ShowHabitViewModel)
} }

Loading…
Cancel
Save