From 21408fb957d1572aafdfb650a97a4d1658ab81c7 Mon Sep 17 00:00:00 2001 From: Avery Pace Date: Fri, 5 Nov 2021 16:21:46 -0400 Subject: [PATCH] Translation works --- .../ViewModels/TokiDictionaryViewModel.swift | 35 +++++++++++++++++++ Toki Trainer/Views/ContentView.swift | 8 ++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Toki Trainer/ViewModels/TokiDictionaryViewModel.swift b/Toki Trainer/ViewModels/TokiDictionaryViewModel.swift index f3fcb4c..e4d85e1 100644 --- a/Toki Trainer/ViewModels/TokiDictionaryViewModel.swift +++ b/Toki Trainer/ViewModels/TokiDictionaryViewModel.swift @@ -11,15 +11,50 @@ class TokiDictionaryViewModel: ObservableObject { let jsonLoader: TokiJSONLoader = TokiJSONLoader() + private var fullDictionary: [TokiDictEntry] = [] + private var fullPartsOfSpeech: [TokiPartOfSpeech] = [] + @Published var dictionary: [TokiDictEntry] = [] @Published var partsOfSpeech: [TokiPartOfSpeech] = [] init() { if let safeDictionary = jsonLoader.loadDictionary() { dictionary = safeDictionary + fullDictionary = safeDictionary } if let safePartsOfSpeech = jsonLoader.loadPOS() { partsOfSpeech = safePartsOfSpeech + fullPartsOfSpeech = safePartsOfSpeech + } + } + + func filterDictionary(_ input: String) { + dictionary = [] + if(input.isEmpty) { + dictionary = fullDictionary + } else { + let capturePattern = #"(\w+)"# + let captures = self.searchStringForRegex(string: input, regex: capturePattern) + for capture in captures { + print(capture) + for value in fullDictionary { + if value.word == capture { + dictionary.append(value) + } + } + } + } + } + + func searchStringForRegex(string: String, regex: String) -> [String] { + let lowerCaseString = string.lowercased() + let strRange = NSRange(lowerCaseString.startIndex..