metatext/System/MetatextApp.swift

61 lines
2.4 KiB
Swift
Raw Normal View History

2020-07-19 02:12:32 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2020-07-19 02:06:12 +00:00
2021-01-30 01:14:22 +00:00
import AVKit
2021-03-12 23:25:16 +00:00
import Combine
import GRDB
2021-01-30 01:14:22 +00:00
import ServiceLayer
2020-07-19 02:06:12 +00:00
import SwiftUI
2020-09-01 07:33:49 +00:00
import ViewModels
2020-07-19 02:06:12 +00:00
@main
struct MetatextApp: App {
2020-08-12 07:24:39 +00:00
@UIApplicationDelegateAdaptor(AppDelegate.self) private var appDelegate
2021-03-12 23:25:16 +00:00
private var cancellables = Set<AnyCancellable>()
2021-01-30 01:14:22 +00:00
init() {
try? AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default)
2021-02-04 08:30:37 +00:00
try? ImageCacheConfiguration(environment: Self.environment).configure()
2021-03-12 23:25:16 +00:00
// swiftlint:disable:next line_length
// https://github.com/groue/GRDB.swift/blob/master/Documentation/SharingADatabase.md#how-to-limit-the-0xdead10cc-exception
// This would ideally be accomplished with `@Environment(\.scenePhase) private var scenePhase`
// and `.onChange(of: scenePhase)` on the `WindowGroup`, but that does not give an accurate
// aggregate scene activation state for iPad multitasking as of iOS 14.4.1
Publishers.MergeMany([UIScene.willConnectNotification,
UIScene.didDisconnectNotification,
UIScene.didActivateNotification,
UIScene.willDeactivateNotification,
UIScene.willEnterForegroundNotification,
UIScene.didEnterBackgroundNotification]
.map { NotificationCenter.default.publisher(for: $0) })
.map { _ in
UIApplication.shared.openSessions
.compactMap(\.scene)
.allSatisfy { $0.activationState == .background }
}
.removeDuplicates()
.sink {
NotificationCenter.default.post(
name: $0 ? Database.suspendNotification : Database.resumeNotification,
object: nil)
}
.store(in: &cancellables)
2021-01-30 01:14:22 +00:00
}
2020-08-12 07:24:39 +00:00
2020-07-19 02:06:12 +00:00
var body: some Scene {
2021-03-05 02:13:30 +00:00
WindowGroup {
// swiftlint:disable:next force_try
RootView(viewModel: try! RootViewModel(
environment: Self.environment,
registerForRemoteNotifications: appDelegate.registerForRemoteNotifications))
2020-07-19 02:06:12 +00:00
}
}
}
2021-01-30 01:14:22 +00:00
private extension MetatextApp {
2021-02-04 08:30:37 +00:00
static let environment = AppEnvironment.live(
userNotificationCenter: .current(),
2021-03-06 02:25:18 +00:00
reduceMotion: { UIAccessibility.isReduceMotionEnabled },
autoplayVideos: { UIAccessibility.isVideoAutoplayEnabled })
2021-01-30 01:14:22 +00:00
}