2020-07-30 01:50:30 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import SwiftUI
|
2020-07-31 23:40:57 +02:00
|
|
|
import KingfisherSwiftUI
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
struct TabNavigation: View {
|
2020-08-04 22:26:09 +02:00
|
|
|
@ObservedObject var viewModel: MainNavigationViewModel
|
|
|
|
@EnvironmentObject var rootViewModel: RootViewModel
|
|
|
|
@Environment(\.displayScale) var displayScale: CGFloat
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
var body: some View {
|
2020-08-03 17:20:51 +02:00
|
|
|
TabView(selection: $viewModel.selectedTab) {
|
|
|
|
ForEach(MainNavigationViewModel.Tab.allCases) { tab in
|
2020-07-30 01:50:30 +02:00
|
|
|
NavigationView {
|
2020-08-03 17:20:51 +02:00
|
|
|
view(tab: tab)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
2020-07-31 23:40:57 +02:00
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
2020-07-30 01:50:30 +02:00
|
|
|
.tabItem {
|
2020-08-03 17:20:51 +02:00
|
|
|
Label(tab.title, systemImage: tab.systemImageName)
|
|
|
|
.accessibility(label: Text(tab.title))
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
2020-08-03 17:20:51 +02:00
|
|
|
.tag(tab)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-03 17:20:51 +02:00
|
|
|
.sheet(isPresented: $viewModel.presentingSettings) {
|
|
|
|
SettingsView(viewModel: viewModel.settingsViewModel())
|
2020-08-04 22:26:09 +02:00
|
|
|
.environmentObject(rootViewModel)
|
2020-07-31 23:40:57 +02:00
|
|
|
}
|
2020-08-07 03:41:59 +02:00
|
|
|
.alertItem($viewModel.alertItem)
|
|
|
|
.onAppear(perform: viewModel.refreshIdentity)
|
2020-08-04 02:48:22 +02:00
|
|
|
.onReceive(NotificationCenter.default
|
|
|
|
.publisher(for: UIScene.willEnterForegroundNotification)
|
|
|
|
.map { _ in () },
|
|
|
|
perform: viewModel.refreshIdentity)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension TabNavigation {
|
2020-08-05 13:48:50 +02:00
|
|
|
@ViewBuilder
|
2020-08-03 17:20:51 +02:00
|
|
|
func view(tab: MainNavigationViewModel.Tab) -> some View {
|
2020-08-05 13:48:50 +02:00
|
|
|
switch tab {
|
|
|
|
case .timelines:
|
|
|
|
TimelineView()
|
|
|
|
.navigationBarTitle(viewModel.identity.handle, displayMode: .inline)
|
|
|
|
.navigationBarItems(
|
|
|
|
leading: Button {
|
|
|
|
viewModel.presentingSettings.toggle()
|
|
|
|
} label: {
|
|
|
|
KFImage(viewModel.identity.image,
|
|
|
|
options: .downsampled(dimension: 28, scaleFactor: displayScale))
|
|
|
|
.placeholder { Image(systemName: "gear") }
|
|
|
|
.renderingMode(.original)
|
|
|
|
.contextMenu(ContextMenu {
|
|
|
|
ForEach(viewModel.recentIdentities) { recentIdentity in
|
|
|
|
Button {
|
|
|
|
rootViewModel.newIdentitySelected(id: recentIdentity.id)
|
|
|
|
} label: {
|
|
|
|
Label(
|
|
|
|
title: { Text(recentIdentity.handle) },
|
|
|
|
icon: {
|
|
|
|
KFImage(recentIdentity.image,
|
|
|
|
options: .downsampled(dimension: 28, scaleFactor: displayScale))
|
|
|
|
.renderingMode(.original)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
default: Text(tab.title)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 23:40:57 +02:00
|
|
|
#if DEBUG
|
2020-07-30 01:50:30 +02:00
|
|
|
struct TabNavigation_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2020-08-04 02:48:22 +02:00
|
|
|
TabNavigation(viewModel: .development)
|
2020-08-05 13:48:50 +02:00
|
|
|
.environmentObject(RootViewModel.development)
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 23:40:57 +02:00
|
|
|
#endif
|