fix: migrate persisted gamified emojis on load
UserDefaults retained the old 🔥/💥 emojis from before the sample data was cleaned. On init, replace those emojis in loaded programs and persist the result so they don't reappear after restart.
This commit is contained in:
@@ -190,7 +190,13 @@ 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 {
|
||||||
|
|
||||||
programs = saved
|
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
|
||||||
|
|
||||||
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] {
|
||||||
@@ -205,7 +211,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 = saved.first { $0.id == pid }?.sections ?? saved[0].sections
|
activeSections = programs.first { $0.id == pid }?.sections ?? programs[0].sections
|
||||||
|
|
||||||
|
if needsMigration { save() }
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// ── First launch: seed with sample programs ──
|
// ── First launch: seed with sample programs ──
|
||||||
|
|||||||
Reference in New Issue
Block a user