IceCubes/IceCubesApp/App/IceCubesApp.swift

243 lines
7.5 KiB
Swift
Raw Normal View History

2023-01-17 11:36:01 +01:00
import Account
import AppAccount
2023-01-09 18:52:53 +01:00
import AVFoundation
2022-12-24 14:55:04 +01:00
import DesignSystem
2023-01-17 11:36:01 +01:00
import Env
import KeychainSwift
import Network
2023-01-07 13:44:13 +01:00
import RevenueCat
2023-01-17 11:36:01 +01:00
import SwiftUI
import Timeline
2022-12-01 09:05:26 +01:00
@main
2023-01-17 11:36:01 +01:00
struct IceCubesApp: App {
2023-01-08 10:22:52 +01:00
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
@Environment(\.scenePhase) private var scenePhase
2023-01-08 10:22:52 +01:00
@StateObject private var appAccountsManager = AppAccountsManager.shared
@StateObject private var currentInstance = CurrentInstance.shared
@StateObject private var currentAccount = CurrentAccount.shared
@StateObject private var userPreferences = UserPreferences.shared
@StateObject private var watcher = StreamWatcher()
2022-12-22 10:53:36 +01:00
@StateObject private var quickLook = QuickLook()
@StateObject private var theme = Theme.shared
@StateObject private var sidebarRouterPath = RouterPath()
2023-01-04 12:55:09 +01:00
@State private var selectedTab: Tab = .timeline
@State private var selectSidebarItem: Tab? = .timeline
2022-12-24 11:50:05 +01:00
@State private var popToRootTab: Tab = .other
@State private var sideBarLoadedTabs: Set<Tab> = Set()
private let feedbackGenerator = UISelectionFeedbackGenerator()
private var availableTabs: [Tab] {
appAccountsManager.currentClient.isAuth ? Tab.loggedInTabs() : Tab.loggedOutTab()
}
2022-12-01 09:05:26 +01:00
var body: some Scene {
WindowGroup {
appView
2023-01-17 11:36:01 +01:00
.applyTheme(theme)
.onAppear {
setNewClientsInEnv(client: appAccountsManager.currentClient)
setupRevenueCat()
refreshPushSubs()
}
.environmentObject(appAccountsManager)
.environmentObject(appAccountsManager.currentClient)
.environmentObject(quickLook)
.environmentObject(currentAccount)
.environmentObject(currentInstance)
.environmentObject(userPreferences)
.environmentObject(theme)
.environmentObject(watcher)
.environmentObject(PushNotificationsService.shared)
.sheet(item: $quickLook.url, content: { url in
QuickLookPreview(selectedURL: url, urls: quickLook.urls)
.edgesIgnoringSafeArea(.bottom)
})
2022-12-01 09:05:26 +01:00
}
.commands {
2023-01-18 08:27:42 +01:00
appMenu
}
.onChange(of: scenePhase) { scenePhase in
2022-12-25 17:39:12 +01:00
handleScenePhase(scenePhase: scenePhase)
}
2022-12-29 10:39:34 +01:00
.onChange(of: appAccountsManager.currentClient) { newClient in
setNewClientsInEnv(client: newClient)
if newClient.isAuth {
watcher.watch(streams: [.user, .direct])
2022-12-29 10:39:34 +01:00
}
}
2022-12-01 09:05:26 +01:00
}
@ViewBuilder
private var appView: some View {
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
2023-01-16 14:40:23 +01:00
sidebarView
} else {
tabBarView
}
}
private func badgeFor(tab: Tab) -> Int {
if tab == .notifications && selectedTab != tab {
2023-01-09 18:52:53 +01:00
return watcher.unreadNotificationsCount + userPreferences.pushNotificationsCount
}
return 0
}
2023-01-16 14:40:23 +01:00
private var sidebarView: some View {
2023-01-16 19:51:05 +01:00
SideBarView(selectedTab: $selectedTab,
popToRootTab: $popToRootTab,
tabs: availableTabs,
routerPath: sidebarRouterPath) {
2023-01-16 19:51:05 +01:00
ZStack {
if selectedTab == .profile {
ProfileTab(popToRootTab: $popToRootTab)
2023-01-16 21:15:33 +01:00
}
2023-01-16 14:40:23 +01:00
ForEach(availableTabs) { tab in
2023-01-16 19:51:05 +01:00
if tab == selectedTab || sideBarLoadedTabs.contains(tab) {
tab
.makeContentView(popToRootTab: $popToRootTab)
.opacity(tab == selectedTab ? 1 : 0)
.transition(.opacity)
.id("\(tab)\(appAccountsManager.currentAccount.id)")
2023-01-16 19:51:05 +01:00
.onAppear {
sideBarLoadedTabs.insert(tab)
2023-01-16 14:40:23 +01:00
}
2023-01-16 19:51:05 +01:00
} else {
EmptyView()
2023-01-16 14:40:23 +01:00
}
}
}
}.onChange(of: $appAccountsManager.currentAccount.id) { _ in
sideBarLoadedTabs.removeAll()
2023-01-16 14:40:23 +01:00
}
}
private var tabBarView: some View {
TabView(selection: .init(get: {
selectedTab
}, set: { newTab in
if newTab == selectedTab {
/// Stupid hack to trigger onChange binding in tab views.
popToRootTab = .other
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
2023-01-04 12:55:09 +01:00
popToRootTab = selectedTab
}
}
selectedTab = newTab
feedbackGenerator.selectionChanged()
})) {
ForEach(availableTabs) { tab in
tab.makeContentView(popToRootTab: $popToRootTab)
.tabItem {
tab.label
}
.tag(tab)
.badge(badgeFor(tab: tab))
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.50), for: .tabBar)
}
}
}
2022-12-25 17:39:12 +01:00
private func setNewClientsInEnv(client: Client) {
currentAccount.setClient(client: client)
2023-01-01 18:31:23 +01:00
currentInstance.setClient(client: client)
2023-01-09 19:47:54 +01:00
userPreferences.setClient(client: client)
2022-12-25 17:39:12 +01:00
watcher.setClient(client: client)
}
2022-12-25 17:39:12 +01:00
private func handleScenePhase(scenePhase: ScenePhase) {
switch scenePhase {
case .background:
watcher.stopWatching()
case .active:
watcher.watch(streams: [.user, .direct])
2023-01-09 18:52:53 +01:00
UIApplication.shared.applicationIconBadgeNumber = 0
2023-01-09 19:47:54 +01:00
Task {
await userPreferences.refreshServerPreferences()
}
2022-12-25 17:39:12 +01:00
case .inactive:
break
default:
break
}
}
2023-01-07 13:44:13 +01:00
private func setupRevenueCat() {
Purchases.logLevel = .error
Purchases.configure(withAPIKey: "appl_JXmiRckOzXXTsHKitQiicXCvMQi")
}
2023-01-08 10:22:52 +01:00
private func refreshPushSubs() {
2023-01-08 14:16:43 +01:00
PushNotificationsService.shared.requestPushNotifications()
2023-01-08 10:22:52 +01:00
}
2023-01-18 08:27:42 +01:00
@CommandsBuilder
private var appMenu: some Commands {
CommandGroup(replacing: .newItem) {
Button("New post") {
sidebarRouterPath.presentedSheet = .newStatusEditor(visibility: userPreferences.postVisibility)
2023-01-18 08:27:42 +01:00
}
}
CommandGroup(replacing: .textFormatting) {
Menu("Font") {
Button("Bigger") {
if userPreferences.fontSizeScale < 1.5 {
userPreferences.fontSizeScale += 0.1
}
}
Button("Smaller") {
if userPreferences.fontSizeScale > 0.5 {
userPreferences.fontSizeScale -= 0.1
}
}
}
}
}
2023-01-08 10:22:52 +01:00
}
class AppDelegate: NSObject, UIApplicationDelegate {
let themeObserver = ThemeObserverViewController(nibName: nil, bundle: nil)
2023-01-17 11:36:01 +01:00
func application(_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool
{
2023-01-09 18:52:53 +01:00
try? AVAudioSession.sharedInstance().setCategory(.ambient, options: .mixWithOthers)
2023-01-08 10:22:52 +01:00
return true
}
2023-01-17 11:36:01 +01:00
func application(_: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
2023-01-08 14:16:43 +01:00
PushNotificationsService.shared.pushToken = deviceToken
#if !DEBUG
Task {
await PushNotificationsService.shared.fetchSubscriptions(accounts: AppAccountsManager.shared.pushAccounts)
await PushNotificationsService.shared.updateSubscriptions(accounts: AppAccountsManager.shared.pushAccounts)
}
#endif
2023-01-08 10:22:52 +01:00
}
2023-01-17 11:36:01 +01:00
func application(_: UIApplication, didFailToRegisterForRemoteNotificationsWithError _: Error) {}
func application(_ 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
}
2022-12-01 09:05:26 +01:00
}
class ThemeObserverViewController: UIViewController {
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
print(traitCollection.userInterfaceStyle.rawValue)
}
}