Skip to content

Commit

Permalink
#96 setup language db connection and add query function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Apr 13, 2023
1 parent 713dd59 commit 5effd73
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Keyboards/KeyboardsBase/InterfaceVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var firstKeyboardLoad = false
var spaceBar = String()
var language = String()
var languageTextForSpaceBar: String {
"\(language) (Scribe)"
"\(language) (Scribe)"
}
var showKeyboardLanguage = false

Expand Down
4 changes: 4 additions & 0 deletions Keyboards/KeyboardsBase/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//

import UIKit
import GRDB

/// The parent KeyboardViewController class that is inherited by all Scribe keyboards.
class KeyboardViewController: UIInputViewController {
Expand Down Expand Up @@ -1410,6 +1411,9 @@ class KeyboardViewController: UIInputViewController {
// Show the name of the keyboard to the user.
showKeyboardLanguage = true

// Initialize the language database.
languageDB = openDBQueue()

// Access UILexicon words including unpaired first and last names from Contacts.
var uiLexiconWords = [String]()
self.requestSupplementaryLexicon { (userLexicon: UILexicon!) -> Void in
Expand Down
33 changes: 33 additions & 0 deletions Keyboards/KeyboardsBase/ScribeFunctionality/CommandVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//

import UIKit
import GRDB

// Basic keyboard functionality variables.
var capsLockPossible = false
Expand All @@ -30,6 +31,38 @@ var previousWord = ""
var backspaceTimer: Timer?
var scribeKeyHeight = CGFloat(0)

/// Makes a connection to the language database given the value for controllerLanguage.
func openDBQueue() -> DatabaseQueue {
let dbName = "\(String(describing: get_iso_code(keyboardLanguage: controllerLanguage).uppercased()))LanguageData"
let dbPath = Bundle.main.path(forResource: dbName, ofType: "sqlite")!
let db = try! DatabaseQueue(
path: dbPath
)

return db
}

/// Returns a value from the language database given a query and arguemtns.
///
/// - Parameters
/// - query: the query to run against the language database.
/// - args: arguments to pass to the query.
/// - outputCol: the column from which the value should come.
func queryDB(query: String, args: [String], outputCol: String) -> String {
var value = ""
do {
try languageDB.read { db in
if let row = try Row.fetchOne(db, sql: query, arguments: StatementArguments(args)) {
value = row[outputCol]
}
}
} catch {}

return value
}

var languageDB = try! DatabaseQueue()

// All data needed for Scribe commands for the given language keyboard.
var nouns = loadJSON(filename: "nouns")
let verbs = loadJSON(filename: "verbs")
Expand Down
34 changes: 30 additions & 4 deletions Keyboards/KeyboardsBase/Utilities.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
//
// File.swift
// Scribe
// Utilities.swift
//
// Created by Andrew Tavis on 13.04.23.
// Simple utility functions for data extractiona and language management.
//

import Foundation

/// Returns the ISO code given a langauge.
///
/// - Parameters
/// - language: the language an ISO code should be returned for.
func get_iso_code(keyboardLanguage: String) -> String {
var iso = ""
switch keyboardLanguage {
case "French":
iso = "fr"
case "German":
iso = "de"
case "Italian":
iso = "it"
case "Portuguese":
iso = "pt"
case "Russian":
iso = "ru"
case "Spanish":
iso = "es"
case "Swedish":
iso = "sv"
default:
break
}

return iso
}
Loading

0 comments on commit 5effd73

Please sign in to comment.