vChewing-macOS/Packages/vChewing_MainAssembly/Sources/MainAssembly/AppDelegate.swift

171 lines
6.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// (c) 2011 and onwards The OpenVanilla Project (MIT License).
// All possible vChewing-specific modifications are of:
// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import AppKit
import FolderMonitor
import Shared
import Uninstaller
import UpdateSputnik
import UserNotifications
@objc(AppDelegate)
public class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate {
public static let shared = AppDelegate()
private var folderMonitor = FolderMonitor(
url: URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: false))
)
public static var updateInfoSourceURL: URL? {
guard let urlText = Bundle.main.infoDictionary?["UpdateInfoEndpoint"] as? String else {
NSLog("vChewingDebug: Fatal error: Info.plist wrecked. It needs to have correct 'UpdateInfoEndpoint' value.")
return nil
}
return .init(string: urlText)
}
public func checkUpdate(forced: Bool, shouldBypass: @escaping () -> Bool) {
guard let url = Self.updateInfoSourceURL else { return }
UpdateSputnik.shared.checkForUpdate(forced: forced, url: url) { shouldBypass() }
}
}
// MARK: - Private Functions
extension AppDelegate {
private func reloadOnFolderChangeHappens(forced: Bool = true) {
// 100ms 使使
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
// forced
// initUserLangModels()
if PrefMgr.shared.shouldAutoReloadUserDataFiles || forced { LMMgr.initUserLangModels() }
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
if PrefMgr.shared.phraseEditorAutoReloadExternalModifications {
Broadcaster.shared.eventForReloadingPhraseEditor = .init()
}
}
}
}
}
// MARK: - Public Functions
public extension AppDelegate {
func applicationWillFinishLaunching(_: Notification) {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { _, _ in })
PrefMgr.shared.fixOddPreferences()
SecurityAgentHelper.shared.timer?.fire()
SpeechSputnik.shared.refreshStatus() //
// 使
// Debug
// Debug
if PrefMgr.shared.failureFlagForUOMObservation {
LMMgr.relocateWreckedUOMData()
PrefMgr.shared.failureFlagForUOMObservation = false
let msgPackage = UNMutableNotificationContent()
msgPackage.title = NSLocalizedString("vChewing", comment: "")
msgPackage.body = NSLocalizedString(
"vChewing crashed while handling previously loaded UOM observation data. These data files are cleaned now to ensure the usability.",
comment: ""
)
msgPackage.sound = .defaultCritical
UNUserNotificationCenter.current().add(
.init(identifier: "vChewing.notification.uomCrash", content: msgPackage, trigger: nil),
withCompletionHandler: nil
)
}
LMMgr.connectCoreDB()
LMMgr.loadCassetteData()
LMMgr.initUserLangModels()
folderMonitor.folderDidChange = { [weak self] in
guard let self = self else { return }
self.reloadOnFolderChangeHappens()
}
if LMMgr.userDataFolderExists { folderMonitor.startMonitoring() }
}
func updateDirectoryMonitorPath() {
folderMonitor.stopMonitoring()
folderMonitor = FolderMonitor(
url: URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: false))
)
folderMonitor.folderDidChange = { [weak self] in
guard let self = self else { return }
self.reloadOnFolderChangeHappens()
}
if LMMgr.userDataFolderExists { // FolderMonitor
folderMonitor.startMonitoring()
reloadOnFolderChangeHappens(forced: true)
}
}
func selfUninstall() {
let content = String(
format: NSLocalizedString(
"This will remove vChewing Input Method from this user account, requiring your confirmation.",
comment: ""
))
let alert = NSAlert()
alert.messageText = NSLocalizedString("Uninstallation", comment: "")
alert.informativeText = content
alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
if #available(macOS 11, *) {
alert.buttons.forEach { button in
button.hasDestructiveAction = true
}
}
alert.addButton(withTitle: NSLocalizedString("Not Now", comment: ""))
let result = alert.runModal()
NSApp.popup()
guard result == NSApplication.ModalResponse.alertFirstButtonReturn else { return }
let url = URL(fileURLWithPath: LMMgr.dataFolderPath(isDefaultFolder: true))
FileOpenMethod.finder.open(url: url)
Uninstaller.uninstall(
selfKill: true, defaultDataFolderPath: LMMgr.dataFolderPath(isDefaultFolder: true)
)
}
///
/// - Returns: MiB
@discardableResult func checkMemoryUsage() -> Double {
guard let currentMemorySizeInBytes = NSApplication.memoryFootprint else { return 0 }
let currentMemorySize: Double = (Double(currentMemorySizeInBytes) / 1024 / 1024).rounded(toPlaces: 1)
switch currentMemorySize {
case 384...:
vCLog("WARNING: EXCESSIVE MEMORY FOOTPRINT (\(currentMemorySize)MB).")
let msgPackage = UNMutableNotificationContent()
msgPackage.title = NSLocalizedString("vChewing", comment: "")
msgPackage.body = NSLocalizedString(
"vChewing is rebooted due to a memory-excessive-usage problem. If convenient, please inform the developer that you are having this issue, stating whether you are using an Intel Mac or Apple Silicon Mac. An NSLog is generated with the current memory footprint size.",
comment: ""
)
UNUserNotificationCenter.current().add(
.init(
identifier: "vChewing.notification.memoryExcessiveUsage",
content: msgPackage, trigger: nil
),
withCompletionHandler: nil
)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
NSApp.terminate(self)
}
default: break
}
return currentMemorySize
}
}