TokiTrainer/Toki Trainer/Views/WordListViews/TranslatorView.swift

72 lines
2.6 KiB
Swift

//
// TranslatorView.swift
// Toki Trainer
//
// Created by maddiefuzz on 10/8/22.
//
import SwiftUI
struct TranslatorView: View {
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
@State private var selectedPartOfSpeech: String?
@State private var tokiInput: String = ""
@State private var translateToTokiPona: Bool = false
var body: some View {
VStack {
// Button(action: changeTranslationDirection) {
// #warning("This needs to actually switch how the lookup happens")
// if translateToTokiPona == true {
// LanguageDirectionView(from: "English", to: "Toki Pona", fromColor: .blue, toColor: .cyan)
// } else {
// LanguageDirectionView(from: "Toki Pona", to: "English", fromColor: .cyan, toColor: .blue)
// }
// }
TextField("Enter Toki Pona Word or Phrase", text: $tokiInput)
.multilineTextAlignment(.center)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.padding(8)
.onSubmit {
tokiDictViewModel.translatePhrase(tokiInput)
}
if tokiInput.count == 0 {
List(tokiDictViewModel.dictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
} else {
List(tokiDictViewModel.translatedDictionary, id: \.header) { section in
Section {
ForEach(section.subDictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
} header: {
Text(section.header)
}
}
}
}
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS)
}
.onChange(of: tokiInput) { newValue in
tokiDictViewModel.translatePhrase(newValue)
}
.onTapGesture {
hideKeyboard()
}
}
func changeTranslationDirection() {
translateToTokiPona.toggle()
}
}
struct TranslatorView_Previews: PreviewProvider {
static var previews: some View {
TranslatorView().previewLayout(.sizeThatFits).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}