From 5a5e56cb66a292d1508f9f8331d5dba77ef04cf8 Mon Sep 17 00:00:00 2001 From: maddiebaka Date: Fri, 17 Nov 2023 11:46:10 -0500 Subject: [PATCH] UI update for advanced search options, search button --- Toki Trainer/Localizable.xcstrings | 13 ++++ .../Views/WordListViews/DictionaryView.swift | 70 ++++++++++++++----- .../Views/WordListViews/TranslatorView.swift | 30 ++++---- 3 files changed, 82 insertions(+), 31 deletions(-) diff --git a/Toki Trainer/Localizable.xcstrings b/Toki Trainer/Localizable.xcstrings index 7484039..973c674 100644 --- a/Toki Trainer/Localizable.xcstrings +++ b/Toki Trainer/Localizable.xcstrings @@ -150,6 +150,19 @@ } } } + }, + "Search for:" : { + "localizations" : { + "fr" : { + "stringUnit" : { + "state" : "translated", + "value" : "Options de recherche" + } + } + } + }, + "Words" : { + } }, "version" : "1.0" diff --git a/Toki Trainer/Views/WordListViews/DictionaryView.swift b/Toki Trainer/Views/WordListViews/DictionaryView.swift index 66bc615..2a779ea 100644 --- a/Toki Trainer/Views/WordListViews/DictionaryView.swift +++ b/Toki Trainer/Views/WordListViews/DictionaryView.swift @@ -17,33 +17,59 @@ struct DictionaryView: View { @State private var tokiInput: String = "" @State private var selectedPartOfSpeech: String? + @State private var advancedSearchEnabled = false @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 { + HStack { + TextField("Search", text: $tokiInput) + //.multilineTextAlignment(.center) + .textInputAutocapitalization(.never) + .disableAutocorrection(true) + .padding(8) + .onSubmit { + filterByInput() + //tokiDictViewModel.filterDictionaryEnglishMode(tokiInput) + } + Button { + hideKeyboard() filterByInput() - //tokiDictViewModel.filterDictionaryEnglishMode(tokiInput) + } label: { + Image(systemName: "magnifyingglass") } - Picker("Language", selection: $searchMode) { - Text("Dictionary").tag(SearchMode.Dictionary) - Text("Definitions").tag(SearchMode.Definitions) + .buttonStyle(.borderedProminent) + + Button(action: { + withAnimation { + advancedSearchEnabled.toggle() + } + }, label: { + Image(systemName: "slider.horizontal.2.square.on.square") + }) + .buttonStyle(.bordered) } - .pickerStyle(SegmentedPickerStyle()) - .onTapGesture { - if self.searchMode == SearchMode.Dictionary { - self.searchMode = SearchMode.Definitions - } else { - self.searchMode = SearchMode.Dictionary + .padding([.top, .leading, .trailing], 8) + if advancedSearchEnabled == true { + VStack { + Text("Search for:") + Picker("Language", selection: $searchMode) { + Text("Words").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() + } } - filterByInput() + .padding([.leading, .trailing], 8) } List(tokiDictViewModel.dictionary, id: \.word) { entry in TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech) @@ -70,6 +96,16 @@ struct DictionaryView: View { } } +extension View { + func searchOptionsButtonStyle(toggle: Binding) -> any PrimitiveButtonStyle { + if toggle.wrappedValue == true { + BorderedButtonStyle() + } else { + BorderlessButtonStyle() + } + } +} + struct DictionaryView_Previews: PreviewProvider { diff --git a/Toki Trainer/Views/WordListViews/TranslatorView.swift b/Toki Trainer/Views/WordListViews/TranslatorView.swift index 64f78e9..d5369e5 100644 --- a/Toki Trainer/Views/WordListViews/TranslatorView.swift +++ b/Toki Trainer/Views/WordListViews/TranslatorView.swift @@ -15,22 +15,24 @@ struct TranslatorView: View { var body: some View { VStack { -// Button(action: changeTranslationDirection) { -// #warning("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 { + HStack { + TextField("Enter Toki Pona Word or Phrase", text: $tokiInput) + .textInputAutocapitalization(.never) + .disableAutocorrection(true) + .padding(8) + .onSubmit { + tokiDictViewModel.translatePhrase(tokiInput) + } + Button { + hideKeyboard() tokiDictViewModel.translatePhrase(tokiInput) + } label: { + Image(systemName: "magnifyingglass") } + .buttonStyle(.borderedProminent) + } + .padding([.top, .leading, .trailing], 8) + if tokiInput.count == 0 { List(tokiDictViewModel.dictionary, id: \.word) { entry in TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)