2022-01-21 11:09:33 +00:00
|
|
|
//
|
|
|
|
|
// SimpleXApp.swift
|
|
|
|
|
// Shared
|
|
|
|
|
//
|
|
|
|
|
// Created by Evgeny Poberezkin on 17/01/2022.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
2022-02-09 22:53:06 +00:00
|
|
|
import OSLog
|
2022-05-31 07:55:13 +01:00
|
|
|
import SimpleXChat
|
2022-02-09 22:53:06 +00:00
|
|
|
|
|
|
|
|
let logger = Logger()
|
2022-01-21 11:09:33 +00:00
|
|
|
|
2022-06-09 17:29:58 +01:00
|
|
|
//let machMessenger = MachMessenger(APP_MACH_PORT, callback: receivedMachMessage)
|
|
|
|
|
//
|
|
|
|
|
//func receivedMachMessage(msgId: Int32, msg: String) -> String? {
|
|
|
|
|
// logger.debug("MachMessenger: receivedMachMessage from FPS")
|
|
|
|
|
//// return "reply from App to: \(msg)"
|
|
|
|
|
//
|
|
|
|
|
// if let data = msg.data(using: .utf8) {
|
|
|
|
|
// logger.debug("receivedMachMessage has data")
|
|
|
|
|
// let endpoint = try! NSKeyedUnarchiver.unarchivedObject(ofClass: NSXPCListenerEndpoint.self, from: data)!
|
|
|
|
|
// logger.debug("receivedMachMessage has endpoint")
|
|
|
|
|
// let connection = NSXPCConnection(listenerEndpoint: endpoint)
|
|
|
|
|
// logger.debug("receivedMachMessage has connection")
|
|
|
|
|
// connection.remoteObjectInterface = NSXPCInterface(with: SimpleXFPServiceProtocol.self)
|
|
|
|
|
//
|
|
|
|
|
// // Start the connection.
|
|
|
|
|
// connection.resume()
|
|
|
|
|
//
|
|
|
|
|
// // Get the proxy object.
|
|
|
|
|
// let rawProxy = connection.remoteObjectProxyWithErrorHandler({ (errorAccessingRemoteObject) in
|
|
|
|
|
// // Handle the error here...
|
|
|
|
|
// })
|
|
|
|
|
//
|
|
|
|
|
// // Cast the proxy object to the interface's protocol.
|
|
|
|
|
// guard let proxy = rawProxy as? SimpleXFPServiceProtocol else {
|
|
|
|
|
// // If the interface is set up properly, this should never fail.
|
|
|
|
|
// fatalError("*** Unable to cast \(rawProxy) to a DesiredProtocol instance ***")
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// logger.debug("receivedMachMessage calling service")
|
|
|
|
|
// proxy.upperCaseString("hello to service", withReply: { reply in
|
|
|
|
|
// logger.debug("receivedMachMessage reply from service \(reply)")
|
|
|
|
|
// })
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// return nil
|
|
|
|
|
//}
|
2022-06-02 13:16:22 +01:00
|
|
|
|
2022-01-21 11:09:33 +00:00
|
|
|
@main
|
|
|
|
|
struct SimpleXApp: App {
|
2022-04-21 20:04:22 +01:00
|
|
|
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
2022-02-09 22:53:06 +00:00
|
|
|
@StateObject private var chatModel = ChatModel.shared
|
2022-05-28 14:58:52 +04:00
|
|
|
@ObservedObject var alertManager = AlertManager.shared
|
2022-02-09 22:53:06 +00:00
|
|
|
@Environment(\.scenePhase) var scenePhase
|
2022-05-28 22:09:46 +04:00
|
|
|
@AppStorage(DEFAULT_PERFORM_LA) private var prefPerformLA = false
|
2022-06-03 09:16:07 +01:00
|
|
|
@State private var userAuthorized: Bool?
|
|
|
|
|
@State private var doAuthenticate = false
|
2022-05-28 14:58:52 +04:00
|
|
|
@State private var enteredBackground: Double? = nil
|
2022-02-09 22:53:06 +00:00
|
|
|
|
2022-01-22 17:54:22 +00:00
|
|
|
init() {
|
|
|
|
|
hs_init(0, nil)
|
2022-05-27 16:36:33 +01:00
|
|
|
UserDefaults.standard.register(defaults: appDefaults)
|
2022-02-09 22:53:06 +00:00
|
|
|
BGManager.shared.register()
|
|
|
|
|
NtfManager.shared.registerCategories()
|
2022-06-09 17:29:58 +01:00
|
|
|
// machMessenger.start()
|
2022-06-02 11:06:45 +01:00
|
|
|
|
|
|
|
|
// test service comms
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
|
|
|
|
|
testFPService()
|
|
|
|
|
}
|
2022-01-22 17:54:22 +00:00
|
|
|
}
|
2022-01-29 11:10:04 +00:00
|
|
|
|
2022-01-21 11:09:33 +00:00
|
|
|
var body: some Scene {
|
2022-02-09 22:53:06 +00:00
|
|
|
return WindowGroup {
|
2022-06-03 12:24:50 +01:00
|
|
|
ContentView(doAuthenticate: $doAuthenticate, userAuthorized: $userAuthorized)
|
2022-01-29 11:10:04 +00:00
|
|
|
.environmentObject(chatModel)
|
2022-02-01 20:30:33 +00:00
|
|
|
.onOpenURL { url in
|
2022-02-09 22:53:06 +00:00
|
|
|
logger.debug("ContentView.onOpenURL: \(url)")
|
2022-02-01 20:30:33 +00:00
|
|
|
chatModel.appOpenUrl = url
|
|
|
|
|
}
|
2022-01-29 11:10:04 +00:00
|
|
|
.onAppear() {
|
2022-02-09 22:53:06 +00:00
|
|
|
initializeChat()
|
|
|
|
|
}
|
|
|
|
|
.onChange(of: scenePhase) { phase in
|
2022-05-03 08:20:19 +01:00
|
|
|
logger.debug("scenePhase \(String(describing: scenePhase))")
|
2022-06-09 17:29:58 +01:00
|
|
|
// let res = machMessenger.sendMessageWithReply(NSE_MACH_PORT, msg: "App scenePhase changed to \(String(describing: scenePhase))")
|
|
|
|
|
// logger.debug("MachMessenger \(String(describing: res), privacy: .public)")
|
2022-05-03 08:20:19 +01:00
|
|
|
setAppState(phase)
|
2022-05-27 18:21:35 +04:00
|
|
|
switch (phase) {
|
|
|
|
|
case .background:
|
2022-02-09 22:53:06 +00:00
|
|
|
BGManager.shared.schedule()
|
2022-06-03 12:24:50 +01:00
|
|
|
if userAuthorized == true {
|
|
|
|
|
enteredBackground = ProcessInfo.processInfo.systemUptime
|
|
|
|
|
}
|
2022-05-28 22:09:46 +04:00
|
|
|
doAuthenticate = false
|
2022-06-09 17:29:58 +01:00
|
|
|
// machMessenger.stop()
|
2022-05-27 18:21:35 +04:00
|
|
|
case .active:
|
2022-06-03 09:16:07 +01:00
|
|
|
doAuthenticate = authenticationExpired()
|
2022-06-09 17:29:58 +01:00
|
|
|
// machMessenger.start()
|
2022-05-27 18:21:35 +04:00
|
|
|
default:
|
|
|
|
|
break
|
2022-02-06 18:26:22 +00:00
|
|
|
}
|
2022-01-29 11:10:04 +00:00
|
|
|
}
|
2022-01-21 11:09:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-06-03 09:16:07 +01:00
|
|
|
|
|
|
|
|
private func authenticationExpired() -> Bool {
|
|
|
|
|
if let enteredBackground = enteredBackground {
|
|
|
|
|
return ProcessInfo.processInfo.systemUptime - enteredBackground >= 30
|
|
|
|
|
} else {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-21 11:09:33 +00:00
|
|
|
}
|