SettingsMigrationManager.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // LoopFollow
  2. // SettingsMigrationManager.swift
  3. import Foundation
  4. class SettingsMigrationManager {
  5. // MARK: - Current Version
  6. static let currentVersion = "1.0"
  7. // MARK: - Migration Methods
  8. static func migrateSettings(_ data: Data) -> CombinedSettingsExport? {
  9. // Try to decode with the current version
  10. do {
  11. let currentSettings = try JSONDecoder().decode(CombinedSettingsExport.self, from: data)
  12. print("✅ Successfully decoded CombinedSettingsExport")
  13. return currentSettings
  14. } catch {
  15. print("❌ Failed to decode CombinedSettingsExport: \(error)")
  16. print("❌ Error details: \(error.localizedDescription)")
  17. // Try to decode as individual components
  18. return tryDecodeIndividualComponents(data)
  19. }
  20. }
  21. private static func tryDecodeIndividualComponents(_ data: Data) -> CombinedSettingsExport? {
  22. // Try to decode as AlarmSettingsExport
  23. if let alarmSettings = try? JSONDecoder().decode(AlarmSettingsExport.self, from: data) {
  24. print("✅ Successfully decoded as AlarmSettingsExport")
  25. return CombinedSettingsExport(
  26. alarms: alarmSettings,
  27. exportType: "Alarm Settings"
  28. )
  29. }
  30. // Try to decode as NightscoutSettingsExport
  31. if let nightscoutSettings = try? JSONDecoder().decode(NightscoutSettingsExport.self, from: data) {
  32. print("✅ Successfully decoded as NightscoutSettingsExport")
  33. return CombinedSettingsExport(
  34. nightscout: nightscoutSettings,
  35. exportType: "Nightscout Settings"
  36. )
  37. }
  38. // Try to decode as DexcomSettingsExport
  39. if let dexcomSettings = try? JSONDecoder().decode(DexcomSettingsExport.self, from: data) {
  40. print("✅ Successfully decoded as DexcomSettingsExport")
  41. return CombinedSettingsExport(
  42. dexcom: dexcomSettings,
  43. exportType: "Dexcom Settings"
  44. )
  45. }
  46. // Try to decode as RemoteSettingsExport
  47. if let remoteSettings = try? JSONDecoder().decode(RemoteSettingsExport.self, from: data) {
  48. print("✅ Successfully decoded as RemoteSettingsExport")
  49. return CombinedSettingsExport(
  50. remote: remoteSettings,
  51. exportType: "Remote Settings"
  52. )
  53. }
  54. print("❌ Failed to decode as any known component")
  55. return nil
  56. }
  57. // MARK: - Version Compatibility
  58. static func isCompatibleVersion(_ version: String) -> Bool {
  59. let currentVersionComponents = currentVersion.split(separator: ".").compactMap { Int($0) }
  60. let importVersionComponents = version.split(separator: ".").compactMap { Int($0) }
  61. // For now, accept any version (can be made more strict later)
  62. return true
  63. }
  64. static func getCompatibilityMessage(for version: String) -> String {
  65. return "Settings from version \(version) may not be fully compatible with current version \(currentVersion). Some features may not work as expected."
  66. }
  67. // MARK: - Error Handling
  68. enum SettingsImportError: Error {
  69. case unsupportedVersion(String)
  70. case migrationFailed(String)
  71. case corruptedData
  72. case incompatibleAlarmFormat
  73. case unknownError
  74. }
  75. }