Skip to content

Commit

Permalink
Allow hiding complications when watch is locked (#1253)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
TomBrien and zacwest committed Nov 2, 2020
1 parent 148e24b commit 55d472c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions Sources/App/Resources/en-GB.lproj/Localizable.strings
Expand Up @@ -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";
1 change: 1 addition & 0 deletions Sources/App/Resources/en.lproj/Localizable.strings
Expand Up @@ -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";
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions Sources/Extensions/Watch/ComplicationController.swift
Expand Up @@ -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?

Expand All @@ -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)
}

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Sources/Shared/API/Models/WatchComplication.swift
Expand Up @@ -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"
}
Expand All @@ -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) {
Expand All @@ -105,6 +108,7 @@ public class WatchComplication: Object, ImmutableMappable {
Family >>> map["Family"]
identifier >>> map["identifier"]
name >>> map["name"]
IsPublic >>> map["IsPublic"]
}

enum RenderedValueType: Hashable {
Expand Down
10 changes: 9 additions & 1 deletion Sources/Shared/Common/Extensions/Realm+Initialization.swift
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)
Expand Down
4 changes: 4 additions & 0 deletions Sources/Shared/Resources/Swiftgen/Strings.swift
Expand Up @@ -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") }
Expand Down

0 comments on commit 55d472c

Please sign in to comment.