Simplify data trasfer in MainScreenDataSource

pull/498/head
Alinson S. Xavier 7 years ago
parent 6ac7ef7807
commit f491acdda9

@ -20,7 +20,6 @@
package org.isoron.uhabits.backend package org.isoron.uhabits.backend
import org.isoron.platform.concurrency.* import org.isoron.platform.concurrency.*
import org.isoron.platform.gui.*
import org.isoron.platform.time.* import org.isoron.platform.time.*
import org.isoron.uhabits.models.* import org.isoron.uhabits.models.*
@ -28,13 +27,11 @@ class MainScreenDataSource(val habits: MutableMap<Int, Habit>,
val checkmarks: MutableMap<Habit, CheckmarkList>, val checkmarks: MutableMap<Habit, CheckmarkList>,
val taskRunner: TaskRunner) { val taskRunner: TaskRunner) {
private val today = LocalDate(2019, 3, 30) private val today = LocalDate(2019, 3, 30) /* TODO */
data class Data(val ids: List<Int>, data class Data(val habits: List<Habit>,
val scores: List<Double>, val currentScore: Map<Habit, Double>,
val names: List<String>, val checkmarkValues: Map<Habit, List<Int>>)
val colors: List<PaletteColor>,
val checkmarks: List<List<Int>>)
val observable = Observable<Listener>() val observable = Observable<Listener>()
@ -44,17 +41,14 @@ class MainScreenDataSource(val habits: MutableMap<Int, Habit>,
fun requestData() { fun requestData() {
taskRunner.runInBackground { taskRunner.runInBackground {
val filteredHabits = habits.values.filter { h -> !h.isArchived } val filtered = habits.values.filter { h -> !h.isArchived }
val ids = filteredHabits.map { it.id } val currentScores = filtered.associate { it to 0.0 /* TODO */}
val scores = filteredHabits.map { 0.0 } val recentCheckmarks = filtered.associate {
val names = filteredHabits.map { it.name } val allValues = checkmarks[it]!!.getValuesUntil(today)
val colors = filteredHabits.map { it.color } if (allValues.size <= 7) it to allValues
val ck = filteredHabits.map { habit -> else it to allValues.subList(0, 7)
val allValues = checkmarks[habit]!!.getValuesUntil(today)
if (allValues.size <= 7) allValues
else allValues.subList(0, 7)
} }
val data = Data(ids, scores, names, colors, ck) val data = Data(filtered, currentScores, recentCheckmarks)
taskRunner.runInForeground { taskRunner.runInForeground {
observable.notifyListeners { listener -> observable.notifyListeners { listener ->
listener.onDataChanged(data) listener.onDataChanged(data)

@ -101,7 +101,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
self.dataSource = backend.mainScreenDataSource self.dataSource = backend.mainScreenDataSource
self.theme = backend.theme self.theme = backend.theme
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
self.dataSource.addListener(listener: self) self.dataSource.observable.addListener(listener: self)
self.dataSource.requestData() self.dataSource.requestData()
} }
@ -133,16 +133,16 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
} }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data?.names.count ?? 0 return data?.habits.count ?? 0
} }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = indexPath.row let row = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainScreenCell let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainScreenCell
let color = theme.color(paletteIndex: data!.colors[row].index) let habit = data!.habits[row]
cell.update(name: data!.names[row], cell.update(name: habit.name,
color: color, color: theme.color(paletteIndex: habit.color.index),
values: data!.checkmarks[row]) values: data!.checkmarkValues[habit]!)
return cell return cell
} }
@ -164,7 +164,9 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener
} }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let color = theme.color(paletteIndex: data!.colors[indexPath.row].index) let color = theme.color(paletteIndex: data!.habits[indexPath.row].color.index)
self.navigationController?.pushViewController(ShowHabitController(theme: theme, color: color), animated: true) self.navigationController?.pushViewController(ShowHabitController(theme: theme,
color: color),
animated: true)
} }
} }

Loading…
Cancel
Save