Part of speech highlighting when tapped from dictionary list

This commit is contained in:
Avery Pace
2021-11-04 14:10:22 -04:00
parent c7f50a4d3a
commit d5e3d29225
6 changed files with 108 additions and 31 deletions

View File

@@ -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.")

View 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())
}
}