MigrationScript.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import CoreData
  2. import Foundation
  3. // MARK: - Protocol Definition
  4. /// A protocol that ensures a Data Transfer Object (DTO) can be stored in Core Data.
  5. /// It requires a method to map the DTO to its corresponding Core Data managed object.
  6. protocol ImportableDTO: Decodable {
  7. associatedtype ManagedObject: NSManagedObject
  8. /// Converts the DTO into a Core Data managed object.
  9. func store(in context: NSManagedObjectContext) -> ManagedObject
  10. }
  11. // MARK: - JSONImporter Class with Generic Import Function
  12. /// Class responsible for importing JSON data into Core Data.
  13. class JSONImporter {
  14. private let context: NSManagedObjectContext
  15. private let fileManager = FileManager.default
  16. /// Initializes the importer with a Core Data context.
  17. init(context: NSManagedObjectContext) {
  18. self.context = context
  19. }
  20. /// Generic function to import data from a JSON file into Core Data.
  21. /// - Parameters:
  22. /// - userDefaultsKey: Key to check if data has already been imported.
  23. /// - filePathComponent: Path component of the JSON file.
  24. /// - dtoType: The DTO type conforming to `ImportableDTO`.
  25. /// - dateDecodingStrategy: The date decoding strategy for JSON decoding.
  26. // func importDataIfNeeded<T: ImportableDTO>(
  27. // userDefaultsKey: String,
  28. // filePathComponent: String,
  29. // dtoType _: T.Type,
  30. // dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601
  31. // ) async {
  32. // let hasImported = UserDefaults.standard.bool(forKey: userDefaultsKey)
  33. //
  34. // // guard !hasImported else {
  35. // // debugPrint("\(filePathComponent) already imported. Skipping import.")
  36. // // return
  37. // // }
  38. //
  39. // do {
  40. // // Get the file path for the JSON file
  41. // guard let filePath = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
  42. // .first?
  43. // .appendingPathComponent(filePathComponent),
  44. // fileManager.fileExists(atPath: filePath.path)
  45. // else {
  46. // debugPrint("\(filePathComponent) file not found at path \(filePathComponent)")
  47. // return
  48. // }
  49. //
  50. // // Read data from the JSON file
  51. // let data = try Data(contentsOf: filePath)
  52. // let decoder = JSONDecoder()
  53. // decoder.dateDecodingStrategy = dateDecodingStrategy
  54. //
  55. // // Decode the data into an array of DTOs
  56. // let entries = try decoder.decode([T].self, from: data)
  57. //
  58. // // Save the DTOs into Core Data
  59. // await context.perform {
  60. // for entry in entries {
  61. // _ = entry.store(in: self.context)
  62. // }
  63. //
  64. // do {
  65. // guard self.context.hasChanges else { return }
  66. // try self.context.save()
  67. // debugPrint("\(DebuggingIdentifiers.succeeded) \(filePathComponent) successfully imported into Core Data.")
  68. // } catch {
  69. // debugPrint("\(DebuggingIdentifiers.failed) Failed to save \(filePathComponent) to Core Data: \(error)")
  70. // }
  71. // }
  72. //
  73. // // Delete the JSON file after successful import
  74. // try fileManager.removeItem(at: filePath)
  75. // debugPrint("\(filePathComponent) deleted after successful import.")
  76. //
  77. // // Update UserDefaults to indicate that the data has been imported
  78. // UserDefaults.standard.set(true, forKey: userDefaultsKey)
  79. // } catch {
  80. // debugPrint("Error importing \(filePathComponent): \(error)")
  81. // }
  82. // }
  83. func importDataIfNeeded<T: ImportableDTO>(
  84. userDefaultsKey: String,
  85. filePathComponent: String,
  86. dtoType _: T.Type,
  87. dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601
  88. ) async {
  89. let hasImported = UserDefaults.standard.bool(forKey: userDefaultsKey)
  90. // if hasImported {
  91. // debugPrint("\(filePathComponent) already imported. Skipping import.")
  92. // return
  93. // }
  94. do {
  95. guard let filePath = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
  96. .first?
  97. .appendingPathComponent(filePathComponent),
  98. fileManager.fileExists(atPath: filePath.path)
  99. else {
  100. debugPrint("File not found: \(filePathComponent).")
  101. return
  102. }
  103. let data = try Data(contentsOf: filePath)
  104. let decoder = JSONDecoder()
  105. decoder.dateDecodingStrategy = dateDecodingStrategy
  106. var entries: [T] = []
  107. do {
  108. if let array = try? decoder.decode([T].self, from: data) {
  109. debugPrint("Decoded \(array.count) entries as an array.")
  110. entries = array
  111. } else if let singleObject = try? decoder.decode(T.self, from: data) {
  112. debugPrint("Decoded a single object.")
  113. entries = [singleObject]
  114. } else {
  115. debugPrint("Failed to decode \(filePathComponent) as either an array or a single object.")
  116. return
  117. }
  118. }
  119. await context.perform {
  120. for entry in entries {
  121. _ = entry.store(in: self.context)
  122. }
  123. do {
  124. guard self.context.hasChanges else {
  125. return
  126. }
  127. try self.context.save()
  128. debugPrint("\(DebuggingIdentifiers.succeeded) \(filePathComponent) successfully imported into Core Data.")
  129. } catch {
  130. debugPrint("\(DebuggingIdentifiers.failed) Failed to save \(filePathComponent) to Core Data: \(error)")
  131. }
  132. }
  133. try fileManager.removeItem(at: filePath)
  134. debugPrint("\(filePathComponent) deleted after successful import.")
  135. UserDefaults.standard.set(true, forKey: userDefaultsKey)
  136. } catch {
  137. debugPrint("Error importing \(filePathComponent): \(error)")
  138. }
  139. }
  140. }
  141. // MARK: - Extension for Specific Import Functions
  142. extension JSONImporter {
  143. func importPumpHistoryIfNeeded() async {
  144. await importDataIfNeeded(
  145. userDefaultsKey: "pumpHistoryImported",
  146. filePathComponent: OpenAPS.Monitor.pumpHistory,
  147. dtoType: PumpEventDTO.self,
  148. dateDecodingStrategy: .iso8601
  149. )
  150. }
  151. func importCarbHistoryIfNeeded() async {
  152. await importDataIfNeeded(
  153. userDefaultsKey: "carbHistoryImported",
  154. filePathComponent: OpenAPS.Monitor.carbHistory,
  155. dtoType: CarbEntryDTO.self,
  156. dateDecodingStrategy: .iso8601
  157. )
  158. }
  159. func importGlucoseHistoryIfNeeded() async {
  160. await importDataIfNeeded(
  161. userDefaultsKey: "glucoseHistoryImported",
  162. filePathComponent: OpenAPS.Monitor.glucose,
  163. dtoType: GlucoseEntryDTO.self,
  164. dateDecodingStrategy: .iso8601
  165. )
  166. }
  167. func importDeterminationHistoryIfNeeded() async {
  168. await importDataIfNeeded(
  169. userDefaultsKey: "enactedHistoryImported",
  170. filePathComponent: OpenAPS.Enact.enacted,
  171. dtoType: Determination2.self,
  172. dateDecodingStrategy: .iso8601
  173. )
  174. }
  175. }