Skip to content

Commit

Permalink
scribe-org#292 fix UILexicon not loading into keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Taufi committed May 3, 2024
1 parent 55bf1b7 commit 63addae
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions Keyboards/KeyboardsBase/LanguageDBManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,33 @@ import SwiftyJSON

class LanguageDBManager {
static let shared = LanguageDBManager()
private lazy var languageDB = openDBQueue()
private var languageDB: DatabaseQueue?

private init() {}
private init() {
languageDB = openDBQueue()
}

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

return dbQueue
let dbResourcePath = Bundle.main.path(forResource: dbName, ofType: "sqlite")!
let fileManager = FileManager.default
do {
let dbPath = try fileManager
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("\(dbName).sqlite")
.path
if fileManager.fileExists(atPath: dbPath) {
try fileManager.removeItem(atPath: dbPath)
}
try fileManager.copyItem(atPath: dbResourcePath, toPath: dbPath)
let dbQueue = try DatabaseQueue(path: dbPath)
return dbQueue
} catch {
print("An error occurred: UILexicon not available")
let dbQueue = try! DatabaseQueue(path: dbResourcePath)
return dbQueue
}
}

/// Loads a JSON file that contains grammatical information into a dictionary.
Expand All @@ -56,7 +72,7 @@ class LanguageDBManager {
private func queryDBRow(query: String, outputCols: [String], args: StatementArguments) -> [String] {
var outputValues = [String]()
do {
try languageDB.read { db in
try languageDB?.read { db in
if let row = try Row.fetchOne(db, sql: query, arguments: args) {
for col in outputCols {
outputValues.append(row[col])
Expand Down Expand Up @@ -89,6 +105,7 @@ class LanguageDBManager {
private func queryDBRows(query: String, outputCols _: [String], args: StatementArguments) -> [String] {
var outputValues = [String]()
do {
guard let languageDB = languageDB else { return [] }
let rows = try languageDB.read { db in
try Row.fetchAll(db, sql: query, arguments: args)
}
Expand Down Expand Up @@ -119,7 +136,7 @@ class LanguageDBManager {
/// - args: arguments to pass to `query`.
private func writeDBRow(query: String, args: StatementArguments) {
do {
try languageDB.write { db in
try languageDB?.write { db in
try db.execute(sql: query, arguments: args)
}
} catch let error as DatabaseError {
Expand All @@ -139,7 +156,7 @@ class LanguageDBManager {
/// - args: arguments to pass to `query`.
private func deleteDBRow(query: String, args: StatementArguments? = nil) {
do {
try languageDB.write { db in
try languageDB?.write { db in
guard let args = args else {
try db.execute(sql: query)
return
Expand Down

0 comments on commit 63addae

Please sign in to comment.