Rough dictionary list working
This commit is contained in:
		@@ -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)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1751
									
								
								Toki Trainer/JSON Data/toki-dictionary.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1751
									
								
								Toki Trainer/JSON Data/toki-dictionary.json
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										46
									
								
								Toki Trainer/JSON Data/toki-partsofspeech.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								Toki Trainer/JSON Data/toki-partsofspeech.json
									
									
									
									
									
										Normal 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"
 | 
			
		||||
  }
 | 
			
		||||
]
 | 
			
		||||
							
								
								
									
										19
									
								
								Toki Trainer/Models/TokiDictionary.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Toki Trainer/Models/TokiDictionary.swift
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										46
									
								
								Toki Trainer/Models/TokiJSONLoader.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								Toki Trainer/Models/TokiJSONLoader.swift
									
									
									
									
									
										Normal 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
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								Toki Trainer/Models/TokiPartOfSpeech.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Toki Trainer/Models/TokiPartOfSpeech.swift
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								Toki Trainer/States/TokiPartsOfSpeechProvider.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Toki Trainer/States/TokiPartsOfSpeechProvider.swift
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
//
 | 
			
		||||
//  TokiPartsOfSpeechProvider.swift
 | 
			
		||||
//  Toki Trainer
 | 
			
		||||
//
 | 
			
		||||
//  Created by Avery Ada Pace on 11/3/21.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
import Foundation
 | 
			
		||||
							
								
								
									
										39
									
								
								Toki Trainer/Views/ContentView.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								Toki Trainer/Views/ContentView.swift
									
									
									
									
									
										Normal 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()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user