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() // initUserLangModels()
if PrefMgr.shared.shouldAutoReloadUserDataFiles || forced { LMMgr.initUserLangModels() } if PrefMgr.shared.shouldAutoReloadUserDataFiles || forced { LMMgr.initUserLangModels() }
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if #available(macOS 10.15, *) { FileObserveProject.shared.touch() }
if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { if PrefMgr.shared.phraseEditorAutoReloadExternalModifications {
Broadcaster.shared.eventForReloadingPhraseEditor = .init() Broadcaster.shared.eventForReloadingPhraseEditor = .init()
} }

View File

@ -8,6 +8,7 @@
import Foundation import Foundation
import LineReader import LineReader
import Shared
public extension LMMgr { public extension LMMgr {
/// 使 TXT /// 使 TXT
@ -63,7 +64,11 @@ public extension LMMgr {
fileHandlerCHT.seekToEndOfFile() fileHandlerCHT.seekToEndOfFile()
fileHandlerCHT.write(outputDataCHT) 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 // The new FolderMonitor module does NOT monitor cases that files are modified
// by the current application itself, requiring additional manual loading process here. // by the current application itself, requiring additional manual loading process here.
if #available(macOS 10.15, *) { FileObserveProject.shared.touch() }
if PrefMgr.shared.phraseEditorAutoReloadExternalModifications { if PrefMgr.shared.phraseEditorAutoReloadExternalModifications {
Broadcaster.shared.eventForReloadingPhraseEditor = .init() Broadcaster.shared.eventForReloadingPhraseEditor = .init()
} }

View File

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

View File

@ -255,14 +255,21 @@ public enum Shared {
} }
} }
// MARK: - Observable Object // MARK: - PEReloadEventObserver
@available(macOS 10.15, *) @available(macOS 10.15, *)
public class FileObserveProject: ObservableObject, Equatable { public class PEReloadEventObserver: ObservableObject, Equatable {
public static let shared = FileObserveProject() public static let shared = PEReloadEventObserver()
private var observation: NSKeyValueObservation?
@Published public var id = UUID().uuidString @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() { public func touch() {
id = UUID().uuidString id = UUID().uuidString