TokiTrainer/Toki Trainer/Views/ContentView.swift

97 lines
3.2 KiB
Swift
Raw Normal View History

2021-11-03 18:21:22 +00:00
//
// ContentView.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/2/21.
//
import SwiftUI
import CoreData
extension String: Identifiable {
public var id: String { self }
}
2021-11-03 18:21:22 +00:00
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
2021-11-03 18:21:22 +00:00
2021-11-07 06:25:18 +00:00
@State private var tokiInput: String = ""
var body: some View {
TabView {
TranslatorView()
.tabItem {
Image(systemName: "pencil")
Text("Phrase Lookup")
}
2021-11-07 06:25:18 +00:00
FlashCardView()
.tabItem {
Image(systemName: "character.textbox")
Text("Flash Cards")
}
2021-11-07 22:15:11 +00:00
FlashCardResultsView()
.tabItem {
Image(systemName: "phone.fill")
Text("Flash Card Results")
}
2021-11-07 06:25:18 +00:00
}
}
func openPartsOfSpeechView() {
print("Button pressed.")
}
}
struct TranslatorView: View {
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
2021-11-06 01:04:03 +00:00
@State private var selectedPartOfSpeech: String?
2021-11-05 20:21:46 +00:00
@State private var tokiInput: String = ""
2021-11-07 06:25:18 +00:00
2021-11-03 18:21:22 +00:00
var body: some View {
VStack {
2021-11-05 20:21:46 +00:00
TextField("Enter Toki Pona Word or Phrase", text: $tokiInput)
.multilineTextAlignment(.center)
2021-11-05 20:21:46 +00:00
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.padding(8)
2021-11-05 20:21:46 +00:00
.onSubmit {
tokiDictViewModel.filterDictionary(tokiInput)
}
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: {
self.selectedPartOfSpeech = String(definition.pos)
}) {
Text(definition.pos)
.frame(width: 45, height: 22, alignment: .center)
.foregroundColor(.black)
.background(Color(K.posColors[definition.pos]!))
.cornerRadius(5.0)
.padding(4)
}
.buttonStyle(BorderlessButtonStyle())
Text(definition.definition)
.fixedSize(horizontal: false, vertical: true)
.padding(4)
}
2021-11-03 18:21:22 +00:00
}
}
}
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
2021-11-06 01:04:03 +00:00
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS, partsOfSpeech: tokiDictViewModel.partsOfSpeech)
}
2021-11-03 18:21:22 +00:00
}
}
2021-11-03 18:21:22 +00:00
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
2021-11-03 18:21:22 +00:00
}
}