IceCubesApp/IceCubesApp/App/Main/AppView.swift

151 lines
4.6 KiB
Swift
Raw Normal View History

import Account
import AppAccount
import AVFoundation
import DesignSystem
import Env
import KeychainSwift
import MediaUI
import Network
import RevenueCat
2024-01-06 18:27:26 +00:00
import StatusKit
import SwiftUI
import Timeline
@MainActor
struct AppView: View {
@Environment(AppAccountsManager.self) private var appAccountsManager
@Environment(UserPreferences.self) private var userPreferences
@Environment(Theme.self) private var theme
@Environment(StreamWatcher.self) private var watcher
2024-02-14 11:48:14 +00:00
2024-01-15 20:15:40 +00:00
@Environment(\.openWindow) var openWindow
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
2024-02-14 11:48:14 +00:00
@Binding var selectedTab: Tab
@Binding var appRouterPath: RouterPath
2024-02-14 11:48:14 +00:00
@State var popToRootTab: Tab = .other
@State var iosTabs = iOSTabs.shared
@State var sidebarTabs = SidebarTabs.shared
2024-02-14 11:48:14 +00:00
var body: some View {
2024-01-08 20:21:14 +00:00
#if os(visionOS)
tabBarView
2024-02-14 11:48:14 +00:00
#else
if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac {
sidebarView
} else {
tabBarView
}
2024-01-08 20:21:14 +00:00
#endif
}
2024-02-14 11:48:14 +00:00
var availableTabs: [Tab] {
guard appAccountsManager.currentClient.isAuth else {
return Tab.loggedOutTab()
}
2024-01-04 20:24:22 +00:00
if UIDevice.current.userInterfaceIdiom == .phone || horizontalSizeClass == .compact {
return iosTabs.tabs
} else if UIDevice.current.userInterfaceIdiom == .vision {
return Tab.visionOSTab()
}
2024-02-14 11:48:14 +00:00
return sidebarTabs.tabs.map { $0.tab }
}
var tabBarView: some View {
TabView(selection: .init(get: {
selectedTab
}, set: { newTab in
if newTab == .post {
2024-01-15 20:15:40 +00:00
#if os(visionOS)
2024-02-14 11:48:14 +00:00
openWindow(value: WindowDestinationEditor.newStatusEditor(visibility: userPreferences.postVisibility))
2024-01-15 20:15:40 +00:00
#else
2024-02-14 11:48:14 +00:00
appRouterPath.presentedSheet = .newStatusEditor(visibility: userPreferences.postVisibility)
2024-01-15 20:15:40 +00:00
#endif
return
}
if newTab == selectedTab {
/// Stupid hack to trigger onChange binding in tab views.
popToRootTab = .other
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
popToRootTab = selectedTab
}
}
HapticManager.shared.fireHaptic(.tabSelection)
SoundEffectManager.shared.playSound(.tabSelection)
selectedTab = newTab
})) {
ForEach(availableTabs) { tab in
tab.makeContentView(selectedTab: $selectedTab, popToRootTab: $popToRootTab)
.tabItem {
if userPreferences.showiPhoneTabLabel {
tab.label
2024-01-10 07:56:35 +00:00
.environment(\.symbolVariants, tab == selectedTab ? .fill : .none)
} else {
Image(systemName: tab.iconName)
}
}
.tag(tab)
.badge(badgeFor(tab: tab))
2024-01-23 07:51:58 +00:00
.toolbarBackground(theme.primaryBackgroundColor.opacity(0.30), for: .tabBar)
}
}
.id(appAccountsManager.currentClient.id)
.withSheetDestinations(sheetDestinations: $appRouterPath.presentedSheet)
}
private func badgeFor(tab: Tab) -> Int {
if tab == .notifications, selectedTab != tab,
let token = appAccountsManager.currentAccount.oauthToken
{
return watcher.unreadNotificationsCount + (userPreferences.notificationsCount[token] ?? 0)
}
return 0
}
2024-02-14 11:48:14 +00:00
2024-01-08 20:21:14 +00:00
#if !os(visionOS)
2024-02-14 11:48:14 +00:00
var sidebarView: some View {
SideBarView(selectedTab: $selectedTab,
popToRootTab: $popToRootTab,
tabs: availableTabs)
{
HStack(spacing: 0) {
TabView(selection: $selectedTab) {
ForEach(availableTabs) { tab in
tab
.makeContentView(selectedTab: $selectedTab, popToRootTab: $popToRootTab)
.tabItem {
tab.label
}
.tag(tab)
}
}
.introspect(.tabView, on: .iOS(.v17)) { (tabview: UITabBarController) in
tabview.tabBar.isHidden = horizontalSizeClass == .regular
tabview.customizableViewControllers = []
tabview.moreNavigationController.isNavigationBarHidden = true
}
if horizontalSizeClass == .regular,
appAccountsManager.currentClient.isAuth,
userPreferences.showiPadSecondaryColumn
{
Divider().edgesIgnoringSafeArea(.all)
notificationsSecondaryColumn
}
}
}
2024-02-14 11:48:14 +00:00
.environment(appRouterPath)
}
2024-01-08 20:21:14 +00:00
#endif
var notificationsSecondaryColumn: some View {
NotificationsTab(selectedTab: .constant(.notifications),
popToRootTab: $popToRootTab, lockedType: nil)
.environment(\.isSecondaryColumn, true)
.frame(maxWidth: .secondaryColumnWidth)
.id(appAccountsManager.currentAccount.id)
}
}