2023-01-16 19:51:05 +01:00
|
|
|
import Account
|
2023-01-16 21:15:33 +01:00
|
|
|
import AppAccount
|
2023-01-17 11:36:01 +01:00
|
|
|
import DesignSystem
|
|
|
|
import Env
|
2023-01-25 21:18:34 +01:00
|
|
|
import Models
|
2023-01-27 20:36:40 +01:00
|
|
|
import SwiftUI
|
2023-01-16 19:51:05 +01:00
|
|
|
|
|
|
|
struct SideBarView<Content: View>: View {
|
2023-01-17 19:41:46 +01:00
|
|
|
@EnvironmentObject private var appAccounts: AppAccountsManager
|
2023-01-16 19:51:05 +01:00
|
|
|
@EnvironmentObject private var currentAccount: CurrentAccount
|
|
|
|
@EnvironmentObject private var theme: Theme
|
2023-01-17 13:02:05 +01:00
|
|
|
@EnvironmentObject private var watcher: StreamWatcher
|
|
|
|
@EnvironmentObject private var userPreferences: UserPreferences
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-01-16 19:51:05 +01:00
|
|
|
@Binding var selectedTab: Tab
|
|
|
|
@Binding var popToRootTab: Tab
|
|
|
|
var tabs: [Tab]
|
2023-01-17 13:02:05 +01:00
|
|
|
@ObservedObject var routerPath = RouterPath()
|
2023-01-16 21:15:33 +01:00
|
|
|
@ViewBuilder var content: () -> Content
|
2023-01-17 11:36:01 +01:00
|
|
|
|
2023-01-17 13:02:05 +01:00
|
|
|
private func badgeFor(tab: Tab) -> Int {
|
2023-02-18 15:36:18 +01:00
|
|
|
if tab == .notifications && selectedTab != tab,
|
2023-02-21 07:23:42 +01:00
|
|
|
let token = appAccounts.currentAccount.oauthToken
|
|
|
|
{
|
2023-02-18 15:36:18 +01:00
|
|
|
return watcher.unreadNotificationsCount + userPreferences.getNotificationsCount(for: token)
|
2023-01-17 13:02:05 +01:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2023-02-21 07:23:42 +01:00
|
|
|
|
2023-01-17 13:02:05 +01:00
|
|
|
private func makeIconForTab(tab: Tab) -> some View {
|
|
|
|
ZStack(alignment: .topTrailing) {
|
2023-01-19 11:59:25 +01:00
|
|
|
SideBarIcon(systemIconName: tab.iconName,
|
|
|
|
isSelected: tab == selectedTab)
|
2023-02-22 07:04:37 +01:00
|
|
|
let badge = badgeFor(tab: tab)
|
|
|
|
if badge > 0 {
|
2023-02-18 15:36:18 +01:00
|
|
|
makeBadgeView(count: badge)
|
2023-01-17 13:02:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
.frame(width: .sidebarWidth, height: 50)
|
|
|
|
}
|
2023-02-21 07:23:42 +01:00
|
|
|
|
2023-02-18 15:36:18 +01:00
|
|
|
private func makeBadgeView(count: Int) -> some View {
|
|
|
|
ZStack {
|
|
|
|
Circle()
|
|
|
|
.fill(.red)
|
2023-02-23 18:42:34 +01:00
|
|
|
Text(count > 99 ? "99+" : String(count))
|
2023-02-18 15:36:18 +01:00
|
|
|
.foregroundColor(.white)
|
|
|
|
.font(.caption2)
|
|
|
|
}
|
2023-02-23 18:42:34 +01:00
|
|
|
.frame(width: 24, height: 24)
|
|
|
|
.offset(x: 14, y: -14)
|
2023-02-18 15:36:18 +01:00
|
|
|
}
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-01-17 13:02:05 +01:00
|
|
|
private var postButton: some View {
|
|
|
|
Button {
|
2023-01-25 06:28:16 +01:00
|
|
|
routerPath.presentedSheet = .newStatusEditor(visibility: userPreferences.postVisibility)
|
2023-01-17 13:02:05 +01:00
|
|
|
} label: {
|
|
|
|
Image(systemName: "square.and.pencil")
|
|
|
|
.resizable()
|
|
|
|
.aspectRatio(contentMode: .fit)
|
|
|
|
.frame(width: 20, height: 30)
|
|
|
|
}
|
|
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
.keyboardShortcut("n", modifiers: .command)
|
|
|
|
}
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-02-18 15:36:18 +01:00
|
|
|
private func makeAccountButton(account: AppAccount, showBadge: Bool) -> some View {
|
2023-01-17 19:41:46 +01:00
|
|
|
Button {
|
|
|
|
if account.id == appAccounts.currentAccount.id {
|
|
|
|
selectedTab = .profile
|
2023-02-28 18:55:08 +01:00
|
|
|
SoundEffectManager.shared.playSound(of: .tabSelection)
|
2023-01-17 19:41:46 +01:00
|
|
|
} else {
|
2023-02-09 18:48:31 +01:00
|
|
|
var transation = Transaction()
|
|
|
|
transation.disablesAnimations = true
|
|
|
|
withTransaction(transation) {
|
2023-01-17 19:41:46 +01:00
|
|
|
appAccounts.currentAccount = account
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} label: {
|
2023-02-18 15:36:18 +01:00
|
|
|
ZStack(alignment: .topTrailing) {
|
|
|
|
AppAccountView(viewModel: .init(appAccount: account, isCompact: true))
|
|
|
|
if showBadge,
|
2023-02-21 07:23:42 +01:00
|
|
|
let token = account.oauthToken,
|
|
|
|
userPreferences.getNotificationsCount(for: token) > 0
|
|
|
|
{
|
2023-02-18 15:36:18 +01:00
|
|
|
makeBadgeView(count: userPreferences.getNotificationsCount(for: token))
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 19:41:46 +01:00
|
|
|
}
|
|
|
|
.frame(width: .sidebarWidth, height: 50)
|
|
|
|
.padding(.vertical, 8)
|
|
|
|
.background(selectedTab == .profile && account.id == appAccounts.currentAccount.id ?
|
2023-01-22 06:38:30 +01:00
|
|
|
theme.secondaryBackgroundColor : .clear)
|
2023-01-17 19:41:46 +01:00
|
|
|
}
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-01-17 19:41:46 +01:00
|
|
|
private var tabsView: some View {
|
|
|
|
ForEach(tabs) { tab in
|
|
|
|
Button {
|
2023-06-26 11:45:45 +02:00
|
|
|
//ensure keyboard is always dismissed when selecting a tab
|
|
|
|
hideKeyboard()
|
|
|
|
|
2023-01-17 19:41:46 +01:00
|
|
|
if tab == selectedTab {
|
|
|
|
popToRootTab = .other
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
|
|
|
|
popToRootTab = tab
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selectedTab = tab
|
2023-02-28 18:55:08 +01:00
|
|
|
SoundEffectManager.shared.playSound(of: .tabSelection)
|
2023-01-17 19:41:46 +01:00
|
|
|
if tab == .notifications {
|
2023-02-18 15:36:18 +01:00
|
|
|
if let token = appAccounts.currentAccount.oauthToken {
|
|
|
|
userPreferences.setNotification(count: 0, token: token)
|
|
|
|
}
|
2023-01-17 19:41:46 +01:00
|
|
|
watcher.unreadNotificationsCount = 0
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
makeIconForTab(tab: tab)
|
|
|
|
}
|
|
|
|
.background(tab == selectedTab ? theme.secondaryBackgroundColor : .clear)
|
|
|
|
}
|
|
|
|
}
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-01-16 19:51:05 +01:00
|
|
|
var body: some View {
|
|
|
|
HStack(spacing: 0) {
|
2023-01-17 13:02:05 +01:00
|
|
|
ScrollView {
|
|
|
|
VStack(alignment: .center) {
|
2023-01-17 19:41:46 +01:00
|
|
|
if appAccounts.availableAccounts.isEmpty {
|
|
|
|
tabsView
|
|
|
|
} else {
|
|
|
|
ForEach(appAccounts.availableAccounts) { account in
|
2023-02-18 15:36:18 +01:00
|
|
|
makeAccountButton(account: account,
|
|
|
|
showBadge: account.id != appAccounts.currentAccount.id)
|
2023-01-17 19:41:46 +01:00
|
|
|
if account.id == appAccounts.currentAccount.id {
|
|
|
|
tabsView
|
2023-01-16 19:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 13:02:05 +01:00
|
|
|
postButton
|
|
|
|
.padding(.top, 12)
|
|
|
|
Spacer()
|
2023-01-16 19:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 13:02:05 +01:00
|
|
|
.frame(width: .sidebarWidth)
|
|
|
|
.scrollContentBackground(.hidden)
|
|
|
|
.background(.thinMaterial)
|
2023-01-16 19:51:05 +01:00
|
|
|
Divider()
|
|
|
|
.edgesIgnoringSafeArea(.top)
|
2023-01-16 21:15:33 +01:00
|
|
|
content()
|
2023-01-16 19:51:05 +01:00
|
|
|
}
|
|
|
|
.background(.thinMaterial)
|
2023-01-17 13:02:05 +01:00
|
|
|
.withSheetDestinations(sheetDestinations: $routerPath.presentedSheet)
|
2023-01-16 19:51:05 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-19 11:59:25 +01:00
|
|
|
|
|
|
|
private struct SideBarIcon: View {
|
|
|
|
@EnvironmentObject private var theme: Theme
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-01-19 11:59:25 +01:00
|
|
|
let systemIconName: String
|
|
|
|
let isSelected: Bool
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-01-19 11:59:25 +01:00
|
|
|
@State private var isHovered: Bool = false
|
2023-01-22 06:38:30 +01:00
|
|
|
|
2023-01-19 11:59:25 +01:00
|
|
|
var body: some View {
|
|
|
|
Image(systemName: systemIconName)
|
2023-02-10 12:53:59 +01:00
|
|
|
.font(.title2)
|
|
|
|
.fontWeight(.medium)
|
2023-01-19 11:59:25 +01:00
|
|
|
.foregroundColor(isSelected ? theme.tintColor : theme.labelColor)
|
|
|
|
.scaleEffect(isHovered ? 0.8 : 1.0)
|
|
|
|
.onHover { isHovered in
|
|
|
|
withAnimation(.interpolatingSpring(stiffness: 300, damping: 15)) {
|
|
|
|
self.isHovered = isHovered
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-26 11:45:45 +02:00
|
|
|
|
|
|
|
extension View {
|
|
|
|
func hideKeyboard() {
|
|
|
|
let resign = #selector(UIResponder.resignFirstResponder)
|
|
|
|
UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)
|
|
|
|
}
|
|
|
|
}
|