fix: resolve Swift phase-1 init errors in WorkoutViewModel
Using self.programs on the RHS of activeSections assignment violated Swift's two-phase init rule (self is partially initialized). Use local `migrated` array instead, then assign programs and activeSections in the correct order.
This commit is contained in:
@@ -190,13 +190,14 @@ class WorkoutViewModel: ObservableObject {
|
||||
let saved = try? JSONDecoder().decode([WorkoutProgram].self, from: data),
|
||||
!saved.isEmpty {
|
||||
|
||||
let gamifiedEmojis: Set<String> = ["🔥", "💥"]
|
||||
let emojiMap = ["🔥": "🔄", "💥": "🏋️"]
|
||||
let needsMigration = saved.contains(where: { gamifiedEmojis.contains($0.emoji) })
|
||||
programs = needsMigration ? saved.map { p in
|
||||
guard let replacement = emojiMap[p.emoji] else { return p }
|
||||
var copy = p; copy.emoji = replacement; return copy
|
||||
} : saved
|
||||
let needsMigration = saved.contains(where: { emojiMap[$0.emoji] != nil })
|
||||
let migrated: [WorkoutProgram] = needsMigration
|
||||
? saved.map { p in
|
||||
guard let rep = emojiMap[p.emoji] else { return p }
|
||||
var copy = p; copy.emoji = rep; return copy
|
||||
}
|
||||
: saved
|
||||
|
||||
var sched: [Int: UUID] = [:]
|
||||
if let dict = UserDefaults.standard.dictionary(forKey: Self.kSchedule) as? [String: String] {
|
||||
@@ -211,7 +212,9 @@ class WorkoutViewModel: ObservableObject {
|
||||
let uid = UUID(uuidString: str),
|
||||
saved.contains(where: { $0.id == uid }) { pid = uid }
|
||||
activeProgramId = pid
|
||||
activeSections = programs.first { $0.id == pid }?.sections ?? programs[0].sections
|
||||
// Use local `migrated` (not self.programs) — self is partially initialized here
|
||||
programs = migrated
|
||||
activeSections = migrated.first { $0.id == pid }?.sections ?? migrated[0].sections
|
||||
|
||||
if needsMigration { save() }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user