metatext/Views/TabNavigationView.swift

112 lines
4.4 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
2020-07-31 21:40:57 +00:00
import KingfisherSwiftUI
2020-08-08 06:01:45 +00:00
struct TabNavigationView: View {
@ObservedObject var viewModel: TabNavigationViewModel
2020-08-04 20:26:09 +00:00
@EnvironmentObject var rootViewModel: RootViewModel
@Environment(\.displayScale) var displayScale: CGFloat
var body: some View {
2020-08-03 15:20:51 +00:00
TabView(selection: $viewModel.selectedTab) {
2020-08-08 06:01:45 +00:00
ForEach(TabNavigationViewModel.Tab.allCases) { tab in
NavigationView {
2020-08-03 15:20:51 +00:00
view(tab: tab)
}
2020-07-31 21:40:57 +00:00
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
2020-08-03 15:20:51 +00:00
Label(tab.title, systemImage: tab.systemImageName)
.accessibility(label: Text(tab.title))
}
2020-08-03 15:20:51 +00:00
.tag(tab)
}
}
2020-08-08 04:08:57 +00:00
.sheet(isPresented: $viewModel.presentingSecondaryNavigation) {
SecondaryNavigationView(viewModel: viewModel.secondaryNavigationViewModel())
2020-08-29 03:50:58 +00:00
.environmentObject(viewModel)
2020-07-31 21:40:57 +00:00
}
2020-08-07 01:41:59 +00:00
.alertItem($viewModel.alertItem)
.onAppear(perform: viewModel.refreshIdentity)
2020-08-04 00:48:22 +00:00
.onReceive(NotificationCenter.default
.publisher(for: UIScene.willEnterForegroundNotification)
.map { _ in () },
perform: viewModel.refreshIdentity)
}
}
2020-08-08 06:01:45 +00:00
private extension TabNavigationView {
2020-08-05 11:48:50 +00:00
@ViewBuilder
2020-08-08 06:01:45 +00:00
func view(tab: TabNavigationViewModel.Tab) -> some View {
2020-08-05 11:48:50 +00:00
switch tab {
case .timelines:
2020-08-29 00:06:09 +00:00
StatusListView(viewModel: viewModel.viewModel(timeline: viewModel.timeline))
.id(viewModel.timeline.id)
2020-08-24 08:29:22 +00:00
.edgesIgnoringSafeArea(.all)
2020-08-29 00:06:09 +00:00
.navigationBarTitle(viewModel.title(timeline: viewModel.timeline), displayMode: .inline)
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text(viewModel.title(timeline: viewModel.timeline))
.font(.headline)
Text(viewModel.timelineSubtitle)
.font(.footnote)
.foregroundColor(.secondary)
}
}
}
2020-08-05 11:48:50 +00:00
.navigationBarItems(
2020-08-29 00:06:09 +00:00
leading: secondaryNavigationButton,
trailing: Menu {
ForEach(viewModel.timelinesAndLists) { timeline in
Button {
2020-08-29 03:50:58 +00:00
viewModel.timeline = timeline
2020-08-29 00:06:09 +00:00
} label: {
Label(viewModel.title(timeline: timeline),
systemImage: viewModel.systemImageName(timeline: timeline))
}
}
2020-08-05 11:48:50 +00:00
} label: {
2020-08-29 00:06:09 +00:00
Image(systemName: viewModel.systemImageName(timeline: viewModel.timeline))
2020-08-05 11:48:50 +00:00
})
default: Text(tab.title)
}
}
2020-08-29 00:06:09 +00:00
@ViewBuilder
var secondaryNavigationButton: some View {
Button {
viewModel.presentingSecondaryNavigation.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)
})
}
}
})
}
}
}
2020-07-31 21:40:57 +00:00
#if DEBUG
struct TabNavigation_Previews: PreviewProvider {
static var previews: some View {
2020-08-08 06:01:45 +00:00
TabNavigationView(viewModel: .development)
2020-08-05 11:48:50 +00:00
.environmentObject(RootViewModel.development)
}
}
2020-07-31 21:40:57 +00:00
#endif