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

@@ -0,0 +1,15 @@
//
// ViewExtensions.swift
// Toki Trainer
//
// Created by maddiefuzz on 10/28/22.
//
import SwiftUI
extension View {
func hideKeyboard() {
let resign = #selector(UIResponder.resignFirstResponder)
UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)
}
}

View File

@@ -52,11 +52,21 @@ class TokiDictionaryViewModel: ObservableObject {
entryMatch = true
}
// Check if any part of the word definitions match in English, even partially
// Check if any part of the word definitions match in English, even partially, but
// only by prefix (if one of the definition words matches the beginning of the word with
// the search term)
for definition in value.definitions {
if definition.definition.contains(input) {
entryMatch = true
let capturePattern = #"(\w+)"#
let captures = self.searchStringForRegex(string: definition.definition, regex: capturePattern)
for capture in captures {
if capture.hasPrefix(input) {
entryMatch = true
}
}
// Commented out, less strict matching that will match on any substring match
// if definition.definition.contains(input) {
// entryMatch = true
// }
}
// Add to dictionary

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() {