diff --git a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt index 043806bb7..3a2a470e9 100644 --- a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt +++ b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt @@ -20,7 +20,6 @@ package org.isoron.uhabits.backend import org.isoron.platform.concurrency.* -import org.isoron.platform.gui.* import org.isoron.platform.time.* import org.isoron.uhabits.models.* @@ -28,13 +27,11 @@ class MainScreenDataSource(val habits: MutableMap, val checkmarks: MutableMap, val taskRunner: TaskRunner) { - private val today = LocalDate(2019, 3, 30) + private val today = LocalDate(2019, 3, 30) /* TODO */ - data class Data(val ids: List, - val scores: List, - val names: List, - val colors: List, - val checkmarks: List>) + data class Data(val habits: List, + val currentScore: Map, + val checkmarkValues: Map>) val observable = Observable() @@ -44,17 +41,14 @@ class MainScreenDataSource(val habits: MutableMap, fun requestData() { taskRunner.runInBackground { - val filteredHabits = habits.values.filter { h -> !h.isArchived } - val ids = filteredHabits.map { it.id } - val scores = filteredHabits.map { 0.0 } - val names = filteredHabits.map { it.name } - val colors = filteredHabits.map { it.color } - val ck = filteredHabits.map { habit -> - val allValues = checkmarks[habit]!!.getValuesUntil(today) - if (allValues.size <= 7) allValues - else allValues.subList(0, 7) + val filtered = habits.values.filter { h -> !h.isArchived } + val currentScores = filtered.associate { it to 0.0 /* TODO */} + val recentCheckmarks = filtered.associate { + val allValues = checkmarks[it]!!.getValuesUntil(today) + if (allValues.size <= 7) it to allValues + else it to allValues.subList(0, 7) } - val data = Data(ids, scores, names, colors, ck) + val data = Data(filtered, currentScores, recentCheckmarks) taskRunner.runInForeground { observable.notifyListeners { listener -> listener.onDataChanged(data) diff --git a/ios/Application/Frontend/MainScreenController.swift b/ios/Application/Frontend/MainScreenController.swift index 0689faf14..fd9d8645b 100644 --- a/ios/Application/Frontend/MainScreenController.swift +++ b/ios/Application/Frontend/MainScreenController.swift @@ -101,7 +101,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener self.dataSource = backend.mainScreenDataSource self.theme = backend.theme super.init(nibName: nil, bundle: nil) - self.dataSource.addListener(listener: self) + self.dataSource.observable.addListener(listener: self) self.dataSource.requestData() } @@ -133,16 +133,16 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener } 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 { let row = indexPath.row let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainScreenCell - let color = theme.color(paletteIndex: data!.colors[row].index) - cell.update(name: data!.names[row], - color: color, - values: data!.checkmarks[row]) + let habit = data!.habits[row] + cell.update(name: habit.name, + color: theme.color(paletteIndex: habit.color.index), + values: data!.checkmarkValues[habit]!) return cell } @@ -164,7 +164,9 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - let color = theme.color(paletteIndex: data!.colors[indexPath.row].index) - self.navigationController?.pushViewController(ShowHabitController(theme: theme, color: color), animated: true) + let color = theme.color(paletteIndex: data!.habits[indexPath.row].color.index) + self.navigationController?.pushViewController(ShowHabitController(theme: theme, + color: color), + animated: true) } }