LMMgr // Simplify writeUserPhrase().
This commit is contained in:
parent
4e9faa70c5
commit
74ccd6d0b7
|
@ -57,7 +57,6 @@ public protocol IMEStateDataProtocol {
|
||||||
var userPhraseKVPair: (String, String) { get }
|
var userPhraseKVPair: (String, String) { get }
|
||||||
var userPhraseDumped: String { get }
|
var userPhraseDumped: String { get }
|
||||||
var userPhraseDumpedConverted: String { get }
|
var userPhraseDumpedConverted: String { get }
|
||||||
var doesUserPhraseExist: Bool { get }
|
|
||||||
var tooltipColorState: TooltipColorState { get set }
|
var tooltipColorState: TooltipColorState { get set }
|
||||||
mutating func updateTooltipForMarking()
|
mutating func updateTooltipForMarking()
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,14 +169,6 @@ public struct IMEStateData: IMEStateDataProtocol {
|
||||||
// MARK: - IMEState 工具函式
|
// MARK: - IMEState 工具函式
|
||||||
|
|
||||||
public extension IMEStateData {
|
public extension IMEStateData {
|
||||||
var doesUserPhraseExist: Bool {
|
|
||||||
let text = displayedText.map(\.description)[markedRange].joined()
|
|
||||||
let joined = markedReadings.joined(separator: InputHandler.keySeparator)
|
|
||||||
return LMMgr.checkIfUserPhraseExist(
|
|
||||||
userPhrase: text, mode: IMEApp.currentInputMode, key: joined
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
var readingThreadForDisplay: String {
|
var readingThreadForDisplay: String {
|
||||||
var arrOutput = [String]()
|
var arrOutput = [String]()
|
||||||
for neta in markedReadings {
|
for neta in markedReadings {
|
||||||
|
|
|
@ -634,9 +634,9 @@ public class LMMgr {
|
||||||
// MARK: - 寫入使用者檔案
|
// MARK: - 寫入使用者檔案
|
||||||
|
|
||||||
public static func writeUserPhrase(
|
public static func writeUserPhrase(
|
||||||
_ userPhrase: String?, inputMode mode: Shared.InputMode, areWeDuplicating: Bool, areWeDeleting: Bool
|
_ userPhrase: String, inputMode mode: Shared.InputMode, areWeDeleting: Bool
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if var currentMarkedPhrase: String = userPhrase {
|
var userPhraseOutput: String = userPhrase
|
||||||
if !chkUserLMFilesExist(.imeModeCHS)
|
if !chkUserLMFilesExist(.imeModeCHS)
|
||||||
|| !chkUserLMFilesExist(.imeModeCHT)
|
|| !chkUserLMFilesExist(.imeModeCHT)
|
||||||
{
|
{
|
||||||
|
@ -646,15 +646,21 @@ public class LMMgr {
|
||||||
let theType: vChewingLM.ReplacableUserDataType = areWeDeleting ? .theFilter : .thePhrases
|
let theType: vChewingLM.ReplacableUserDataType = areWeDeleting ? .theFilter : .thePhrases
|
||||||
let theURL = userDictDataURL(mode: mode, type: theType)
|
let theURL = userDictDataURL(mode: mode, type: theType)
|
||||||
|
|
||||||
|
let arr = userPhraseOutput.split(separator: " ")
|
||||||
|
var areWeDuplicating = false
|
||||||
|
if arr.count >= 2 {
|
||||||
|
areWeDuplicating = Self.checkIfUserPhraseExist(
|
||||||
|
userPhrase: arr[0].description, mode: mode, key: arr[1].description, factoryDictionaryOnly: true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if areWeDuplicating, !areWeDeleting {
|
if areWeDuplicating, !areWeDeleting {
|
||||||
// Do not use ASCII characters to comment here.
|
// Do not use ASCII characters to comment here.
|
||||||
// Otherwise, it will be scrambled by cnvHYPYtoBPMF
|
userPhraseOutput += " #𝙾𝚟𝚎𝚛𝚛𝚒𝚍𝚎"
|
||||||
// module shipped in the vChewing Phrase Editor.
|
|
||||||
currentMarkedPhrase += " #𝙾𝚟𝚎𝚛𝚛𝚒𝚍𝚎"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let writeFile = FileHandle(forUpdatingAtPath: theURL.path),
|
if let writeFile = FileHandle(forUpdatingAtPath: theURL.path),
|
||||||
let data = currentMarkedPhrase.data(using: .utf8),
|
let data = userPhraseOutput.data(using: .utf8),
|
||||||
let endl = "\n".data(using: .utf8)
|
let endl = "\n".data(using: .utf8)
|
||||||
{
|
{
|
||||||
writeFile.seekToEndOfFile()
|
writeFile.seekToEndOfFile()
|
||||||
|
@ -681,8 +687,6 @@ public class LMMgr {
|
||||||
loadUserPhrasesData(type: .thePhrases)
|
loadUserPhrasesData(type: .thePhrases)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - 藉由語彙編輯器開啟使用者檔案
|
// MARK: - 藉由語彙編輯器開啟使用者檔案
|
||||||
|
|
||||||
|
|
|
@ -35,12 +35,10 @@ extension SessionCtl: InputHandlerDelegate {
|
||||||
guard let inputHandler = inputHandler, state.type == .ofMarking else { return false }
|
guard let inputHandler = inputHandler, state.type == .ofMarking else { return false }
|
||||||
if !LMMgr.writeUserPhrase(
|
if !LMMgr.writeUserPhrase(
|
||||||
state.data.userPhraseDumped, inputMode: inputMode,
|
state.data.userPhraseDumped, inputMode: inputMode,
|
||||||
areWeDuplicating: state.data.doesUserPhraseExist,
|
|
||||||
areWeDeleting: addToFilter
|
areWeDeleting: addToFilter
|
||||||
)
|
)
|
||||||
|| !LMMgr.writeUserPhrase(
|
|| !LMMgr.writeUserPhrase(
|
||||||
state.data.userPhraseDumpedConverted, inputMode: inputMode.reversed,
|
state.data.userPhraseDumpedConverted, inputMode: inputMode.reversed,
|
||||||
areWeDuplicating: false,
|
|
||||||
areWeDeleting: addToFilter
|
areWeDeleting: addToFilter
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
@ -194,12 +192,10 @@ extension SessionCtl: CtlCandidateDelegate {
|
||||||
|
|
||||||
if !LMMgr.writeUserPhrase(
|
if !LMMgr.writeUserPhrase(
|
||||||
userPhraseDumped, inputMode: inputMode,
|
userPhraseDumped, inputMode: inputMode,
|
||||||
areWeDuplicating: action != .toFilter,
|
|
||||||
areWeDeleting: action == .toFilter
|
areWeDeleting: action == .toFilter
|
||||||
)
|
)
|
||||||
|| !LMMgr.writeUserPhrase(
|
|| !LMMgr.writeUserPhrase(
|
||||||
userPhraseDumpedConverted, inputMode: inputMode.reversed,
|
userPhraseDumpedConverted, inputMode: inputMode.reversed,
|
||||||
areWeDuplicating: action != .toFilter,
|
|
||||||
areWeDeleting: action == .toFilter
|
areWeDeleting: action == .toFilter
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue