2021-11-03 18:21:22 +00:00
|
|
|
//
|
|
|
|
// TokiJSONLoader.swift
|
|
|
|
// TokiJSONLoader
|
|
|
|
//
|
|
|
|
// Created by Avery Ada Pace on 10/1/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
class TokiJSONLoader: ObservableObject {
|
|
|
|
|
2021-11-04 18:10:22 +00:00
|
|
|
func loadDictionary() -> [TokiDictEntry]? {
|
2021-11-03 18:21:22 +00:00
|
|
|
let jsonData = loadJSON("toki-dictionary")
|
|
|
|
do {
|
|
|
|
let decodedData = try JSONDecoder().decode([TokiDictEntry].self, from: jsonData!)
|
2021-11-04 18:10:22 +00:00
|
|
|
return decodedData
|
2021-11-03 18:21:22 +00:00
|
|
|
} catch {
|
|
|
|
print("Decode error: \(error)")
|
2021-11-04 18:10:22 +00:00
|
|
|
return nil
|
2021-11-03 18:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:10:22 +00:00
|
|
|
func loadPOS() -> [TokiPartOfSpeech]? {
|
2021-11-03 18:21:22 +00:00
|
|
|
let jsonData = loadJSON("toki-partsofspeech")
|
|
|
|
do {
|
|
|
|
let decodedData = try JSONDecoder().decode([TokiPartOfSpeech].self, from: jsonData!)
|
2021-11-04 18:10:22 +00:00
|
|
|
return decodedData
|
2021-11-03 18:21:22 +00:00
|
|
|
} catch {
|
|
|
|
print("Decode error: \(error)")
|
2021-11-04 18:10:22 +00:00
|
|
|
return nil
|
2021-11-03 18:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:24:46 +00:00
|
|
|
func loadLessons() -> [TokiLesson]? {
|
|
|
|
let jsonData = loadJSON("toki-lessons")
|
|
|
|
do {
|
|
|
|
let decodedData = try JSONDecoder().decode([TokiLesson].self, from: jsonData!)
|
|
|
|
return decodedData
|
|
|
|
} catch {
|
|
|
|
print("Decode error: \(error)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:21:22 +00:00
|
|
|
func loadJSON(_ resource: String) -> Data? {
|
|
|
|
do {
|
|
|
|
if let bundlePath = Bundle.main.path(forResource: resource, ofType: "json"), let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
|
|
|
|
return jsonData
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
print(error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|