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

80 lines
2.5 KiB
Swift

//
// DictionaryView.swift
// Toki Trainer
//
// Created by maddiefuzz on 10/4/22.
//
import SwiftUI
enum SearchMode {
case Dictionary
case Definitions
}
struct DictionaryView: View {
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
@State private var tokiInput: String = ""
@State private var selectedPartOfSpeech: String?
@State var searchMode: SearchMode = .Dictionary
@FocusState private var searchInputIsForuced: Bool
var body: some View {
VStack {
TextField("Search", text: $tokiInput)
.multilineTextAlignment(.center)
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.padding(8)
.onSubmit {
filterByInput()
//tokiDictViewModel.filterDictionaryEnglishMode(tokiInput)
}
Picker("Language", selection: $searchMode) {
Text("Dictionary").tag(SearchMode.Dictionary)
Text("Definitions").tag(SearchMode.Definitions)
}
.pickerStyle(SegmentedPickerStyle())
.onTapGesture {
if self.searchMode == SearchMode.Dictionary {
self.searchMode = SearchMode.Definitions
} else {
self.searchMode = SearchMode.Dictionary
}
filterByInput()
}
List(tokiDictViewModel.dictionary, id: \.word) { entry in
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
}
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS)
}
.onChange(of: tokiInput) { newValue in
filterByInput()
//tokiDictViewModel.filterDictionaryEnglishMode(newValue)
}
}
.onTapGesture {
hideKeyboard()
}
}
func filterByInput() {
if self.searchMode == SearchMode.Dictionary {
tokiDictViewModel.filterDictionary(tokiInput)
} else {
tokiDictViewModel.filterDictionaryEnglishMode(tokiInput)
}
}
}
struct DictionaryView_Previews: PreviewProvider {
static var previews: some View {
DictionaryView().previewLayout(.sizeThatFits).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}