Skip to content

Commit

Permalink
快打模式下簡碼加選字碼可能和非簡碼有相同碼數 #26
Browse files Browse the repository at this point in the history
  • Loading branch information
y1lichen committed Jun 8, 2024
1 parent 7f2757b commit 02a6a0d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class DataInitializer {
}
persistenceContainer.saveContext()
userDefaults.set(true, forKey: "hadReadLiuJson")
userDefaults.set(false, forKey: "isLoadByLiuUniTab")
userDefaults.set(false, forKey: "isLoadByLiuUniTab")
NSLog("liu.json loaded")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CinReader {
persistenceContainer.saveContext()
// hadReadLiuJson就先不改名成hadReadLiu了...
userDefaults.set(true, forKey: "hadReadLiuJson")
userDefaults.set(false, forKey: "isLoadByLiuUniTab")
userDefaults.set(false, forKey: "isLoadByLiuUniTab")
NotifierController.notify(message: "自liu.cin讀取\(priority)個字元", stay: true)
} catch {
NSLog(error.localizedDescription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ struct InputEngine {
InputContext.shared.candidates = CoreDataHelper.getCharByZhuyin(text)
}

func getPhraseExactly(_ text: String) -> [Phrase] {
let request = NSFetchRequest<Phrase>(entityName: "Phrase")
request.predicate = NSPredicate(format: "key == %@", text)
request.sortDescriptors = [NSSortDescriptor(key: "key_priority", ascending: true)]
do {
let response = try PersistenceController.shared.container.viewContext.fetch(request)
return response
} catch {
NSLog(error.localizedDescription)
}
return []
}

func getNormalModePhrase(_ text: String) -> [Phrase] {
let request = NSFetchRequest<Phrase>(entityName: "Phrase")
request.predicate = settingsModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import CoreData
import Foundation

class SpModeManager {
// MARK: Internal

static func getSpKeyOfChar(_ chr: String) -> [String] {
let isLoadByLiu = UserDefaults.standard.bool(forKey: "isLoadByLiuUniTab")
if isLoadByLiu {
Expand All @@ -31,6 +33,9 @@ class SpModeManager {
return false
}
if !isLoadByLiu, !getSpOfCharWithoutLiuTab(text).contains(input) {
if handleMultipleSp(text, isLoadByLiu) {
return true
}
return false
}
return true
Expand Down Expand Up @@ -123,6 +128,20 @@ class SpModeManager {
}
return []
}



// MARK: Private
// 快打模式下簡碼加選字碼可能和非簡碼有相同碼數
// 使用.tab時不用特別處理,因使用.tab時簡碼判斷是直接讀字根檔
// https://github.com/y1lichen/ilimi-inputmethod/issues/26
private static func handleMultipleSp(_ text: String, _ isLoadByLiu: Bool) -> Bool {
let sps: [String] = isLoadByLiu ? getSpOfCharWithLiuTab(text) : getSpOfCharWithoutLiuTab(text)
if !sps.isEmpty {
let minLen = sps.first!.count
if InputEngine.shared.getPhraseExactly(sps.first!).first?.value != text,
InputContext.shared.getCurrentInput().count == minLen + 1 {
return true
}
}
return false
}
}

0 comments on commit 02a6a0d

Please sign in to comment.