Skip to content

Commit d5c6fc3

Browse files
Copilotbgoncal
andcommitted
Address code review feedback: fix force unwrap, improve filtering and initialization
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
1 parent a521312 commit d5c6fc3

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

Sources/App/Settings/AppleWatch/ComplicationFamilySelectViewController.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class ComplicationFamilySelectViewController: HAFormViewController, RowControlle
6363
}
6464

6565
$0.presentationMode = .show(controllerProvider: .callback { [allowMultiple] in
66-
var complication = WatchComplicationGRDB()
67-
complication.Family = family
68-
69-
if !allowMultiple {
70-
// if the user hasn't upgraded to watchOS 7 yet, we want to preserve our migration behavior
71-
// so any watchOS 6-created complications have a predicable globally-unique identifier
72-
complication.identifier = family.rawValue
73-
}
66+
let identifier = allowMultiple ? UUID().uuidString : family.rawValue
67+
// Initialize with proper family and template to avoid inconsistent state
68+
let template = family.templates.first ?? .ModularSmallSimpleText
69+
var complication = WatchComplicationGRDB(
70+
identifier: identifier,
71+
rawFamily: family.rawValue,
72+
rawTemplate: template.rawValue
73+
)
7474

7575
return ComplicationEditViewController(config: complication)
7676
}, onDismiss: { [weak self] vc in

Sources/Shared/API/Models/WatchComplicationGRDB+Queries.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ extension WatchComplicationGRDB {
7070

7171
try Current.database().write { db in
7272
// Find complications with serverIdentifiers that don't match any current servers
73-
// This includes complications with nil serverIdentifier or identifiers not in the current server list
74-
let serverIdColumn = Column(DatabaseTables.WatchComplication.serverIdentifier.rawValue)
75-
let orphanedComplications = try WatchComplicationGRDB
76-
.filter(serverIdColumn == nil || !serverIdentifiers.contains(serverIdColumn))
77-
.fetchAll(db)
73+
// Fetch all complications and filter in Swift for clarity
74+
let allComplications = try WatchComplicationGRDB.fetchAll(db)
75+
let orphanedComplications = allComplications.filter { complication in
76+
guard let serverId = complication.serverIdentifier else {
77+
return true // nil serverIdentifier is orphaned
78+
}
79+
return !serverIdentifiers.contains(serverId)
80+
}
7881

7982
if !orphanedComplications.isEmpty {
8083
Current.Log.info("Migrating \(orphanedComplications.count) orphaned watch complications to \(replacementServer.identifier)")

Sources/Shared/API/Models/WatchComplicationGRDB.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public struct WatchComplicationGRDB: Codable, FetchableRecord, PersistableRecord
3838
if let t = ComplicationTemplate(rawValue: rawTemplate) {
3939
return t
4040
}
41-
return Family.templates.first!
41+
// Fallback to first template for the family, or modularSmall template if family has no templates
42+
return Family.templates.first ?? ComplicationGroupMember.modularSmall.templates.first ?? .ModularSmallSimpleText
4243
}
4344
set {
4445
rawTemplate = newValue.rawValue

Sources/Shared/Database/Tables/WatchComplicationTable.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ final class WatchComplicationTable: DatabaseTableProtocol {
3838
Current.Log.info("Migrating \(realmComplications.count) watch complications from Realm to GRDB")
3939

4040
try database.write { db in
41-
for realmComplication in realmComplications {
42-
let grdbComplication = WatchComplicationGRDB(from: realmComplication)
43-
try grdbComplication.insert(db)
44-
}
41+
// Convert Realm complications to GRDB complications
42+
let grdbComplications = realmComplications.map { WatchComplicationGRDB(from: $0) }
43+
// Batch insert for better performance
44+
try grdbComplications.forEach { try $0.insert(db) }
4545
}
4646

4747
Current.Log.info("Successfully migrated \(realmComplications.count) watch complications to GRDB")

0 commit comments

Comments
 (0)