diff --git a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift index 3eb773fe..ab1e179b 100644 --- a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift +++ b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift @@ -141,7 +141,7 @@ public extension vChewingLM { // 上述幾個函式不要加 Async,因為這些內容都被 LMMgr 負責用別的方法 Async 了、用 GCD 的多任務並行共結來完成。 - public func loadUserPhrasesData(path: String, filterPath: String) { + public func loadUserPhrasesData(path: String, filterPath: String?) { DispatchQueue.main.async { if FileManager.default.isReadableFile(atPath: path) { self.lmUserPhrases.clear() @@ -151,6 +151,7 @@ public extension vChewingLM { vCLog("lmUserPhrases: File access failure: \(path)") } } + guard let filterPath = filterPath else { return } DispatchQueue.main.async { if FileManager.default.isReadableFile(atPath: filterPath) { self.lmFiltered.clear() @@ -162,6 +163,17 @@ public extension vChewingLM { } } + /// 這個函式不用 GCD。 + public func reloadUserFilterDirectly(path: String) { + if FileManager.default.isReadableFile(atPath: path) { + lmFiltered.clear() + lmFiltered.open(path) + vCLog("lmFiltered: \(lmFiltered.count) entries of data loaded from: \(path)") + } else { + vCLog("lmFiltered: File access failure: \(path)") + } + } + public func loadUserSymbolData(path: String) { DispatchQueue.main.async { if FileManager.default.isReadableFile(atPath: path) { diff --git a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Core.swift b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Core.swift index b5030007..076c6253 100644 --- a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Core.swift +++ b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Core.swift @@ -220,15 +220,22 @@ public class LMMgr { return } switch type { - case .thePhrases, .theFilter: + case .thePhrases: Self.lmCHT.loadUserPhrasesData( path: userDictDataURL(mode: .imeModeCHT, type: .thePhrases).path, - filterPath: userDictDataURL(mode: .imeModeCHT, type: .theFilter).path + filterPath: nil ) Self.lmCHS.loadUserPhrasesData( path: userDictDataURL(mode: .imeModeCHS, type: .thePhrases).path, - filterPath: userDictDataURL(mode: .imeModeCHS, type: .theFilter).path + filterPath: nil ) + case .theFilter: + DispatchQueue.main.async { + Self.reloadUserFilterDirectly(mode: IMEApp.currentInputMode) + } + DispatchQueue.main.async { + Self.reloadUserFilterDirectly(mode: IMEApp.currentInputMode.reversed) + } case .theReplacements: if PrefMgr.shared.phraseReplacementEnabled { Self.loadUserPhraseReplacement() } case .theAssociates: @@ -270,6 +277,10 @@ public class LMMgr { ) } + public static func reloadUserFilterDirectly(mode: Shared.InputMode) { + Self.getLM(mode: mode).reloadUserFilterDirectly(path: userDictDataURL(mode: mode, type: .theFilter).path) + } + public static func checkIfPhrasePairExists( userPhrase: String, mode: Shared.InputMode, diff --git a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_UserPhraseStructure.swift b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_UserPhraseStructure.swift index b53ff9f1..14ec9b83 100644 --- a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_UserPhraseStructure.swift +++ b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_UserPhraseStructure.swift @@ -185,6 +185,7 @@ public extension LMMgr { fileHandle.seek(toFileOffset: currentWorkingOffset) fileHandle.write(blankData) } + LMMgr.reloadUserFilterDirectly(mode: inputMode) return true } } diff --git a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Utilities.swift b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Utilities.swift index de8f883d..48836072 100644 --- a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Utilities.swift +++ b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LMMgr_Utilities.swift @@ -282,7 +282,7 @@ public extension LMMgr { if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { Broadcaster.shared.eventForReloadingPhraseEditor = .init() } - loadUserPhrasesData(type: .thePhrases) + loadUserPhrasesData(type: areWeFiltering ? .theFilter : .thePhrases) } // MARK: - 藉由語彙編輯器開啟使用者檔案