From 082c575f82479dbfa5ff418c46b2cf4cff94f40e Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Tue, 2 Apr 2019 07:46:41 -0500 Subject: [PATCH] Allow user to show/hide archived and completed habits --- core/assets/main/databases/template.db | Bin 462848 -> 462848 bytes .../org/isoron/uhabits/backend/Backend.kt | 5 +- .../uhabits/backend/MainScreenDataSource.kt | 33 +++++-- .../kotlin/org/isoron/uhabits/i18n/Strings.kt | 1 + .../Frontend/MainScreenController.swift | 86 ++++++++++++------ ios/uhabits.xcodeproj/project.pbxproj | 4 - 6 files changed, 84 insertions(+), 45 deletions(-) diff --git a/core/assets/main/databases/template.db b/core/assets/main/databases/template.db index 37e613a7bac1bfc3ef755d7d0aebd69e1c7d5501..5e920e8d7d83a18cbfb1fe56e31612f416f3310c 100644 GIT binary patch delta 1804 zcmZA1e@xV690%~v_ZRm)c=+BSAfhMSg&Oyih(()G!688;&S0U{#@tvrGij32a?3oI zPHHq05BXXs9tOf1(FMX+k(AA5BU{nnac*p+mW(w({xFjtt)Az0pVqwF{dj$!=llIW z_uS|GIS_Om2)aUYrmGfWjAC$%8P^GE2j>Dr}f}yWzA@% z#9y*CTB&&R)dkrA)3;Viwmc!FR5H{~wO{=~y`he)_cYlY3dw6Er+MZZr9@V?T5^}t zB>T0iVDM{49^pG^(wna>+Xu7{wKeYs&YiVvND4A^@DbE`JMVXbvS{=Ar zfSRdc#hMyclODHJw_2*0R<~A`#J4EptrqZfgm1>>eZB}hUs(f+me--iz&ne2?!~>}okOilbAfjrl^)W;yMRjjxC6X@qs-om znqju{w~fXOUoPN)eefnwDWVPk@K{k9HMfNqmx7v!P5`Q+{`l=YP_1TUQnN-TqcRm` zzusWl+|qPhlzv|y*RShW^}j-U0o+fa zMHAr8LW?%Q{Ulm60`4c!q7`swqD3>{_Gd&6g!_R^Sh}!Ci6}q99z}}!BWx8?6d++M zk)m)3TY(f&AuJ6kvO?G+ND&soJYo9zokUy+bK|hc3t_295g5W!kRmXIEk}y@66QjR z{1UbdDUwduQl#h@ge4&b7joFONCHSnA{MH9^6GO}iEh@7ZQ6{|(3akhz874>+ zk_@_iu+o^{tmc+I*^3%cU@u}AfhaO6B}8pjmY8XC`9L4LaN+#UIsDJzf3LsA;%~8p zgpHPd5G6OTQK^Gr$<4Z19ox@b(r4+obSaYJ%8>%@0#~4|polO#q&F2B*O>JwphL-e zz0QOHbh>z~EB2kJt`@2SR;QJ)TaeLIXbyOtl>wJCBhuqsXF!dSPuoT4r8m0!ZGBrp{8x-O}PqFeG)BJXm)D7Q+Vi2Nu4o~0_+>d*47zep7MuN&f zjHDp3Kr9pYibupVVm*(}5Mu?ivRszWN?8Rv#7?m~@|_CQKr+?6@l}x27~ubj7l-Lh zecZO(V?nUB^XV`R@T^J7GRq!c26;|()%z?8wxCM8uh)aEje9WNqlovYk80f~`wSre z;I&=!uHBs-(t*5?=++_kCc6dXUn*_hdKTn)?s-V3cdq10$DVk>p;2k#;{$L|m10S0 z;2ZtWWU%B@^9tkBRVdm zs>uVp7C`>VzCvaE&G+%%9fH>N%WHXc(HL%Fv`{J;vUoX0ElOu6S`ouQbnVs#gC27s}IX diff --git a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/Backend.kt b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/Backend.kt index 62b45d81e..ecb18cdd6 100644 --- a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/Backend.kt +++ b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/Backend.kt @@ -70,7 +70,8 @@ class Backend(databaseName: String, checkmarks[habit]?.setManualCheckmarks(checks) } } - mainScreenDataSource = MainScreenDataSource(habits, + mainScreenDataSource = MainScreenDataSource(preferences, + habits, checkmarks, taskRunner) } @@ -99,6 +100,4 @@ class Backend(databaseName: String, habitsRepository.update(modified) } } - - } 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 3a2a470e9..ba69e0c55 100644 --- a/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt +++ b/core/src/commonMain/kotlin/org/isoron/uhabits/backend/MainScreenDataSource.kt @@ -22,8 +22,10 @@ package org.isoron.uhabits.backend import org.isoron.platform.concurrency.* import org.isoron.platform.time.* import org.isoron.uhabits.models.* +import org.isoron.uhabits.models.Checkmark.Companion.UNCHECKED -class MainScreenDataSource(val habits: MutableMap, +class MainScreenDataSource(val preferences: Preferences, + val habits: MutableMap, val checkmarks: MutableMap, val taskRunner: TaskRunner) { @@ -41,16 +43,31 @@ class MainScreenDataSource(val habits: MutableMap, fun requestData() { taskRunner.runInBackground { - 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) + var filtered = habits.values.toList() + + if (!preferences.showArchived) { + filtered = filtered.filter { !it.isArchived } + } + + val recentCheckmarks = filtered.associate { habit -> + val allValues = checkmarks.getValue(habit).getValuesUntil(today) + if (allValues.size <= 7) habit to allValues + else habit to allValues.subList(0, 7) } - val data = Data(filtered, currentScores, recentCheckmarks) + + if (!preferences.showCompleted) { + filtered = filtered.filter { habit -> + recentCheckmarks.getValue(habit)[0] == UNCHECKED + } + } + + val currentScores = filtered.associate { + it to 0.0 /* TODO */ + } + taskRunner.runInForeground { observable.notifyListeners { listener -> + val data = Data(filtered, currentScores, recentCheckmarks) listener.onDataChanged(data) } } diff --git a/core/src/commonMain/kotlin/org/isoron/uhabits/i18n/Strings.kt b/core/src/commonMain/kotlin/org/isoron/uhabits/i18n/Strings.kt index ed3744bc3..823ac364c 100644 --- a/core/src/commonMain/kotlin/org/isoron/uhabits/i18n/Strings.kt +++ b/core/src/commonMain/kotlin/org/isoron/uhabits/i18n/Strings.kt @@ -186,4 +186,5 @@ open class Strings() { open val weekends = "Weekends" open val year = "Year" open val yes = "Yes" + open val day_mode = "Day mode" } diff --git a/ios/Application/Frontend/MainScreenController.swift b/ios/Application/Frontend/MainScreenController.swift index 81f5a8041..0f19ec95b 100644 --- a/ios/Application/Frontend/MainScreenController.swift +++ b/ios/Application/Frontend/MainScreenController.swift @@ -61,13 +61,11 @@ class MainScreenCell : UITableViewCell { fatalError() } - func update(name: String, - color: Color, - values: [KotlinInt]) { - - label.text = name + func update(habit: Habit, values: [KotlinInt], theme: Theme) { + var color = theme.color(paletteIndex: habit.color.index) + if habit.isArchived { color = theme.mediumContrastTextColor } + label.text = habit.name label.textColor = color.uicolor - ring.component = Ring(color: color, percentage: Double.random(in: 0...1), thickness: 2.5, @@ -90,6 +88,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener var backend: Backend var dataSource: MainScreenDataSource var data: MainScreenDataSource.Data? + var preferences: Preferences var theme: Theme var nButtons = 3 var strings: Strings @@ -103,14 +102,11 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener self.strings = backend.strings self.dataSource = backend.mainScreenDataSource self.theme = backend.theme + self.preferences = backend.preferences super.init(nibName: nil, bundle: nil) self.dataSource.observable.addListener(listener: self) self.dataSource.requestData() } - - func onDataChanged(newData: MainScreenDataSource.Data) { - self.data = newData - } override func viewDidLoad() { self.title = strings.main_activity_title @@ -140,12 +136,9 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let row = indexPath.row let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainScreenCell - let habit = data!.habits[row] - cell.update(name: habit.name, - color: theme.color(paletteIndex: habit.color.index), - values: data!.checkmarkValues[habit]!) + let habit = data!.habits[indexPath.row] + cell.update(habit: habit, values: data!.checkmarkValues[habit]!, theme: theme) return cell } @@ -168,9 +161,7 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let habit = data!.habits[indexPath.row] - let color = theme.color(paletteIndex: habit.color.index) - self.navigationController?.pushViewController(DetailScreenController(habit: habit, backend: backend), - animated: true) + self.navigationController?.pushViewController(DetailScreenController(habit: habit, backend: backend), animated: true) } @objc func onCreateHabitClicked() { @@ -179,18 +170,47 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener @objc func onMoreActionsClicked() { let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) - alert.addAction(UIAlertAction(title: strings.show_archived, style: .default) { - (action: UIAlertAction) -> Void in - // TODO - }) - alert.addAction(UIAlertAction(title: strings.hide_completed, style: .default) { - (action: UIAlertAction) -> Void in - // TODO - }) - alert.addAction(UIAlertAction(title: strings.night_mode, style: .default) { - (action: UIAlertAction) -> Void in - // TODO - }) + + if preferences.showArchived { + alert.addAction(UIAlertAction(title: strings.hide_archived, style: .default) { + (action: UIAlertAction) -> Void in + self.preferences.showArchived = false + self.dataSource.requestData() + }) + } else { + alert.addAction(UIAlertAction(title: strings.show_archived, style: .default) { + (action: UIAlertAction) -> Void in + self.preferences.showArchived = true + self.dataSource.requestData() + }) + } + + if preferences.showCompleted { + alert.addAction(UIAlertAction(title: strings.hide_completed, style: .default) { + (action: UIAlertAction) -> Void in + self.preferences.showCompleted = false + self.dataSource.requestData() + }) + } else { + alert.addAction(UIAlertAction(title: strings.show_completed, style: .default) { + (action: UIAlertAction) -> Void in + self.preferences.showCompleted = true + self.dataSource.requestData() + }) + } + + if preferences.nightMode { + alert.addAction(UIAlertAction(title: strings.day_mode, style: .default) { + (action: UIAlertAction) -> Void in + self.preferences.nightMode = false + }) + } else { + alert.addAction(UIAlertAction(title: strings.night_mode, style: .default) { + (action: UIAlertAction) -> Void in + self.preferences.nightMode = true + }) + } + alert.addAction(UIAlertAction(title: strings.help, style: .default) { (action: UIAlertAction) -> Void in // TODO @@ -205,4 +225,10 @@ class MainScreenController: UITableViewController, MainScreenDataSourceListener }) present(alert, animated: true, completion: nil) } + + func onDataChanged(newData: MainScreenDataSource.Data) { + data = newData + let sections = NSIndexSet(indexesIn: NSMakeRange(0, self.tableView.numberOfSections)) + tableView.reloadSections(sections as IndexSet, with: .automatic) + } } diff --git a/ios/uhabits.xcodeproj/project.pbxproj b/ios/uhabits.xcodeproj/project.pbxproj index c0e4f2891..9c92cf952 100644 --- a/ios/uhabits.xcodeproj/project.pbxproj +++ b/ios/uhabits.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - 0057EC2B224C4CDB00C49288 /* icons in Resources */ = {isa = PBXBuildFile; fileRef = 0057EC2A224C4CDB00C49288 /* icons */; }; 006EFE4C2252E9F3008464E0 /* IosLocaleTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 006EFE4A2252E9D3008464E0 /* IosLocaleTest.swift */; }; 006EFE4E2252EA2B008464E0 /* IosLocale.swift in Sources */ = {isa = PBXBuildFile; fileRef = 006EFE4D2252EA2B008464E0 /* IosLocale.swift */; }; 00A5B42822009F590024E00C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00A5B42722009F590024E00C /* AppDelegate.swift */; }; @@ -58,7 +57,6 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 0057EC2A224C4CDB00C49288 /* icons */ = {isa = PBXFileReference; lastKnownFileType = folder; path = icons; sourceTree = ""; }; 006EFE4A2252E9D3008464E0 /* IosLocaleTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosLocaleTest.swift; sourceTree = ""; }; 006EFE4D2252EA2B008464E0 /* IosLocale.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IosLocale.swift; sourceTree = ""; }; 00A5B42422009F590024E00C /* uhabits.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = uhabits.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -171,7 +169,6 @@ 00C0C6C022465F80003D8AF0 /* Assets */ = { isa = PBXGroup; children = ( - 0057EC2A224C4CDB00C49288 /* icons */, 00C0C6BB22465F65003D8AF0 /* databases */, 00C0C6BA22465F65003D8AF0 /* fonts */, 00C0C6BC22465F65003D8AF0 /* migrations */, @@ -291,7 +288,6 @@ 00C0C6BD22465F65003D8AF0 /* fonts in Resources */, 00C0C6BE22465F65003D8AF0 /* databases in Resources */, 00C0C6BF22465F65003D8AF0 /* migrations in Resources */, - 0057EC2B224C4CDB00C49288 /* icons in Resources */, 00A5B42F22009F5A0024E00C /* Assets.xcassets in Resources */, 00D48BD12200A31300CC4527 /* Launch.storyboard in Resources */, );