@@ -14,8 +14,9 @@ struct HomeView: View {
14
14
let abbreviations : [ String ]
15
15
16
16
@Binding var cryptocurrencies : [ Cryptocurrency ]
17
+ @Binding var userMoney : Double
18
+ @Binding var unknownErrorAlert : Bool
17
19
18
- @State var unknownErrorAlert : Bool = false
19
20
@State var isSyncing : Bool = false
20
21
@State var favoriteCryptocurrencies : [ Cryptocurrency ] = [ ]
21
22
@@ -149,7 +150,7 @@ struct HomeView: View {
149
150
let button = getStarButton ( cryptocurrency: cryptocurrency, imageName: " star.fill " ) {
150
151
removeFromFavoriteCryptocarrencies ( cryptocurrency)
151
152
cryptocurrency. isFavorite = false
152
- doDummyOnCryptocurrencies ( )
153
+ doDummyOnCryptocurrencies ( cryptocurrencies : $cryptocurrencies , userMoney : $userMoney , unknownErrorAlert : $unknownErrorAlert )
153
154
}
154
155
155
156
return BarChartView ( data: ChartData ( values: data) , title: cryptocurrency. name, legend: " Daily " , form: ChartForm . medium, cornerButton: button, valueSpecifier: " %.2f " )
@@ -222,14 +223,14 @@ struct HomeView: View {
222
223
getStarButton ( cryptocurrency: cryptocurrency, imageName: " star.fill " ) {
223
224
cryptocurrency. isFavorite = false
224
225
removeFromFavoriteCryptocarrencies ( cryptocurrency)
225
- doDummyOnCryptocurrencies ( )
226
+ doDummyOnCryptocurrencies ( cryptocurrencies : $cryptocurrencies , userMoney : $userMoney , unknownErrorAlert : $unknownErrorAlert )
226
227
}
227
228
} else {
228
229
getStarButton ( cryptocurrency: cryptocurrency, imageName: " star " ) {
229
230
cryptocurrency. isFavorite = true
230
231
favoriteCryptocurrencies. append ( cryptocurrency)
231
232
favoriteCryptocurrencies = sortCryptocurrencyByName ( favoriteCryptocurrencies)
232
- doDummyOnCryptocurrencies ( )
233
+ doDummyOnCryptocurrencies ( cryptocurrencies : $cryptocurrencies , userMoney : $userMoney , unknownErrorAlert : $unknownErrorAlert )
233
234
}
234
235
}
235
236
}
@@ -242,16 +243,6 @@ struct HomeView: View {
242
243
. coordinateSpace ( name: " " )
243
244
}
244
245
245
- func doDummyOnCryptocurrencies( ) {
246
- let dummy = Cryptocurrency ( symbol: " " , name: " " , history: [ ] , abbreviation: " " )
247
- cryptocurrencies. append ( dummy)
248
- cryptocurrencies. removeLast ( )
249
-
250
- for cryptocurrency in cryptocurrencies {
251
- writeData ( cryptocurrency)
252
- }
253
- }
254
-
255
246
func getData( ) {
256
247
isSyncing = true
257
248
@@ -282,9 +273,7 @@ struct HomeView: View {
282
273
// directory available
283
274
try FileManager . default. createDirectory ( at: directoryName, withIntermediateDirectories: true )
284
275
285
- for abbreviation in abbreviations {
286
- readData ( abbreviation)
287
- }
276
+ readAllData ( )
288
277
} catch {
289
278
// directory unavailable
290
279
do {
@@ -297,11 +286,6 @@ struct HomeView: View {
297
286
}
298
287
}
299
288
300
- func getProjectDirectory( ) -> URL {
301
- let projectDirectory = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) [ 0 ]
302
- return projectDirectory. appendingPathComponent ( " cryptocurrencies " , isDirectory: true )
303
- }
304
-
305
289
func getCryptocurrencyData( _ abbreviation: String , _ apiKey: String ) {
306
290
let request = NSMutableURLRequest ( url: NSURL ( string: " https://api.twelvedata.com/time_series?apikey= \( apiKey) &interval=1day&symbol= \( abbreviation) /USD&dp=2&format=JSON " ) ! as URL ,
307
291
cachePolicy: . useProtocolCachePolicy,
@@ -347,32 +331,34 @@ struct HomeView: View {
347
331
let cryptocurrency = Cryptocurrency ( symbol: meta [ " symbol " ] !, name: meta [ " currency_base " ] !, history: history, abbreviation: abbreviation)
348
332
cryptocurrencies. append ( cryptocurrency)
349
333
cryptocurrencies = sortCryptocurrencyByName ( cryptocurrencies)
350
- writeData ( cryptocurrency)
334
+ writeData ( cryptocurrency: cryptocurrency , unknownErrorAlert : $unknownErrorAlert )
351
335
} else {
352
336
cryptocurrency!. history = history
353
- doDummyOnCryptocurrencies ( )
337
+ doDummyOnCryptocurrencies ( cryptocurrencies : $cryptocurrencies , userMoney : $userMoney , unknownErrorAlert : $unknownErrorAlert )
354
338
}
355
339
356
340
} else {
357
341
runOutOfAPICredits = true
342
+ readAllData ( )
358
343
}
359
344
} else {
360
345
unknownErrorAlert = true
361
346
}
362
347
}
363
348
364
- func writeData( _ cryptocurrency: Cryptocurrency ) {
349
+ func readAllData( ) {
350
+ let directoryName = getProjectDirectory ( )
351
+
365
352
do {
366
- let directoryName = getProjectDirectory ( )
367
-
368
- let jsonEncoder = JSONEncoder ( )
369
- let jsonData = try jsonEncoder. encode ( cryptocurrency)
370
- let json = String ( data: jsonData, encoding: . utf8)
371
- let cryptocurrencyDir = directoryName. appendingPathComponent ( " \( cryptocurrency. abbreviation) .txt " , isDirectory: true )
372
- try json? . write ( to: cryptocurrencyDir, atomically: true , encoding: String . Encoding. utf16)
353
+ let moneyDir = directoryName. appendingPathComponent ( " money.txt " , isDirectory: true )
354
+ userMoney = Double ( try String ( contentsOf: moneyDir, encoding: . utf16) ) !
373
355
} catch {
374
356
unknownErrorAlert = true
375
357
}
358
+
359
+ for abbreviation in abbreviations {
360
+ readData ( abbreviation)
361
+ }
376
362
}
377
363
378
364
func readData( _ cryptocurrencyAbbreviation: String ) {
@@ -392,7 +378,7 @@ struct HomeView: View {
392
378
} else {
393
379
getCryptocurrency!. history = cryptocurrency. history
394
380
getCryptocurrency!. price = cryptocurrency. price
395
- doDummyOnCryptocurrencies ( )
381
+ doDummyOnCryptocurrencies ( cryptocurrencies : $cryptocurrencies , userMoney : $userMoney , unknownErrorAlert : $unknownErrorAlert )
396
382
}
397
383
} catch {
398
384
unknownErrorAlert = true
@@ -418,7 +404,7 @@ struct HomeView: View {
418
404
} else {
419
405
do {
420
406
try updatePrice ( abbreviation, data!)
421
- doDummyOnCryptocurrencies ( )
407
+ doDummyOnCryptocurrencies ( cryptocurrencies : $cryptocurrencies , userMoney : $userMoney , unknownErrorAlert : $unknownErrorAlert )
422
408
} catch {
423
409
unknownErrorAlert = true
424
410
}
@@ -440,6 +426,7 @@ struct HomeView: View {
440
426
}
441
427
} else {
442
428
runOutOfAPICredits = true
429
+ readAllData ( )
443
430
}
444
431
445
432
} else {
0 commit comments