2022-10-08 12:35:14 +00:00
|
|
|
//
|
|
|
|
// 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 {
|
2023-11-17 16:46:10 +00:00
|
|
|
HStack {
|
|
|
|
TextField("Enter Toki Pona Word or Phrase", text: $tokiInput)
|
|
|
|
.textInputAutocapitalization(.never)
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.padding(8)
|
|
|
|
.onSubmit {
|
|
|
|
tokiDictViewModel.translatePhrase(tokiInput)
|
|
|
|
}
|
|
|
|
Button {
|
|
|
|
hideKeyboard()
|
2022-10-08 12:35:14 +00:00
|
|
|
tokiDictViewModel.translatePhrase(tokiInput)
|
2023-11-17 16:46:10 +00:00
|
|
|
} label: {
|
|
|
|
Image(systemName: "magnifyingglass")
|
2022-10-08 12:35:14 +00:00
|
|
|
}
|
2023-11-17 16:46:10 +00:00
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
}
|
|
|
|
.padding([.top, .leading, .trailing], 8)
|
|
|
|
|
2022-10-08 12:35:14 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-10-28 04:52:53 +00:00
|
|
|
.onTapGesture {
|
|
|
|
hideKeyboard()
|
|
|
|
}
|
2022-10-08 12:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func changeTranslationDirection() {
|
|
|
|
translateToTokiPona.toggle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TranslatorView_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
TranslatorView().previewLayout(.sizeThatFits).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
|
|
|
}
|
|
|
|
}
|