2022-10-08 10:36:19 +00:00
|
|
|
//
|
|
|
|
// DictionaryView.swift
|
|
|
|
// Toki Trainer
|
|
|
|
//
|
|
|
|
// Created by maddiefuzz on 10/4/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2022-10-28 04:52:53 +00:00
|
|
|
enum SearchMode {
|
|
|
|
case Dictionary
|
|
|
|
case Definitions
|
|
|
|
}
|
|
|
|
|
2022-10-08 10:36:19 +00:00
|
|
|
struct DictionaryView: View {
|
|
|
|
@ObservedObject var tokiDictViewModel = TokiDictionaryViewModel()
|
|
|
|
|
2022-10-26 19:46:50 +00:00
|
|
|
@State private var tokiInput: String = ""
|
|
|
|
@State private var selectedPartOfSpeech: String?
|
2023-11-17 19:15:27 +00:00
|
|
|
@State private var showGenericLegend: Bool = false
|
2023-11-17 16:46:10 +00:00
|
|
|
@State private var advancedSearchEnabled = false
|
2022-10-28 04:52:53 +00:00
|
|
|
@State var searchMode: SearchMode = .Dictionary
|
|
|
|
|
|
|
|
@FocusState private var searchInputIsForuced: Bool
|
2022-10-08 10:36:19 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
2023-11-17 16:46:10 +00:00
|
|
|
HStack {
|
|
|
|
TextField("Search", text: $tokiInput)
|
|
|
|
//.multilineTextAlignment(.center)
|
|
|
|
.textInputAutocapitalization(.never)
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.padding(8)
|
|
|
|
.onSubmit {
|
|
|
|
filterByInput()
|
|
|
|
//tokiDictViewModel.filterDictionaryEnglishMode(tokiInput)
|
|
|
|
}
|
|
|
|
Button {
|
|
|
|
hideKeyboard()
|
2022-10-28 04:52:53 +00:00
|
|
|
filterByInput()
|
2023-11-17 16:46:10 +00:00
|
|
|
} label: {
|
|
|
|
Image(systemName: "magnifyingglass")
|
2022-10-08 10:36:19 +00:00
|
|
|
}
|
2023-11-17 16:46:10 +00:00
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
|
|
|
|
Button(action: {
|
|
|
|
withAnimation {
|
|
|
|
advancedSearchEnabled.toggle()
|
|
|
|
}
|
|
|
|
}, label: {
|
|
|
|
Image(systemName: "slider.horizontal.2.square.on.square")
|
|
|
|
})
|
|
|
|
.buttonStyle(.bordered)
|
2022-10-28 04:52:53 +00:00
|
|
|
}
|
2023-11-17 16:46:10 +00:00
|
|
|
.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()
|
|
|
|
}
|
2023-11-17 19:15:27 +00:00
|
|
|
Button(action: {
|
|
|
|
print("Legend")
|
|
|
|
showGenericLegend.toggle()
|
|
|
|
}, label: {
|
|
|
|
Text("Legend")
|
|
|
|
})
|
|
|
|
.padding(4)
|
2022-10-28 04:52:53 +00:00
|
|
|
}
|
2023-11-17 16:46:10 +00:00
|
|
|
.padding([.leading, .trailing], 8)
|
2022-10-28 04:52:53 +00:00
|
|
|
}
|
2022-10-08 10:36:19 +00:00
|
|
|
List(tokiDictViewModel.dictionary, id: \.word) { entry in
|
|
|
|
TokiWordsListEntryView(entry: entry, selectedPartOfSpeech: $selectedPartOfSpeech)
|
|
|
|
}
|
2022-10-08 12:35:14 +00:00
|
|
|
.sheet(item: $selectedPartOfSpeech) { selectedPOS in
|
|
|
|
PartsOfSpeechView(selectedPartOfSpeech: selectedPOS)
|
|
|
|
}
|
2023-11-17 19:15:27 +00:00
|
|
|
.sheet(isPresented: $showGenericLegend, content: {
|
|
|
|
PartsOfSpeechView(selectedPartOfSpeech: nil)
|
|
|
|
})
|
2022-10-08 10:36:19 +00:00
|
|
|
.onChange(of: tokiInput) { newValue in
|
2022-10-28 04:52:53 +00:00
|
|
|
filterByInput()
|
|
|
|
//tokiDictViewModel.filterDictionaryEnglishMode(newValue)
|
2022-10-08 10:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-28 04:52:53 +00:00
|
|
|
.onTapGesture {
|
|
|
|
hideKeyboard()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterByInput() {
|
|
|
|
if self.searchMode == SearchMode.Dictionary {
|
|
|
|
tokiDictViewModel.filterDictionary(tokiInput)
|
|
|
|
} else {
|
|
|
|
tokiDictViewModel.filterDictionaryEnglishMode(tokiInput)
|
|
|
|
}
|
2022-10-08 10:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-08 12:35:14 +00:00
|
|
|
|
2023-11-17 16:46:10 +00:00
|
|
|
extension View {
|
|
|
|
func searchOptionsButtonStyle(toggle: Binding<Bool>) -> any PrimitiveButtonStyle {
|
|
|
|
if toggle.wrappedValue == true {
|
|
|
|
BorderedButtonStyle()
|
|
|
|
} else {
|
|
|
|
BorderlessButtonStyle()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 04:52:53 +00:00
|
|
|
|
2022-10-08 12:35:14 +00:00
|
|
|
struct DictionaryView_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
static var previews: some View {
|
2022-10-26 19:46:50 +00:00
|
|
|
DictionaryView().previewLayout(.sizeThatFits).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
|
2022-10-08 12:35:14 +00:00
|
|
|
}
|
|
|
|
}
|