Skip to content

Commit 3ffa3da

Browse files
committed
#378 reimplement db manager class across keyboard functionalities
1 parent 571becb commit 3ffa3da

File tree

8 files changed

+166
-333
lines changed

8 files changed

+166
-333
lines changed

Keyboards/KeyboardsBase/KeyboardViewController.swift

+14-55
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,7 @@ class KeyboardViewController: UIInputViewController {
367367
/// - Parameters
368368
/// - word: the word for which corresponding emojis should be shown for.
369369
func getEmojiAutoSuggestions(for word: String) {
370-
let query = "SELECT * FROM emoji_keywords WHERE word = ?"
371-
let args = [word.lowercased()]
372-
let outputCols = ["emoji_0", "emoji_1", "emoji_2"]
373-
let emojisToDisplay = queryDBRow(query: query, outputCols: outputCols, args: args)
370+
let emojisToDisplay = LanguageDBManager.shared.queryEmojis(of: word.lowercased())
374371

375372
if !emojisToDisplay[0].isEmpty {
376373
emojisToDisplayArray = [String]()
@@ -448,7 +445,7 @@ class KeyboardViewController: UIInputViewController {
448445
}
449446

450447
// Get options for completion that start with the current prefix and are not just one letter.
451-
let completionOptions = queryAutocompletions(word: currentPrefix)
448+
let completionOptions = LanguageDBManager.shared.queryAutocompletions(word: currentPrefix)
452449

453450
if !completionOptions[0].isEmpty {
454451
if completionOptions.count <= 3 {
@@ -505,20 +502,20 @@ class KeyboardViewController: UIInputViewController {
505502
let prefix = proxy.documentContextBeforeInput?.components(separatedBy: " ").secondToLast() ?? ""
506503

507504
completionWords = [String]()
508-
let query = "SELECT * FROM verbs WHERE verb = ?"
509505
for i in 0 ..< 3 {
510506
// Get conjugations of the preselected verbs.
511-
let args = [verbsAfterPronounsArray[i]]
512507
if let tense = pronounAutosuggestionTenses[prefix.lowercased()] {
513508
let outputCols = [tense]
514-
var suggestion = queryDBRow(query: query, outputCols: outputCols, args: args)[0]
509+
var suggestion = LanguageDBManager.shared.queryVerb(of: verbsAfterPronounsArray[i], with: outputCols)[0]
510+
515511
if suggestion == "" {
516512
suggestion = verbsAfterPronounsArray[i]
517513
}
518514

519515
if suggestion == "REFLEXIVE_PRONOUN" && controllerLanguage == "Spanish" {
520516
suggestion = getESReflexivePronoun(pronoun: prefix.lowercased())
521517
}
518+
522519
if shiftButtonState == .shift {
523520
completionWords.append(suggestion.capitalize())
524521
} else if capsLockButtonState == .locked {
@@ -579,13 +576,9 @@ class KeyboardViewController: UIInputViewController {
579576
} else {
580577
// We have to consider these different cases as the key always has to match.
581578
// Else, even if the lowercased prefix is present in the dictionary, if the actual prefix isn't present we won't get an output.
582-
let query = "SELECT * FROM autosuggestions WHERE word = ?"
583-
let argsLower = [prefix.lowercased()]
584-
let argsCapitalize = [prefix.capitalized]
585-
let outputCols = ["suggestion_0", "suggestion_1", "suggestion_2"]
579+
let suggestionsLowerCasePrefix = LanguageDBManager.shared.queryAutosuggestions(of: prefix.lowercased())
580+
let suggestionsCapitalizedPrefix = LanguageDBManager.shared.queryAutosuggestions(of: prefix.capitalized)
586581

587-
let suggestionsLowerCasePrefix = queryDBRow(query: query, outputCols: outputCols, args: argsLower)
588-
let suggestionsCapitalizedPrefix = queryDBRow(query: query, outputCols: outputCols, args: argsCapitalize)
589582
if !suggestionsLowerCasePrefix[0].isEmpty {
590583
completionWords = [String]()
591584
for i in 0 ..< 3 {
@@ -598,12 +591,9 @@ class KeyboardViewController: UIInputViewController {
598591
} else if capsLockButtonState == .locked {
599592
completionWords.append(suggestionsLowerCasePrefix[i].uppercased())
600593
} else {
601-
let nounGenderQuery = "SELECT * FROM nouns WHERE noun = ?"
602-
let nounGenderArgs = [suggestionsLowerCasePrefix[i]]
603-
let outputCols = ["form"]
604-
605-
let nounForm = queryDBRow(query: nounGenderQuery, outputCols: outputCols, args: nounGenderArgs)[0]
594+
let nounForm = LanguageDBManager.shared.queryNounForm(of: suggestionsLowerCasePrefix[i])[0]
606595
hasNounForm = !nounForm.isEmpty
596+
607597
if !hasNounForm {
608598
completionWords.append(suggestionsLowerCasePrefix[i].lowercased())
609599
} else {
@@ -618,6 +608,7 @@ class KeyboardViewController: UIInputViewController {
618608
completionWords.append(previousWord)
619609
continue
620610
}
611+
621612
if shiftButtonState == .shift {
622613
completionWords.append(suggestionsCapitalizedPrefix[i].capitalize())
623614
} else if capsLockButtonState == .locked {
@@ -1496,10 +1487,8 @@ class KeyboardViewController: UIInputViewController {
14961487
}
14971488

14981489
// Populate conjugation view buttons.
1499-
let query = "SELECT * FROM verbs WHERE verb = ?"
1500-
let args = [verbToConjugate]
15011490
let outputCols = allConjugations
1502-
let conjugationsToDisplay = queryDBRow(query: query, outputCols: outputCols, args: args)
1491+
let conjugationsToDisplay = LanguageDBManager.shared.queryVerb(of: verbToConjugate, with: outputCols)
15031492
for index in 0 ..< allConjugations.count {
15041493
if conjugationsToDisplay[index] == "" {
15051494
// Assign the invalid message if the conjugation isn't present in the directory.
@@ -1879,46 +1868,19 @@ class KeyboardViewController: UIInputViewController {
18791868
// Show the name of the keyboard to the user.
18801869
showKeyboardLanguage = true
18811870

1882-
// Initialize the language database and create the autosuggestions lexicon.
1883-
languageDB = openDBQueue()
1884-
18851871
// Add UILexicon words including unpaired first and last names from Contacts to autocompletions.
1886-
let addToAutocompleteLexiconQuery = "INSERT OR IGNORE INTO autocomplete_lexicon (word) VALUES (?)"
18871872
requestSupplementaryLexicon { (userLexicon: UILexicon?) in
18881873
if let lexicon = userLexicon {
18891874
for item in lexicon.entries {
18901875
if item.documentText.count > 1 {
1891-
writeDBRow(query: addToAutocompleteLexiconQuery, args: [item.documentText])
1876+
LanguageDBManager.shared.insertAutocompleteLexion(of: item.documentText)
18921877
}
18931878
}
18941879
}
18951880
}
18961881

18971882
// Drop non-unique values in case the lexicon has added words that were already present.
1898-
let dropNonUniqueAutosuggestionsQuery = """
1899-
DELETE FROM autocomplete_lexicon
1900-
WHERE rowid NOT IN (
1901-
SELECT
1902-
MIN(rowid)
1903-
1904-
FROM
1905-
autocomplete_lexicon
1906-
1907-
GROUP BY
1908-
word
1909-
)
1910-
"""
1911-
do {
1912-
try languageDB.write { db in
1913-
try db.execute(sql: dropNonUniqueAutosuggestionsQuery)
1914-
}
1915-
} catch let error as DatabaseError {
1916-
let errorMessage = error.message
1917-
let errorSQL = error.sql
1918-
print(
1919-
"An error '\(String(describing: errorMessage))' occurred in the query: \(String(describing: errorSQL))"
1920-
)
1921-
} catch {}
1883+
LanguageDBManager.shared.deleteNonUniqueAutosuggestions()
19221884
}
19231885

19241886
setKeyboard()
@@ -2437,11 +2399,8 @@ class KeyboardViewController: UIInputViewController {
24372399
}
24382400
}
24392401
}
2440-
let prepCaseQuery = "SELECT * FROM prepositions WHERE preposition = ?"
2441-
let prepCaseArgs = [wordToCheck.lowercased()]
2442-
let outputCols = ["form"]
24432402

2444-
let prepForm = queryDBRow(query: prepCaseQuery, outputCols: outputCols, args: prepCaseArgs)[0]
2403+
let prepForm = LanguageDBManager.shared.queryPrepForm(of: wordToCheck.lowercased())[0]
24452404
hasPrepForm = !prepForm.isEmpty
24462405
if hasPrepForm {
24472406
resetCaseDeclensionState()

0 commit comments

Comments
 (0)