metatext-app-ios-iphone-ipad/Views/TabNavigationView.swift

252 lines
9.4 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
2020-07-31 23:40:57 +02:00
import KingfisherSwiftUI
2020-09-05 04:31:43 +02:00
import SwiftUI
2020-09-01 09:33:49 +02:00
import ViewModels
2020-08-08 08:01:45 +02:00
struct TabNavigationView: View {
2020-09-10 01:00:10 +02:00
@ObservedObject var viewModel: NavigationViewModel
2020-08-04 22:26:09 +02:00
@EnvironmentObject var rootViewModel: RootViewModel
@Environment(\.displayScale) var displayScale: CGFloat
2020-10-30 08:11:24 +01:00
@State var selectedTab = NavigationViewModel.Tab.timelines
var body: some View {
2020-09-13 10:03:08 +02:00
Group {
if viewModel.identification.identity.pending {
pendingView
} else {
2020-10-30 08:11:24 +01:00
TabView(selection: $selectedTab) {
2020-09-13 10:03:08 +02:00
ForEach(viewModel.tabs) { tab in
NavigationView {
view(tab: tab)
}
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
Label(tab.title, systemImage: tab.systemImageName)
.accessibility(label: Text(tab.title))
}
.tag(tab)
2020-12-05 22:42:54 +01:00
.overlay(newStatusButton, alignment: .bottomTrailing)
2020-09-13 10:03:08 +02:00
}
}
}
}
2020-09-10 00:48:56 +02:00
.environmentObject(viewModel.identification)
2020-08-08 06:08:57 +02:00
.sheet(isPresented: $viewModel.presentingSecondaryNavigation) {
2020-09-10 00:48:56 +02:00
SecondaryNavigationView(viewModel: viewModel)
2020-08-29 05:50:58 +02:00
.environmentObject(viewModel)
2020-09-09 12:12:38 +02:00
.environmentObject(rootViewModel)
2020-07-31 23:40:57 +02:00
}
2020-12-06 04:10:27 +01:00
.background(
EmptyView()
.fullScreenCover(isPresented: $viewModel.presentingNewStatus) {
NavigationView {
2021-01-01 01:49:59 +01:00
NewStatusView { rootViewModel.newStatusViewModel(identification: viewModel.identification) }
2020-12-10 03:44:06 +01:00
.navigationBarTitleDisplayMode(.inline)
2020-12-06 04:10:27 +01:00
}
.navigationViewStyle(StackNavigationViewStyle())
.environmentObject(viewModel)
.environmentObject(rootViewModel)
})
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-08-08 08:01:45 +02:00
private extension TabNavigationView {
2020-09-13 10:03:08 +02:00
@ViewBuilder
var pendingView: some View {
NavigationView {
Text("pending.pending-confirmation")
.navigationBarItems(leading: secondaryNavigationButton)
.navigationTitle(viewModel.identification.identity.handle)
.navigationBarTitleDisplayMode(.inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
2020-08-05 13:48:50 +02:00
@ViewBuilder
2020-10-29 07:03:45 +01:00
// swiftlint:disable:next function_body_length
2020-09-10 01:00:10 +02:00
func view(tab: NavigationViewModel.Tab) -> some View {
2020-08-05 13:48:50 +02:00
switch tab {
case .timelines:
TableView { viewModel.timelineViewModel }
2020-08-29 02:06:09 +02:00
.id(viewModel.timeline.id)
2020-08-24 10:29:22 +02:00
.edgesIgnoringSafeArea(.all)
2020-09-13 10:03:08 +02:00
.navigationTitle(viewModel.timeline.title)
.navigationBarTitleDisplayMode(.inline)
2020-08-29 02:06:09 +02:00
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
2020-09-01 10:11:34 +02:00
Text(viewModel.timeline.title)
2020-08-29 02:06:09 +02:00
.font(.headline)
Text(viewModel.timelineSubtitle)
.font(.footnote)
.foregroundColor(.secondary)
}
}
}
2020-08-05 13:48:50 +02:00
.navigationBarItems(
2020-08-29 02:06:09 +02:00
leading: secondaryNavigationButton,
trailing: Menu {
ForEach(viewModel.timelinesAndLists) { timeline in
Button {
2020-08-29 05:50:58 +02:00
viewModel.timeline = timeline
2020-08-29 02:06:09 +02:00
} label: {
2020-09-01 10:11:34 +02:00
Label(timeline.title,
2020-09-09 07:40:49 +02:00
systemImage: timeline.systemImageName)
2020-08-29 02:06:09 +02:00
}
}
2020-08-05 13:48:50 +02:00
} label: {
2020-09-09 07:40:49 +02:00
Image(systemName: viewModel.timeline.systemImageName)
2020-09-16 08:34:58 +02:00
.padding([.leading, .top, .bottom])
2020-08-05 13:48:50 +02:00
})
2020-10-30 08:11:24 +01:00
case .notifications:
if let notificationsViewModel = viewModel.notificationsViewModel {
TableView { notificationsViewModel }
2020-10-30 08:11:24 +01:00
.id(tab)
.edgesIgnoringSafeArea(.all)
.navigationTitle("notifications")
.navigationBarTitleDisplayMode(.inline)
2020-10-29 07:03:45 +01:00
.navigationBarItems(leading: secondaryNavigationButton)
}
case .messages:
if let conversationsViewModel = viewModel.conversationsViewModel {
TableView { conversationsViewModel }
2020-10-29 07:03:45 +01:00
.id(tab)
.edgesIgnoringSafeArea(.all)
.navigationTitle("messages")
.navigationBarTitleDisplayMode(.inline)
2020-10-30 08:11:24 +01:00
.navigationBarItems(leading: secondaryNavigationButton)
}
2020-08-05 13:48:50 +02:00
default: Text(tab.title)
}
}
2020-08-29 02:06:09 +02:00
@ViewBuilder
var secondaryNavigationButton: some View {
Button {
viewModel.presentingSecondaryNavigation.toggle()
} label: {
2020-09-10 00:48:56 +02:00
KFImage(viewModel.identification.identity.image,
2020-12-10 03:44:06 +01:00
options: .downsampled(
dimension: .barButtonItemDimension,
scaleFactor: displayScale))
2020-08-29 02:06:09 +02:00
.placeholder { Image(systemName: "gear") }
.renderingMode(.original)
.contextMenu(ContextMenu {
ForEach(viewModel.recentIdentities) { recentIdentity in
Button {
2020-09-09 14:05:43 +02:00
rootViewModel.identitySelected(id: recentIdentity.id)
2020-08-29 02:06:09 +02:00
} label: {
Label(
title: { Text(recentIdentity.handle) },
icon: {
KFImage(recentIdentity.image,
2020-12-10 03:44:06 +01:00
options: .downsampled(
dimension: .barButtonItemDimension,
scaleFactor: displayScale))
2020-08-29 02:06:09 +02:00
.renderingMode(.original)
})
}
}
})
2020-09-16 08:34:58 +02:00
.padding([.trailing, .top, .bottom])
2020-08-29 02:06:09 +02:00
}
}
2020-12-05 22:42:54 +01:00
@ViewBuilder
var newStatusButton: some View {
if viewModel.identification.identity.authenticated
&& !viewModel.identification.identity.pending {
Button {
2020-12-06 04:10:27 +01:00
viewModel.presentingNewStatus = true
2020-12-05 22:42:54 +01:00
} label: {
2020-12-11 03:51:08 +01:00
VisualEffectBlur(vibrancyStyle: .label) {
2020-12-05 22:42:54 +01:00
Image(systemName: "pencil")
.resizable()
2020-12-11 03:51:08 +01:00
.frame(width: .newStatusButtonDimension / 2,
height: .newStatusButtonDimension / 2)
2020-12-05 22:42:54 +01:00
}
2020-12-11 03:51:08 +01:00
.clipShape(Circle())
.frame(width: .newStatusButtonDimension,
height: .newStatusButtonDimension)
.shadow(radius: .newStatusButtonShadowRadius)
2020-12-05 22:42:54 +01:00
.padding()
}
}
}
}
2020-09-01 10:11:34 +02:00
private extension Timeline {
var title: String {
switch self {
2020-08-31 21:39:26 +02:00
case .home:
return NSLocalizedString("timelines.home", comment: "")
case .local:
return NSLocalizedString("timelines.local", comment: "")
case .federated:
return NSLocalizedString("timelines.federated", comment: "")
case let .list(list):
return list.title
case let .tag(tag):
2020-12-03 23:40:33 +01:00
return "#".appending(tag)
2020-10-01 04:35:06 +02:00
case .profile:
return ""
2020-12-01 04:07:38 +01:00
case .favorites:
return NSLocalizedString("favorites", comment: "")
2020-12-01 19:40:19 +01:00
case .bookmarks:
return NSLocalizedString("bookmarks", comment: "")
2020-08-31 21:39:26 +02:00
}
}
2020-09-09 07:40:49 +02:00
var systemImageName: String {
switch self {
case .home: return "house"
case .local: return "person.3"
2020-10-06 09:40:17 +02:00
case .federated: return "network"
2020-09-09 07:40:49 +02:00
case .list: return "scroll"
case .tag: return "number"
2020-10-01 04:35:06 +02:00
case .profile: return "person"
2020-12-01 19:40:19 +01:00
case .favorites: return "star"
case .bookmarks: return "bookmark"
2020-09-09 07:40:49 +02:00
}
}
2020-08-31 21:39:26 +02:00
}
2020-09-10 01:00:10 +02:00
extension NavigationViewModel.Tab {
2020-09-01 09:33:49 +02:00
var title: String {
switch self {
case .timelines: return "Timelines"
2020-09-09 07:40:49 +02:00
case .explore: return "Explore"
2020-09-01 09:33:49 +02:00
case .notifications: return "Notifications"
case .messages: return "Messages"
}
}
var systemImageName: String {
switch self {
case .timelines: return "newspaper"
2020-09-09 07:40:49 +02:00
case .explore: return "magnifyingglass"
2020-09-01 09:33:49 +02:00
case .notifications: return "bell"
case .messages: return "envelope"
}
}
}
2020-07-31 23:40:57 +02:00
#if DEBUG
2020-09-01 09:33:49 +02:00
import PreviewViewModels
struct TabNavigation_Previews: PreviewProvider {
static var previews: some View {
2020-09-10 01:00:10 +02:00
TabNavigationView(viewModel: NavigationViewModel(identification: .preview))
2020-09-08 04:12:38 +02:00
.environmentObject(Identification.preview)
.environmentObject(RootViewModel.preview)
}
}
2020-07-31 23:40:57 +02:00
#endif