IceCubes/IceCubesApp/App/Main/IceCubesApp.swift

124 lines
4.0 KiB
Swift
Raw Normal View History

2023-10-26 06:23:00 +02:00
import Account
import AppAccount
import AVFoundation
import DesignSystem
import Env
import KeychainSwift
2023-11-01 18:58:44 +01:00
import MediaUI
2023-10-26 06:23:00 +02:00
import Network
import RevenueCat
2024-01-06 19:27:26 +01:00
import StatusKit
2023-10-26 06:23:00 +02:00
import SwiftUI
import Timeline
@main
struct IceCubesApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
@Environment(\.scenePhase) var scenePhase
@Environment(\.openWindow) var openWindow
@State var appAccountsManager = AppAccountsManager.shared
@State var currentInstance = CurrentInstance.shared
@State var currentAccount = CurrentAccount.shared
@State var userPreferences = UserPreferences.shared
@State var pushNotificationsService = PushNotificationsService.shared
@State var watcher = StreamWatcher.shared
2023-10-26 06:23:00 +02:00
@State var quickLook = QuickLook.shared
@State var theme = Theme.shared
2024-02-14 12:48:14 +01:00
2023-10-26 06:23:00 +02:00
@State var selectedTab: Tab = .timeline
@State var appRouterPath = RouterPath()
2024-02-14 12:48:14 +01:00
2023-10-26 06:23:00 +02:00
@State var isSupporter: Bool = false
var body: some Scene {
appScene
otherScenes
}
2023-11-01 18:58:44 +01:00
2023-10-26 06:23:00 +02:00
func setNewClientsInEnv(client: Client) {
currentAccount.setClient(client: client)
currentInstance.setClient(client: client)
userPreferences.setClient(client: client)
Task {
await currentInstance.fetchCurrentInstance()
watcher.setClient(client: client, instanceStreamingURL: currentInstance.instance?.urls?.streamingApi)
watcher.watch(streams: [.user, .direct])
}
}
func handleScenePhase(scenePhase: ScenePhase) {
switch scenePhase {
case .background:
watcher.stopWatching()
case .active:
watcher.watch(streams: [.user, .direct])
UNUserNotificationCenter.current().setBadgeCount(0)
userPreferences.reloadNotificationsCount(tokens: appAccountsManager.availableAccounts.compactMap(\.oauthToken))
Task {
await userPreferences.refreshServerPreferences()
}
default:
break
}
}
func setupRevenueCat() {
Purchases.logLevel = .error
Purchases.configure(withAPIKey: "appl_JXmiRckOzXXTsHKitQiicXCvMQi")
Purchases.shared.getCustomerInfo { info, _ in
if info?.entitlements["Supporter"]?.isActive == true {
isSupporter = true
}
}
}
func refreshPushSubs() {
PushNotificationsService.shared.requestPushNotifications()
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
2023-10-26 06:23:00 +02:00
func application(_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool
{
2024-01-07 17:52:28 +01:00
try? AVAudioSession.sharedInstance().setCategory(.ambient, options: .mixWithOthers)
try? AVAudioSession.sharedInstance().setActive(true)
2023-10-26 06:23:00 +02:00
PushNotificationsService.shared.setAccounts(accounts: AppAccountsManager.shared.pushAccounts)
return true
}
func application(_: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
PushNotificationsService.shared.pushToken = deviceToken
Task {
PushNotificationsService.shared.setAccounts(accounts: AppAccountsManager.shared.pushAccounts)
await PushNotificationsService.shared.updateSubscriptions(forceCreate: false)
}
}
func application(_: UIApplication, didFailToRegisterForRemoteNotificationsWithError _: Error) {}
func application(_: UIApplication, didReceiveRemoteNotification _: [AnyHashable: Any]) async -> UIBackgroundFetchResult {
UserPreferences.shared.reloadNotificationsCount(tokens: AppAccountsManager.shared.availableAccounts.compactMap(\.oauthToken))
return .noData
}
func application(_: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options _: UIScene.ConnectionOptions) -> UISceneConfiguration {
let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
if connectingSceneSession.role == .windowApplication {
configuration.delegateClass = SceneDelegate.self
}
return configuration
}
override func buildMenu(with builder: UIMenuBuilder) {
super.buildMenu(with: builder)
builder.remove(menu: .document)
builder.remove(menu: .toolbar)
builder.remove(menu: .sidebar)
}
2023-10-26 06:23:00 +02:00
}