@@ -246,53 +246,61 @@ struct HomeView: View {
246
246
let dummy = Cryptocurrency ( symbol: " " , name: " " , history: [ ] , abbreviation: " " )
247
247
cryptocurrencies. append ( dummy)
248
248
cryptocurrencies. removeLast ( )
249
+
250
+ for cryptocurrency in cryptocurrencies {
251
+ writeData ( cryptocurrency)
252
+ }
249
253
}
250
254
251
255
func getData( ) {
252
256
isSyncing = true
253
257
254
258
if isNetworkAvailable ( ) {
255
- if let path = Bundle . main. path ( forResource: " API Keys " , ofType: " plist " ) {
256
- let keys = NSDictionary ( contentsOfFile: path) !
257
-
258
- for abbreviation in abbreviations {
259
- getCryptocurrencyData ( abbreviation, ( keys [ " twelvedataAPIKey " ] as? String ) !)
260
- }
261
- // write data
262
- } else {
263
- unknownErrorAlert = true
264
- }
259
+ doNetworkAvailable ( )
265
260
} else {
266
- unavailableNetworkAlert = true
261
+ doNetworkUnavailable ( )
262
+ }
263
+ }
264
+
265
+ func doNetworkAvailable( ) {
266
+ if let path = Bundle . main. path ( forResource: " API Keys " , ofType: " plist " ) {
267
+ let keys = NSDictionary ( contentsOfFile: path) !
267
268
268
- let projectDirectory = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) [ 0 ]
269
- let directoryName = projectDirectory. appendingPathComponent ( " cryptocurrencies " , isDirectory: true )
269
+ for abbreviation in abbreviations {
270
+ getCryptocurrencyData ( abbreviation, ( keys [ " twelvedataAPIKey " ] as? String ) !)
271
+ }
272
+ } else {
273
+ unknownErrorAlert = true
274
+ }
275
+ }
276
+
277
+ func doNetworkUnavailable( ) {
278
+ unavailableNetworkAlert = true
279
+ let directoryName = getProjectDirectory ( )
280
+
281
+ do {
282
+ // directory available
283
+ try FileManager . default. createDirectory ( at: directoryName, withIntermediateDirectories: true )
270
284
285
+ for abbreviation in abbreviations {
286
+ let _ = print ( 4 )
287
+ readData ( abbreviation)
288
+ }
289
+ } catch {
290
+ // directory unavailable
271
291
do {
272
- // directory available
273
- try FileManager . default. createDirectory ( at: directoryName, withIntermediateDirectories: true )
274
-
275
- // read data
292
+ try FileManager . default. removeItem ( at: directoryName)
293
+ unavailableDataAlert = true
276
294
} catch {
277
- // directory unavailable
278
- do {
279
- try FileManager . default. removeItem ( at: directoryName)
280
- unavailableDataAlert = true
281
- } catch {
282
- unknownErrorAlert = true
283
- }
284
-
295
+ unknownErrorAlert = true
285
296
}
286
- }
287
-
288
- // write data in file
289
- // let cryptocurrency: Cryptocurrency = Cryptocurrency(symbol: "a", name: "A", history: [])
290
- // let jsonEncoder = JSONEncoder()
291
- // let jsonData = try jsonEncoder.encode(cryptocurrency)
292
- // let json = String(data: jsonData, encoding: String.Encoding.utf16)
293
- // let cryptocurrencyDir = directoryName.appendingPathComponent("Bitcoin.txt", isDirectory: true)
294
- // try json?.write(to: cryptocurrencyDir, atomically: true, encoding: String.Encoding.utf16)
295
297
298
+ }
299
+ }
300
+
301
+ func getProjectDirectory( ) -> URL {
302
+ let projectDirectory = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) [ 0 ]
303
+ return projectDirectory. appendingPathComponent ( " cryptocurrencies " , isDirectory: true )
296
304
}
297
305
298
306
func getCryptocurrencyData( _ abbreviation: String , _ apiKey: String ) {
@@ -340,6 +348,8 @@ struct HomeView: View {
340
348
let cryptocurrency = Cryptocurrency ( symbol: meta [ " symbol " ] !, name: meta [ " currency_base " ] !, history: history, abbreviation: abbreviation)
341
349
cryptocurrencies. append ( cryptocurrency)
342
350
cryptocurrencies = sortCryptocurrencyByName ( cryptocurrencies)
351
+ let _ = print ( 1 )
352
+ writeData ( cryptocurrency)
343
353
} else {
344
354
cryptocurrency!. history = history
345
355
doDummyOnCryptocurrencies ( )
@@ -353,6 +363,50 @@ struct HomeView: View {
353
363
}
354
364
}
355
365
366
+ func writeData( _ cryptocurrency: Cryptocurrency ) {
367
+ do {
368
+ let _ = print ( 2 )
369
+ let directoryName = getProjectDirectory ( )
370
+
371
+ let jsonEncoder = JSONEncoder ( )
372
+ let jsonData = try jsonEncoder. encode ( cryptocurrency)
373
+ let json = String ( data: jsonData, encoding: . utf8)
374
+ let cryptocurrencyDir = directoryName. appendingPathComponent ( " \( cryptocurrency. abbreviation) .txt " , isDirectory: true )
375
+ try json? . write ( to: cryptocurrencyDir, atomically: true , encoding: String . Encoding. utf16)
376
+ } catch {
377
+ let _ = print ( 3 )
378
+ unknownErrorAlert = true
379
+ }
380
+ }
381
+
382
+ func readData( _ cryptocurrencyAbbreviation: String ) {
383
+ do {
384
+ let _ = print ( 5 )
385
+ let directoryName = getProjectDirectory ( )
386
+ let cryptocurrencyDir = directoryName. appendingPathComponent ( " \( cryptocurrencyAbbreviation) .txt " , isDirectory: true )
387
+
388
+ let json = try String ( contentsOf: cryptocurrencyDir, encoding: . utf16)
389
+
390
+ let jsonDecoder = JSONDecoder ( )
391
+ let cryptocurrency = try jsonDecoder. decode ( Cryptocurrency . self, from: json. data ( using: . utf8) !)
392
+
393
+ let getCryptocurrency = getCryptocurrency ( cryptocurrencyAbbreviation)
394
+ if getCryptocurrency == nil {
395
+ let _ = print ( 7 )
396
+ cryptocurrencies. append ( cryptocurrency)
397
+ cryptocurrencies = sortCryptocurrencyByName ( cryptocurrencies)
398
+ } else {
399
+ let _ = print ( 8 )
400
+ getCryptocurrency!. history = cryptocurrency. history
401
+ getCryptocurrency!. price = cryptocurrency. price
402
+ doDummyOnCryptocurrencies ( )
403
+ }
404
+ } catch {
405
+ let _ = print ( 6 )
406
+ unknownErrorAlert = true
407
+ }
408
+ }
409
+
356
410
func sortCryptocurrencyByName( _ array: [ Cryptocurrency ] ) -> [ Cryptocurrency ] {
357
411
array. sorted ( by: { $0. name < $1. name } )
358
412
}
0 commit comments