1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-18 12:28:35 +01:00
metatext-app-ios-iphone-ipad/Views/TabNavigationView.swift

153 lines
5.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-01 10:11:34 +02:00
import enum Mastodon.Timeline
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 {
@ObservedObject var viewModel: TabNavigationViewModel
2020-08-04 22:26:09 +02:00
@EnvironmentObject var rootViewModel: RootViewModel
@Environment(\.displayScale) var displayScale: CGFloat
var body: some View {
2020-08-03 17:20:51 +02:00
TabView(selection: $viewModel.selectedTab) {
2020-08-08 08:01:45 +02:00
ForEach(TabNavigationViewModel.Tab.allCases) { tab in
NavigationView {
2020-08-03 17:20:51 +02:00
view(tab: tab)
}
2020-07-31 23:40:57 +02:00
.navigationViewStyle(StackNavigationViewStyle())
.tabItem {
2020-08-03 17:20:51 +02:00
Label(tab.title, systemImage: tab.systemImageName)
.accessibility(label: Text(tab.title))
}
2020-08-03 17:20:51 +02:00
.tag(tab)
}
}
2020-08-08 06:08:57 +02:00
.sheet(isPresented: $viewModel.presentingSecondaryNavigation) {
SecondaryNavigationView(viewModel: viewModel.secondaryNavigationViewModel())
2020-08-29 05:50:58 +02:00
.environmentObject(viewModel)
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-08-08 08:01:45 +02:00
private extension TabNavigationView {
2020-08-05 13:48:50 +02:00
@ViewBuilder
2020-08-08 08:01:45 +02:00
func view(tab: TabNavigationViewModel.Tab) -> some View {
2020-08-05 13:48:50 +02:00
switch tab {
case .timelines:
2020-08-29 02:06:09 +02:00
StatusListView(viewModel: viewModel.viewModel(timeline: viewModel.timeline))
.id(viewModel.timeline.id)
2020-08-24 10:29:22 +02:00
.edgesIgnoringSafeArea(.all)
2020-09-01 10:11:34 +02:00
.navigationBarTitle(viewModel.timeline.title, displayMode: .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-08-29 02:06:09 +02:00
systemImage: viewModel.systemImageName(timeline: timeline))
}
}
2020-08-05 13:48:50 +02:00
} label: {
2020-08-29 02:06:09 +02:00
Image(systemName: viewModel.systemImageName(timeline: viewModel.timeline))
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: {
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-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):
return "#" + tag
}
}
}
2020-09-01 09:33:49 +02:00
extension TabNavigationViewModel.Tab {
var title: String {
switch self {
case .timelines: return "Timelines"
case .search: return "Search"
case .notifications: return "Notifications"
case .messages: return "Messages"
}
}
var systemImageName: String {
switch self {
case .timelines: return "newspaper"
case .search: return "magnifyingglass"
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-01 09:33:49 +02:00
TabNavigationView(viewModel: .mock())
.environmentObject(RootViewModel.mock())
}
}
2020-07-31 23:40:57 +02:00
#endif