Hide keyboard on tap, more robust search algorithm, option to search by dictionary word or definition

This commit is contained in:
Madeline
2022-10-28 00:52:53 -04:00
parent c74a2f520c
commit 0fea23b057
5 changed files with 86 additions and 11 deletions

View File

@@ -7,11 +7,19 @@
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 {
@@ -21,8 +29,22 @@ struct DictionaryView: View {
.disableAutocorrection(true)
.padding(8)
.onSubmit {
tokiDictViewModel.filterDictionary(tokiInput)
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)
}
@@ -30,12 +52,25 @@ struct DictionaryView: View {
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS)
}
.onChange(of: tokiInput) { newValue in
tokiDictViewModel.filterDictionaryEnglishMode(newValue)
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 {

View File

@@ -53,6 +53,9 @@ struct TranslatorView: View {
.onChange(of: tokiInput) { newValue in
tokiDictViewModel.translatePhrase(newValue)
}
.onTapGesture {
hideKeyboard()
}
}
func changeTranslationDirection() {