@@ -55,14 +55,14 @@ class AccessoryController: ObservableObject {
5555 }
5656
5757 func save( ) throws {
58- try KeychainController . storeInKeychain ( accessories: self . accessories)
59-
6058 for i in 0 ... ( accessories. count - 1 ) {
6159 if ( accessories [ i] . number == " " )
6260 {
6361 accessories [ i] . number = String ( i + 1 )
6462 }
6563 }
64+
65+ try KeychainController . storeInKeychain ( accessories: self . accessories)
6666 }
6767
6868 func updateWithDecryptedReports( devices: [ FindMyDevice ] ) {
@@ -260,7 +260,7 @@ class AccessoryController: ObservableObject {
260260 case . success( let accountData) :
261261
262262 guard let token = accountData. searchPartyToken,
263- token. isEmpty == false
263+ ! token. isEmpty
264264 else {
265265 completion ( . failure( . searchPartyToken) )
266266 return
@@ -273,52 +273,69 @@ class AccessoryController: ObservableObject {
273273 completion ( . failure( . downloadingReportsFailed) )
274274 case . success( let devices) :
275275 let reports = devices. compactMap ( { $0. reports } ) . flatMap ( { $0 } )
276- self ? . mergeAndSaveReports ( reports: reports)
277-
278- if reports. isEmpty {
279- completion ( . failure( . noReportsFound) )
280- } else {
281- self ? . updateWithDecryptedReports ( devices: devices)
282- completion ( . success( ( ) ) )
276+
277+ // Use mergeAndSaveReports asynchronously
278+ self ? . mergeAndSaveReports ( reports: reports) { error in
279+ if let error = error {
280+ print ( " Failed to merge and save reports: \( error) " )
281+ completion ( . failure( . noReportsFound) )
282+ return
283+ }
284+
285+ if reports. isEmpty {
286+ completion ( . failure( . noReportsFound) )
287+ } else {
288+ self ? . updateWithDecryptedReports ( devices: devices)
289+ completion ( . success( ( ) ) )
290+ }
283291 }
284292 }
285293 }
286294 }
287295 }
288296 }
297+
289298
290- func mergeAndSaveReports( reports: [ FindMyReport ] ) {
299+ func mergeAndSaveReports( reports: [ FindMyReport ] , completion : @escaping ( Error ? ) -> Void ) {
291300 // Get URL of the file to save reports
292301 guard let fileURL = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . first? . appendingPathComponent ( " location_reports.json " ) else {
302+ completion ( NSError ( domain: " FileURLNotFound " , code: 0 , userInfo: nil ) )
293303 return
294304 }
295305 print ( fileURL)
296306
297- var allReports : [ FindMyReport ] = [ ]
298-
299- // Read previously saved reports from file
300- if let data = try ? Data ( contentsOf: fileURL) ,
301- let savedReports = try ? JSONDecoder ( ) . decode ( [ FindMyReport ] . self, from: data) {
302- // Remove duplicates from newly downloaded reports
303- let uniqueReports = reports. filter { newReport in
304- !savedReports. contains { existingReport in
305- return newReport. id == existingReport. id
307+ // Perform decoding asynchronously
308+ DispatchQueue . global ( ) . async {
309+ do {
310+ var allReports : [ FindMyReport ] = [ ]
311+
312+ // Read previously saved reports from file
313+ if let data = try ? Data ( contentsOf: fileURL) ,
314+ let savedReports = try ? JSONDecoder ( ) . decode ( [ FindMyReport ] . self, from: data) {
315+ // Remove duplicates from newly downloaded reports
316+ let uniqueReports = reports. filter { newReport in
317+ !savedReports. contains { existingReport in
318+ return newReport. id == existingReport. id
319+ }
320+ }
321+ // Merge newly downloaded reports with previously saved reports
322+ allReports. append ( contentsOf: savedReports)
323+ allReports. append ( contentsOf: uniqueReports)
324+ } else {
325+ // No previously saved reports, simply add all newly downloaded reports
326+ allReports. append ( contentsOf: reports)
306327 }
307- }
308- // Merge newly downloaded reports with previously saved reports
309- allReports. append ( contentsOf: savedReports)
310- allReports. append ( contentsOf: uniqueReports)
311- } else {
312- // No previously saved reports, simply add all newly downloaded reports
313- allReports. append ( contentsOf: reports)
314- }
315328
316- // Write merged reports back to file
317- do {
318- let jsonData = try JSONEncoder ( ) . encode ( allReports)
319- try jsonData. write ( to: fileURL)
320- } catch {
321- print ( " Failed to write new reports to file " )
329+ // Write merged reports back to file
330+ let jsonData = try JSONEncoder ( ) . encode ( allReports)
331+ try jsonData. write ( to: fileURL)
332+
333+ // Completion
334+ completion ( nil )
335+ } catch {
336+ // Handle error
337+ completion ( error)
338+ }
322339 }
323340 }
324341}
0 commit comments