Skip to content

Commit e88c4cd

Browse files
Cache data added (not tested completely)
1 parent eb9d559 commit e88c4cd

File tree

2 files changed

+89
-35
lines changed

2 files changed

+89
-35
lines changed

Shared/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftUICharts
1212
import Reachability
1313

1414
struct ContentView: View {
15-
let abbreviations = ["BTC", "BNB", "DOGE", "XRP"].sorted()
15+
let abbreviations = ["BNB", "BTC", "DOGE", "XRP"].sorted()
1616
@State var cryptocurrencies: [Cryptocurrency] = []
1717

1818
var body: some View {

Shared/HomeView.swift

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -246,53 +246,61 @@ struct HomeView: View {
246246
let dummy = Cryptocurrency(symbol: "", name: "", history: [], abbreviation: "")
247247
cryptocurrencies.append(dummy)
248248
cryptocurrencies.removeLast()
249+
250+
for cryptocurrency in cryptocurrencies {
251+
writeData(cryptocurrency)
252+
}
249253
}
250254

251255
func getData() {
252256
isSyncing = true
253257

254258
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()
265260
} 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)!
267268

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)
270284

285+
for abbreviation in abbreviations {
286+
let _ = print(4)
287+
readData(abbreviation)
288+
}
289+
} catch {
290+
// directory unavailable
271291
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
276294
} 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
285296
}
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)
295297

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)
296304
}
297305

298306
func getCryptocurrencyData(_ abbreviation: String, _ apiKey: String) {
@@ -340,6 +348,8 @@ struct HomeView: View {
340348
let cryptocurrency = Cryptocurrency(symbol: meta["symbol"]!, name: meta["currency_base"]!, history: history, abbreviation: abbreviation)
341349
cryptocurrencies.append(cryptocurrency)
342350
cryptocurrencies = sortCryptocurrencyByName(cryptocurrencies)
351+
let _ = print(1)
352+
writeData(cryptocurrency)
343353
} else {
344354
cryptocurrency!.history = history
345355
doDummyOnCryptocurrencies()
@@ -353,6 +363,50 @@ struct HomeView: View {
353363
}
354364
}
355365

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+
356410
func sortCryptocurrencyByName(_ array: [Cryptocurrency]) -> [Cryptocurrency] {
357411
array.sorted(by: { $0.name < $1.name })
358412
}

0 commit comments

Comments
 (0)