Refactor, decompose and add previews. Fix part of speech sheet pop over
This commit is contained in:
68
Toki Trainer/Views/WordListViews/TranslatorView.swift
Normal file
68
Toki Trainer/Views/WordListViews/TranslatorView.swift
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// 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) {
|
||||
// TODO: 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)
|
||||
}
|
||||
}
|
||||
|
||||
func changeTranslationDirection() {
|
||||
translateToTokiPona.toggle()
|
||||
}
|
||||
}
|
||||
|
||||
struct TranslatorView_Previews: PreviewProvider {
|
||||
|
||||
static var previews: some View {
|
||||
TranslatorView().previewLayout(.sizeThatFits).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user