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?
|
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 {
|
|
|
|
TextField("Search", text: $tokiInput)
|
|
|
|
.multilineTextAlignment(.center)
|
|
|
|
.textInputAutocapitalization(.never)
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.padding(8)
|
|
|
|
.onSubmit {
|
2022-10-28 04:52:53 +00:00
|
|
|
filterByInput()
|
|
|
|
//tokiDictViewModel.filterDictionaryEnglishMode(tokiInput)
|
2022-10-08 10:36:19 +00:00
|
|
|
}
|
2022-10-28 04:52:53 +00:00
|
|
|
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()
|
|
|
|
}
|
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)
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|