Rough dictionary list working

This commit is contained in:
Avery Pace 2021-11-03 14:21:22 -04:00
parent 4f342ce48d
commit 731a71904a
10 changed files with 1980 additions and 90 deletions

View File

@ -7,6 +7,12 @@
objects = {
/* Begin PBXBuildFile section */
7E2811172733027F0063DC78 /* TokiDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E2811142733027F0063DC78 /* TokiDictionary.swift */; };
7E2811182733027F0063DC78 /* TokiJSONLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E2811152733027F0063DC78 /* TokiJSONLoader.swift */; };
7E2811192733027F0063DC78 /* TokiPartOfSpeech.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E2811162733027F0063DC78 /* TokiPartOfSpeech.swift */; };
7E28111C273302860063DC78 /* toki-partsofspeech.json in Resources */ = {isa = PBXBuildFile; fileRef = 7E28111A273302860063DC78 /* toki-partsofspeech.json */; };
7E28111D273302860063DC78 /* toki-dictionary.json in Resources */ = {isa = PBXBuildFile; fileRef = 7E28111B273302860063DC78 /* toki-dictionary.json */; };
7E281120273303220063DC78 /* TokiPartsOfSpeechProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E28111F273303220063DC78 /* TokiPartsOfSpeechProvider.swift */; };
7E943A21273211C200E7DDF4 /* Toki_TrainerApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E943A20273211C200E7DDF4 /* Toki_TrainerApp.swift */; };
7E943A23273211C200E7DDF4 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E943A22273211C200E7DDF4 /* ContentView.swift */; };
7E943A25273211C300E7DDF4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7E943A24273211C300E7DDF4 /* Assets.xcassets */; };
@ -16,6 +22,12 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
7E2811142733027F0063DC78 /* TokiDictionary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokiDictionary.swift; sourceTree = "<group>"; };
7E2811152733027F0063DC78 /* TokiJSONLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokiJSONLoader.swift; sourceTree = "<group>"; };
7E2811162733027F0063DC78 /* TokiPartOfSpeech.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TokiPartOfSpeech.swift; sourceTree = "<group>"; };
7E28111A273302860063DC78 /* toki-partsofspeech.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "toki-partsofspeech.json"; sourceTree = "<group>"; };
7E28111B273302860063DC78 /* toki-dictionary.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "toki-dictionary.json"; sourceTree = "<group>"; };
7E28111F273303220063DC78 /* TokiPartsOfSpeechProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokiPartsOfSpeechProvider.swift; sourceTree = "<group>"; };
7E943A1D273211C200E7DDF4 /* Toki Trainer.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Toki Trainer.app"; sourceTree = BUILT_PRODUCTS_DIR; };
7E943A20273211C200E7DDF4 /* Toki_TrainerApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Toki_TrainerApp.swift; sourceTree = "<group>"; };
7E943A22273211C200E7DDF4 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@ -36,6 +48,42 @@
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
7E281111273302420063DC78 /* Models */ = {
isa = PBXGroup;
children = (
7E2811142733027F0063DC78 /* TokiDictionary.swift */,
7E2811152733027F0063DC78 /* TokiJSONLoader.swift */,
7E2811162733027F0063DC78 /* TokiPartOfSpeech.swift */,
);
path = Models;
sourceTree = "<group>";
};
7E2811122733024F0063DC78 /* Views */ = {
isa = PBXGroup;
children = (
7E943A20273211C200E7DDF4 /* Toki_TrainerApp.swift */,
7E943A22273211C200E7DDF4 /* ContentView.swift */,
);
path = Views;
sourceTree = "<group>";
};
7E281113273302530063DC78 /* States */ = {
isa = PBXGroup;
children = (
7E28111F273303220063DC78 /* TokiPartsOfSpeechProvider.swift */,
);
path = States;
sourceTree = "<group>";
};
7E28111E273302890063DC78 /* JSON Data */ = {
isa = PBXGroup;
children = (
7E28111B273302860063DC78 /* toki-dictionary.json */,
7E28111A273302860063DC78 /* toki-partsofspeech.json */,
);
path = "JSON Data";
sourceTree = "<group>";
};
7E943A14273211C200E7DDF4 = {
isa = PBXGroup;
children = (
@ -55,8 +103,10 @@
7E943A1F273211C200E7DDF4 /* Toki Trainer */ = {
isa = PBXGroup;
children = (
7E943A20273211C200E7DDF4 /* Toki_TrainerApp.swift */,
7E943A22273211C200E7DDF4 /* ContentView.swift */,
7E28111E273302890063DC78 /* JSON Data */,
7E281113273302530063DC78 /* States */,
7E2811122733024F0063DC78 /* Views */,
7E281111273302420063DC78 /* Models */,
7E943A24273211C300E7DDF4 /* Assets.xcassets */,
7E943A29273211C300E7DDF4 /* Persistence.swift */,
7E943A2B273211C300E7DDF4 /* Toki_Trainer.xcdatamodeld */,
@ -133,6 +183,8 @@
files = (
7E943A28273211C300E7DDF4 /* Preview Assets.xcassets in Resources */,
7E943A25273211C300E7DDF4 /* Assets.xcassets in Resources */,
7E28111D273302860063DC78 /* toki-dictionary.json in Resources */,
7E28111C273302860063DC78 /* toki-partsofspeech.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -145,8 +197,12 @@
files = (
7E943A2D273211C300E7DDF4 /* Toki_Trainer.xcdatamodeld in Sources */,
7E943A2A273211C300E7DDF4 /* Persistence.swift in Sources */,
7E2811192733027F0063DC78 /* TokiPartOfSpeech.swift in Sources */,
7E281120273303220063DC78 /* TokiPartsOfSpeechProvider.swift in Sources */,
7E943A23273211C200E7DDF4 /* ContentView.swift in Sources */,
7E2811172733027F0063DC78 /* TokiDictionary.swift in Sources */,
7E943A21273211C200E7DDF4 /* Toki_TrainerApp.swift in Sources */,
7E2811182733027F0063DC78 /* TokiJSONLoader.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -1,88 +0,0 @@
//
// ContentView.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/2/21.
//
import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
animation: .default)
private var items: FetchedResults<Item>
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink {
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
} label: {
Text(item.timestamp!, formatter: itemFormatter)
}
}
.onDelete(perform: deleteItems)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
Text("Select an item")
}
}
private func addItem() {
withAnimation {
let newItem = Item(context: viewContext)
newItem.timestamp = Date()
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
private let itemFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
return formatter
}()
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,46 @@
[
{
"pos": "n",
"definition": "head noun"
},
{
"pos": "mod",
"definition": "modifier (adjective or adverb)"
},
{
"pos": "sep",
"definition": "separator"
},
{
"pos": "vt",
"definition": "verb, transitive (normally used with e)"
},
{
"pos": "vi",
"definition": "verb, intransitive"
},
{
"pos": "interj",
"definition": "interjection"
},
{
"pos": "prep",
"definition": "quasi-preposition"
},
{
"pos": "conj",
"definition": "conjunction"
},
{
"pos": "kama",
"definition": "compound verb preceded by kama"
},
{
"pos": "cont",
"definition": "context word used before la"
},
{
"pos": "oth",
"definition": "special, other word"
}
]

View File

@ -0,0 +1,19 @@
//
// TokiDictionary.swift
// TokiDictionary
//
// Created by Avery Ada Pace on 10/1/21.
//
import Foundation
struct TokiDictEntry: Decodable {
var word: String
var definitions: [TokiDictDefinition]
}
struct TokiDictDefinition: Decodable {
var pos: String
var definition: String
}

View File

@ -0,0 +1,46 @@
//
// TokiJSONLoader.swift
// TokiJSONLoader
//
// Created by Avery Ada Pace on 10/1/21.
//
import Foundation
class TokiJSONLoader: ObservableObject {
@Published var dictionary: [TokiDictEntry] = []
@Published var partsOfSpeech: [TokiPartOfSpeech] = []
func loadDictionary() {
let jsonData = loadJSON("toki-dictionary")
do {
let decodedData = try JSONDecoder().decode([TokiDictEntry].self, from: jsonData!)
dictionary = decodedData
} catch {
print("Decode error: \(error)")
return
}
}
func loadPOS() {
let jsonData = loadJSON("toki-partsofspeech")
do {
let decodedData = try JSONDecoder().decode([TokiPartOfSpeech].self, from: jsonData!)
partsOfSpeech = decodedData
} catch {
print("Decode error: \(error)")
return
}
}
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
}
}

View File

@ -0,0 +1,13 @@
//
// TokiPartsOfSpeech.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/2/21.
//
import Foundation
struct TokiPartOfSpeech: Decodable {
var pos: String
var definition: String
}

View File

@ -0,0 +1,8 @@
//
// TokiPartsOfSpeechProvider.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/3/21.
//
import Foundation

View File

@ -0,0 +1,39 @@
//
// ContentView.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/2/21.
//
import SwiftUI
import CoreData
struct ContentView: View {
@ObservedObject var jsonLoader = TokiJSONLoader()
var body: some View {
List(jsonLoader.dictionary, id: \.word) { entry in
VStack(alignment: .leading) {
Text(entry.word)
.font(.title)
ForEach(entry.definitions, id: \.pos) { definition in
HStack(alignment: .top) {
Text(definition.pos)
Text(definition.definition)
}
}}
}
.onAppear {
self.jsonLoader.loadDictionary()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}