Part of speech highlighting when tapped from dictionary list
This commit is contained in:
@@ -8,28 +8,26 @@
|
||||
import Foundation
|
||||
|
||||
class TokiJSONLoader: ObservableObject {
|
||||
@Published var dictionary: [TokiDictEntry] = []
|
||||
@Published var partsOfSpeech: [TokiPartOfSpeech] = []
|
||||
|
||||
func loadDictionary() {
|
||||
func loadDictionary() -> [TokiDictEntry]? {
|
||||
let jsonData = loadJSON("toki-dictionary")
|
||||
do {
|
||||
let decodedData = try JSONDecoder().decode([TokiDictEntry].self, from: jsonData!)
|
||||
dictionary = decodedData
|
||||
return decodedData
|
||||
} catch {
|
||||
print("Decode error: \(error)")
|
||||
return
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func loadPOS() {
|
||||
func loadPOS() -> [TokiPartOfSpeech]? {
|
||||
let jsonData = loadJSON("toki-partsofspeech")
|
||||
do {
|
||||
let decodedData = try JSONDecoder().decode([TokiPartOfSpeech].self, from: jsonData!)
|
||||
partsOfSpeech = decodedData
|
||||
return decodedData
|
||||
} catch {
|
||||
print("Decode error: \(error)")
|
||||
return
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,8 +0,0 @@
|
||||
//
|
||||
// TokiPartsOfSpeechProvider.swift
|
||||
// Toki Trainer
|
||||
//
|
||||
// Created by Avery Ada Pace on 11/3/21.
|
||||
//
|
||||
|
||||
import Foundation
|
25
Toki Trainer/ViewModels/TokiDictionaryViewModel.swift
Normal file
25
Toki Trainer/ViewModels/TokiDictionaryViewModel.swift
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// TokiDictionaryViewModel.swift
|
||||
// Toki Trainer
|
||||
//
|
||||
// Created by Avery Ada Pace on 11/4/21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class TokiDictionaryViewModel: ObservableObject
|
||||
{
|
||||
let jsonLoader: TokiJSONLoader = TokiJSONLoader()
|
||||
|
||||
@Published var dictionary: [TokiDictEntry] = []
|
||||
@Published var partsOfSpeech: [TokiPartOfSpeech] = []
|
||||
|
||||
init() {
|
||||
if let safeDictionary = jsonLoader.loadDictionary() {
|
||||
dictionary = safeDictionary
|
||||
}
|
||||
if let safePartsOfSpeech = jsonLoader.loadPOS() {
|
||||
partsOfSpeech = safePartsOfSpeech
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,22 +8,30 @@
|
||||
import SwiftUI
|
||||
import CoreData
|
||||
|
||||
extension String: Identifiable {
|
||||
public var id: String { self }
|
||||
}
|
||||
|
||||
struct ContentView: View {
|
||||
@Environment(\.managedObjectContext) private var viewContext
|
||||
|
||||
@ObservedObject var jsonLoader = TokiJSONLoader()
|
||||
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
|
||||
@State private var selectedPartOfSpeech: String? = nil
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
TextField("Enter Toki Pona Word or Phrase", text: /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Value@*/.constant("")/*@END_MENU_TOKEN@*/)
|
||||
.multilineTextAlignment(.center)
|
||||
.padding(8)
|
||||
List(jsonLoader.dictionary, id: \.word) { entry in
|
||||
List(tokiDictViewModel.dictionary, id: \.word) { entry in
|
||||
VStack(alignment: .leading) {
|
||||
Text(entry.word)
|
||||
.font(.title)
|
||||
ForEach(entry.definitions, id: \.pos) { definition in
|
||||
HStack(alignment: .top) {
|
||||
Button(action: openPartsOfSpeechView) {
|
||||
Button(action: {
|
||||
self.selectedPartOfSpeech = String(definition.pos)
|
||||
}) {
|
||||
Text(definition.pos)
|
||||
.frame(width: 45, height: 22, alignment: .center)
|
||||
.foregroundColor(.black)
|
||||
@@ -31,6 +39,7 @@ struct ContentView: View {
|
||||
.cornerRadius(5.0)
|
||||
.padding(4)
|
||||
}
|
||||
.buttonStyle(BorderlessButtonStyle())
|
||||
Text(definition.definition)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.padding(4)
|
||||
@@ -40,19 +49,19 @@ struct ContentView: View {
|
||||
}
|
||||
.safeAreaInset(edge: .bottom) {
|
||||
HStack() {
|
||||
Button(action: openPartsOfSpeechView) {
|
||||
Text("Parts of Speech")
|
||||
Button("Parts of Speech") {
|
||||
self.selectedPartOfSpeech = ""
|
||||
}
|
||||
.padding(8)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(.thinMaterial)
|
||||
}
|
||||
.onAppear {
|
||||
self.jsonLoader.loadDictionary()
|
||||
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
|
||||
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS, tokiDictViewModel: self.tokiDictViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func openPartsOfSpeechView() {
|
||||
print("Button pressed.")
|
||||
|
49
Toki Trainer/Views/PartsOfSpeechView.swift
Normal file
49
Toki Trainer/Views/PartsOfSpeechView.swift
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// PartsOfSpeechView.swift
|
||||
// Toki Trainer
|
||||
//
|
||||
// Created by Avery Ada Pace on 11/4/21.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct PartsOfSpeechView: View {
|
||||
var selectedPartOfSpeech: String? = nil
|
||||
|
||||
@ObservedObject var tokiDictViewModel: TokiDictionaryViewModel
|
||||
|
||||
// init(selectedPartOfSpeech: String) {
|
||||
// _selectedPartOfSpeech = State(initialValue: selectedPartOfSpeech)
|
||||
// }
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text("Parts of Speech")
|
||||
.padding()
|
||||
VStack(alignment: .leading) {
|
||||
ForEach(tokiDictViewModel.partsOfSpeech, id: \.pos) { pos in
|
||||
HStack {
|
||||
Text(pos.pos)
|
||||
.frame(width: 45, height: 22, alignment: .center)
|
||||
.background(Color(K.posColors[pos.pos]!))
|
||||
.cornerRadius(5.0)
|
||||
.padding(1)
|
||||
Text(pos.definition)
|
||||
Spacer()
|
||||
}
|
||||
//.background(.blue)
|
||||
.background((selectedPartOfSpeech == pos.pos) ? Color(UIColor.systemGray4) : .white)
|
||||
.cornerRadius(5.0)
|
||||
.padding(2)
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct PartsOfSpeechView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
PartsOfSpeechView(selectedPartOfSpeech: "sep", tokiDictViewModel: TokiDictionaryViewModel())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user