LMMgr // Fix when to reload phrase editors.

This commit is contained in:
ShikiSuen 2024-02-14 13:35:23 +08:00
parent 71e34790e8
commit bb4729ee3f
5 changed files with 22 additions and 16 deletions

View File

@ -47,7 +47,6 @@ extension AppDelegate {
// initUserLangModels()
if PrefMgr.shared.shouldAutoReloadUserDataFiles || forced { LMMgr.initUserLangModels() }
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if #available(macOS 10.15, *) { FileObserveProject.shared.touch() }
if PrefMgr.shared.phraseEditorAutoReloadExternalModifications {
Broadcaster.shared.eventForReloadingPhraseEditor = .init()
}

View File

@ -8,6 +8,7 @@
import Foundation
import LineReader
import Shared
public extension LMMgr {
/// 使 TXT
@ -63,7 +64,11 @@ public extension LMMgr {
fileHandlerCHT.seekToEndOfFile()
fileHandlerCHT.write(outputDataCHT)
return allPhrasesCHT.count
let result = allPhrasesCHT.count
if result > 0 {
Broadcaster.shared.eventForReloadingPhraseEditor = .init()
}
return result
}
}

View File

@ -240,7 +240,6 @@ public extension LMMgr {
}
// The new FolderMonitor module does NOT monitor cases that files are modified
// by the current application itself, requiring additional manual loading process here.
if #available(macOS 10.15, *) { FileObserveProject.shared.touch() }
if PrefMgr.shared.phraseEditorAutoReloadExternalModifications {
Broadcaster.shared.eventForReloadingPhraseEditor = .init()
}

View File

@ -17,19 +17,15 @@ import SwiftUI
private let loc: String =
(UserDefaults.current.array(forKey: UserDef.kAppleLanguages.rawValue) as? [String] ?? ["auto"])[0]
@available(macOS 13, *)
extension VwrPhraseEditorUI {
@AppStorage("PhraseEditorAutoReloadExternalModifications")
private static var autoReloadExternalModifications: Bool = true
}
@available(macOS 13, *)
public struct VwrPhraseEditorUI: View {
static var txtContentStorage: String = NSLocalizedString(
"Please select Simplified / Traditional Chinese mode above.", comment: ""
)
@Binding public var txtContent: String
@ObservedObject public var fileChangeIndicator = FileObserveProject.shared
@ObservedObject public var fileChangeIndicator = PEReloadEventObserver.shared
@AppStorage("PhraseEditorAutoReloadExternalModifications")
private var autoReloadExternalModifications: Bool = true
@State private var selAutoReloadExternalModifications: Bool = UserDefaults.current.bool(
forKey: UserDef.kPhraseEditorAutoReloadExternalModifications.rawValue)
@State var lblAddPhraseTag1 = PETerms.AddPhrases.locPhrase.localized.0
@ -252,7 +248,7 @@ public struct VwrPhraseEditorUI: View {
.frame(minWidth: 320, minHeight: 240)
.onChange(of: fileChangeIndicator.id) { _ in
Task {
if Self.autoReloadExternalModifications { update() }
if autoReloadExternalModifications { update() }
}
}
}
@ -291,7 +287,7 @@ public struct VwrPhraseEditorUI: View {
Toggle(
LocalizedStringKey("This editor only: Auto-reload modifications happened outside of this editor"),
isOn: $selAutoReloadExternalModifications.didChange {
Self.autoReloadExternalModifications = selAutoReloadExternalModifications
autoReloadExternalModifications = selAutoReloadExternalModifications
}
)
.controlSize(.small)

View File

@ -255,14 +255,21 @@ public enum Shared {
}
}
// MARK: - Observable Object
// MARK: - PEReloadEventObserver
@available(macOS 10.15, *)
public class FileObserveProject: ObservableObject, Equatable {
public static let shared = FileObserveProject()
public class PEReloadEventObserver: ObservableObject, Equatable {
public static let shared = PEReloadEventObserver()
private var observation: NSKeyValueObservation?
@Published public var id = UUID().uuidString
public static func == (lhs: FileObserveProject, rhs: FileObserveProject) -> Bool { lhs.id == rhs.id }
public init() {
observation = Broadcaster.shared.observe(\.eventForReloadingPhraseEditor, options: [.new]) { _, _ in
self.touch()
}
}
public static func == (lhs: PEReloadEventObserver, rhs: PEReloadEventObserver) -> Bool { lhs.id == rhs.id }
public func touch() {
id = UUID().uuidString