2020-07-29 23:50:30 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2020-07-31 21:40:57 +00:00
|
|
|
import KingfisherSwiftUI
|
2020-09-01 08:11:34 +00:00
|
|
|
import enum Mastodon.Timeline
|
2020-09-05 02:31:43 +00:00
|
|
|
import SwiftUI
|
2020-09-01 07:33:49 +00:00
|
|
|
import ViewModels
|
2020-07-29 23:50:30 +00:00
|
|
|
|
2020-08-08 06:01:45 +00:00
|
|
|
struct TabNavigationView: View {
|
2020-09-09 23:00:10 +00:00
|
|
|
@ObservedObject var viewModel: NavigationViewModel
|
2020-08-04 20:26:09 +00:00
|
|
|
@EnvironmentObject var rootViewModel: RootViewModel
|
|
|
|
@Environment(\.displayScale) var displayScale: CGFloat
|
2020-07-29 23:50:30 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2020-08-03 15:20:51 +00:00
|
|
|
TabView(selection: $viewModel.selectedTab) {
|
2020-09-09 05:40:49 +00:00
|
|
|
ForEach(viewModel.tabs) { tab in
|
2020-07-29 23:50:30 +00:00
|
|
|
NavigationView {
|
2020-08-03 15:20:51 +00:00
|
|
|
view(tab: tab)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
2020-07-31 21:40:57 +00:00
|
|
|
.navigationViewStyle(StackNavigationViewStyle())
|
2020-07-29 23:50:30 +00:00
|
|
|
.tabItem {
|
2020-08-03 15:20:51 +00:00
|
|
|
Label(tab.title, systemImage: tab.systemImageName)
|
|
|
|
.accessibility(label: Text(tab.title))
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
2020-08-03 15:20:51 +00:00
|
|
|
.tag(tab)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-09 22:48:56 +00:00
|
|
|
.environmentObject(viewModel.identification)
|
2020-08-08 04:08:57 +00:00
|
|
|
.sheet(isPresented: $viewModel.presentingSecondaryNavigation) {
|
2020-09-09 22:48:56 +00:00
|
|
|
SecondaryNavigationView(viewModel: viewModel)
|
2020-08-29 03:50:58 +00:00
|
|
|
.environmentObject(viewModel)
|
2020-09-09 10:12:38 +00:00
|
|
|
.environmentObject(rootViewModel)
|
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-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 06:01:45 +00:00
|
|
|
private extension TabNavigationView {
|
2020-08-05 11:48:50 +00:00
|
|
|
@ViewBuilder
|
2020-09-09 23:00:10 +00:00
|
|
|
func view(tab: NavigationViewModel.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-09-01 08:11:34 +00:00
|
|
|
.navigationBarTitle(viewModel.timeline.title, displayMode: .inline)
|
2020-08-29 00:06:09 +00:00
|
|
|
.toolbar {
|
|
|
|
ToolbarItem(placement: .principal) {
|
|
|
|
VStack {
|
2020-09-01 08:11:34 +00:00
|
|
|
Text(viewModel.timeline.title)
|
2020-08-29 00:06:09 +00:00
|
|
|
.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: {
|
2020-09-01 08:11:34 +00:00
|
|
|
Label(timeline.title,
|
2020-09-09 05:40:49 +00:00
|
|
|
systemImage: timeline.systemImageName)
|
2020-08-29 00:06:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 11:48:50 +00:00
|
|
|
} label: {
|
2020-09-09 05:40:49 +00:00
|
|
|
Image(systemName: viewModel.timeline.systemImageName)
|
2020-08-05 11:48:50 +00:00
|
|
|
})
|
|
|
|
default: Text(tab.title)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 00:06:09 +00:00
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
var secondaryNavigationButton: some View {
|
|
|
|
Button {
|
|
|
|
viewModel.presentingSecondaryNavigation.toggle()
|
|
|
|
} label: {
|
2020-09-09 22:48:56 +00:00
|
|
|
KFImage(viewModel.identification.identity.image,
|
2020-08-29 00:06:09 +00:00
|
|
|
options: .downsampled(dimension: 28, scaleFactor: displayScale))
|
|
|
|
.placeholder { Image(systemName: "gear") }
|
|
|
|
.renderingMode(.original)
|
|
|
|
.contextMenu(ContextMenu {
|
|
|
|
ForEach(viewModel.recentIdentities) { recentIdentity in
|
|
|
|
Button {
|
2020-09-09 12:05:43 +00:00
|
|
|
rootViewModel.identitySelected(id: recentIdentity.id)
|
2020-08-29 00:06:09 +00:00
|
|
|
} label: {
|
|
|
|
Label(
|
|
|
|
title: { Text(recentIdentity.handle) },
|
|
|
|
icon: {
|
|
|
|
KFImage(recentIdentity.image,
|
|
|
|
options: .downsampled(dimension: 28, scaleFactor: displayScale))
|
|
|
|
.renderingMode(.original)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 08:11:34 +00:00
|
|
|
private extension Timeline {
|
|
|
|
var title: String {
|
|
|
|
switch self {
|
2020-08-31 19:39:26 +00: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-09 05:40:49 +00:00
|
|
|
|
|
|
|
var systemImageName: String {
|
|
|
|
switch self {
|
|
|
|
case .home: return "house"
|
|
|
|
case .local: return "person.3"
|
|
|
|
case .federated: return "globe"
|
|
|
|
case .list: return "scroll"
|
|
|
|
case .tag: return "number"
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 19:39:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 23:00:10 +00:00
|
|
|
extension NavigationViewModel.Tab {
|
2020-09-01 07:33:49 +00:00
|
|
|
var title: String {
|
|
|
|
switch self {
|
|
|
|
case .timelines: return "Timelines"
|
2020-09-09 05:40:49 +00:00
|
|
|
case .explore: return "Explore"
|
2020-09-01 07:33:49 +00:00
|
|
|
case .notifications: return "Notifications"
|
|
|
|
case .messages: return "Messages"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var systemImageName: String {
|
|
|
|
switch self {
|
|
|
|
case .timelines: return "newspaper"
|
2020-09-09 05:40:49 +00:00
|
|
|
case .explore: return "magnifyingglass"
|
2020-09-01 07:33:49 +00:00
|
|
|
case .notifications: return "bell"
|
|
|
|
case .messages: return "envelope"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 21:40:57 +00:00
|
|
|
#if DEBUG
|
2020-09-01 07:33:49 +00:00
|
|
|
import PreviewViewModels
|
|
|
|
|
2020-07-29 23:50:30 +00:00
|
|
|
struct TabNavigation_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2020-09-09 23:00:10 +00:00
|
|
|
TabNavigationView(viewModel: NavigationViewModel(identification: .preview))
|
2020-09-08 02:12:38 +00:00
|
|
|
.environmentObject(Identification.preview)
|
|
|
|
.environmentObject(RootViewModel.preview)
|
2020-07-29 23:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-31 21:40:57 +00:00
|
|
|
#endif
|