Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#292 Load UI lexicon #420

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions Scribe/ParentTableCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ struct Section {
init(
sectionTitle: String,
imageString: String? = nil,
hasToggle: Bool = false, h _: Bool = false,

hasToggle: Bool = false,
hasNestedNavigation: Bool = false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly very confused how this even got through 🤔 Maybe just something from auto save when I was using VS Code to format? Thanks again! :)

sectionState: SectionState,
shortDescription: String? = nil,
externalLink: Bool? = false
) {
self.sectionTitle = sectionTitle
self.imageString = imageString
self.hasToggle = hasToggle
hasNestedNavigation = hasNestedNavigation
self.hasNestedNavigation = hasNestedNavigation
self.sectionState = sectionState
self.shortDescription = shortDescription
self.externalLink = externalLink
Expand Down
Loading