From 55d472c93ec7a13ca0fd0395ae8e747b55aca6e2 Mon Sep 17 00:00:00 2001 From: Tom Brien Date: Mon, 2 Nov 2020 17:21:03 +0000 Subject: [PATCH] Allow hiding complications when watch is locked (#1253) * Allow private complications * Add string * Add british string, tea and crumpets and what not * Move row * Probably want to save setting... * No idea when this moved... * Fix logic * Tweak migration Forgot to commit this earlier * Bump schema * Flip logic * Add helper * Change func name * Use helper in template func * Update function name * Tidy up Co-authored-by: Zac West <74188+zacwest@users.noreply.github.com> --- .../Resources/en-GB.lproj/Localizable.strings | 1 + .../Resources/en.lproj/Localizable.strings | 1 + .../ComplicationEditViewController.swift | 10 +++++++++ .../Watch/ComplicationController.swift | 21 ++++++++++++++++--- .../Shared/API/Models/WatchComplication.swift | 4 ++++ .../Extensions/Realm+Initialization.swift | 10 ++++++++- .../Shared/Resources/Swiftgen/Strings.swift | 4 ++++ 7 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Sources/App/Resources/en-GB.lproj/Localizable.strings b/Sources/App/Resources/en-GB.lproj/Localizable.strings index 7604ceb64..b3cc72c83 100644 --- a/Sources/App/Resources/en-GB.lproj/Localizable.strings +++ b/Sources/App/Resources/en-GB.lproj/Localizable.strings @@ -902,3 +902,4 @@ Tags will work on any device with Home Assistant installed which has hardware su "watch.configurator.list.description" = "Configure a new Complication using the Add button. Once saved, you can choose it on your Apple Watch or in the Watch app."; "watch.configurator.list.learn_more" = "Learn More"; "watch.configurator.rows.display_name.title" = "Display Name"; +"watch.configurator.rows.is_public.title" = "Show When Locked"; diff --git a/Sources/App/Resources/en.lproj/Localizable.strings b/Sources/App/Resources/en.lproj/Localizable.strings index 33ab069c5..6c06908b4 100644 --- a/Sources/App/Resources/en.lproj/Localizable.strings +++ b/Sources/App/Resources/en.lproj/Localizable.strings @@ -902,3 +902,4 @@ Tags will work on any device with Home Assistant installed which has hardware su "watch.configurator.list.description" = "Configure a new Complication using the Add button. Once saved, you can choose it on your Apple Watch or in the Watch app."; "watch.configurator.list.learn_more" = "Learn More"; "watch.configurator.rows.display_name.title" = "Display Name"; +"watch.configurator.rows.is_public.title" = "Show When Locked"; diff --git a/Sources/App/Settings/AppleWatch/ComplicationEditViewController.swift b/Sources/App/Settings/AppleWatch/ComplicationEditViewController.swift index a4564cb91..f858be579 100644 --- a/Sources/App/Settings/AppleWatch/ComplicationEditViewController.swift +++ b/Sources/App/Settings/AppleWatch/ComplicationEditViewController.swift @@ -53,6 +53,11 @@ class ComplicationEditViewController: FormViewController, TypedRowControllerType } else { config.name = nil } + if let IsPublic = (form.rowBy(tag: "IsPublic") as? SwitchRow)?.value { + config.IsPublic = IsPublic + } else { + config.IsPublic = true + } config.Template = displayTemplate config.Data = getValuesGroupedBySection() @@ -184,6 +189,11 @@ class ComplicationEditViewController: FormViewController, TypedRowControllerType cell.detailTextLabel?.text = row.value?.style } + <<< SwitchRow("IsPublic") { + $0.title = L10n.Watch.Configurator.Rows.IsPublic.title + $0.value = self.config.IsPublic + } + self.form.append(contentsOf: textSections) self.form diff --git a/Sources/Extensions/Watch/ComplicationController.swift b/Sources/Extensions/Watch/ComplicationController.swift index 3384330e7..96040a988 100644 --- a/Sources/Extensions/Watch/ComplicationController.swift +++ b/Sources/Extensions/Watch/ComplicationController.swift @@ -16,8 +16,8 @@ class ComplicationController: NSObject, CLKComplicationDataSource { // https://github.com/LoopKit/Loop/issues/816 // https://crunchybagel.com/detecting-which-complication-was-tapped/ - private func template(for complication: CLKComplication) -> CLKComplicationTemplate? { - Iconic.registerMaterialDesignIcons() + private func complicationModel(for complication: CLKComplication) -> WatchComplication? { + // Helper function to get a complication using the correct ID depending on watchOS version let model: WatchComplication? @@ -32,6 +32,14 @@ class ComplicationController: NSObject, CLKComplicationDataSource { model = Current.realm().object(ofType: WatchComplication.self, forPrimaryKey: matchedFamily.rawValue) } + return model + } + + private func template(for complication: CLKComplication) -> CLKComplicationTemplate? { + Iconic.registerMaterialDesignIcons() + + let model = complicationModel(for: complication) + return model?.CLKComplicationTemplate(family: complication.family) } @@ -41,7 +49,14 @@ class ComplicationController: NSObject, CLKComplicationDataSource { for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void ) { - handler(.showOnLockScreen) + + let model = complicationModel(for: complication) + + if model?.IsPublic == false { + handler(.hideOnLockScreen) + } else { + handler(.showOnLockScreen) + } } // MARK: - Timeline Population diff --git a/Sources/Shared/API/Models/WatchComplication.swift b/Sources/Shared/API/Models/WatchComplication.swift index 0bb285e94..bb0e98f4b 100644 --- a/Sources/Shared/API/Models/WatchComplication.swift +++ b/Sources/Shared/API/Models/WatchComplication.swift @@ -75,6 +75,8 @@ public class WatchComplication: Object, ImmutableMappable { name ?? Template.style } + @objc dynamic public var IsPublic: Bool = true + override public static func primaryKey() -> String? { return "identifier" } @@ -96,6 +98,7 @@ public class WatchComplication: Object, ImmutableMappable { self.Family = try map.value("Family") self.identifier = try map.value("identifier") self.name = try map.value("name") + self.IsPublic = try map.value("IsPublic") } public func mapping(map: Map) { @@ -105,6 +108,7 @@ public class WatchComplication: Object, ImmutableMappable { Family >>> map["Family"] identifier >>> map["identifier"] name >>> map["name"] + IsPublic >>> map["IsPublic"] } enum RenderedValueType: Hashable { diff --git a/Sources/Shared/Common/Extensions/Realm+Initialization.swift b/Sources/Shared/Common/Extensions/Realm+Initialization.swift index 397f60db4..eb16455d0 100644 --- a/Sources/Shared/Common/Extensions/Realm+Initialization.swift +++ b/Sources/Shared/Common/Extensions/Realm+Initialization.swift @@ -75,9 +75,11 @@ extension Realm { // 10 - 2020-07-31 v2020.5 (added isServerControlled to Action) // 11 - 2020-08-12 v2020.5.2 (cleaning up duplicate NotificationCategory identifiers) // 12 - 2020-08-16 v2020.6 (mdi upgrade/migration to 5.x) + // 13 - 2020-10-17 v2020.7 (allow multiple complications) + // 14 - 2020-10-29 v2020.8 (complication privacy) let config = Realm.Configuration( fileURL: storeURL, - schemaVersion: 13, + schemaVersion: 14, migrationBlock: { migration, oldVersion in Current.Log.info("migrating from \(oldVersion)") if oldVersion < 9 { @@ -145,6 +147,12 @@ extension Realm { newObject!["identifier"] = newObject!["rawFamily"] } } + + if oldVersion < 14 { + migration.enumerateObjects(ofType: WatchComplication.className()) { _, newObject in + newObject?["IsPublic"] = true + } + } }, deleteRealmIfMigrationNeeded: false ) diff --git a/Sources/Shared/Resources/Swiftgen/Strings.swift b/Sources/Shared/Resources/Swiftgen/Strings.swift index a9d29528a..b703b1368 100644 --- a/Sources/Shared/Resources/Swiftgen/Strings.swift +++ b/Sources/Shared/Resources/Swiftgen/Strings.swift @@ -2315,6 +2315,10 @@ public enum L10n { public static var title: String { return L10n.tr("Localizable", "watch.configurator.rows.icon.color.title") } } } + public enum IsPublic { + /// Show When Locked + public static var title: String { return L10n.tr("Localizable", "watch.configurator.rows.is_public.title") } + } public enum Ring { /// Ring public static var title: String { return L10n.tr("Localizable", "watch.configurator.rows.ring.title") }