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),
|
let saved = try? JSONDecoder().decode([WorkoutProgram].self, from: data),
|
||||||
!saved.isEmpty {
|
!saved.isEmpty {
|
||||||
|
|
||||||
let gamifiedEmojis: Set<String> = ["🔥", "💥"]
|
|
||||||
let emojiMap = ["🔥": "🔄", "💥": "🏋️"]
|
let emojiMap = ["🔥": "🔄", "💥": "🏋️"]
|
||||||
let needsMigration = saved.contains(where: { gamifiedEmojis.contains($0.emoji) })
|
let needsMigration = saved.contains(where: { emojiMap[$0.emoji] != nil })
|
||||||
programs = needsMigration ? saved.map { p in
|
let migrated: [WorkoutProgram] = needsMigration
|
||||||
guard let replacement = emojiMap[p.emoji] else { return p }
|
? saved.map { p in
|
||||||
var copy = p; copy.emoji = replacement; return copy
|
guard let rep = emojiMap[p.emoji] else { return p }
|
||||||
} : saved
|
var copy = p; copy.emoji = rep; return copy
|
||||||
|
}
|
||||||
|
: saved
|
||||||
|
|
||||||
var sched: [Int: UUID] = [:]
|
var sched: [Int: UUID] = [:]
|
||||||
if let dict = UserDefaults.standard.dictionary(forKey: Self.kSchedule) as? [String: String] {
|
if let dict = UserDefaults.standard.dictionary(forKey: Self.kSchedule) as? [String: String] {
|
||||||
@@ -211,7 +212,9 @@ class WorkoutViewModel: ObservableObject {
|
|||||||
let uid = UUID(uuidString: str),
|
let uid = UUID(uuidString: str),
|
||||||
saved.contains(where: { $0.id == uid }) { pid = uid }
|
saved.contains(where: { $0.id == uid }) { pid = uid }
|
||||||
activeProgramId = pid
|
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() }
|
if needsMigration { save() }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user