metatext/iOS/TabNavigation.swift

72 lines
2.5 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
2020-07-31 21:40:57 +00:00
import KingfisherSwiftUI
import struct Kingfisher.DownsamplingImageProcessor
struct TabNavigation: View {
2020-08-04 00:48:22 +00:00
@StateObject var viewModel: MainNavigationViewModel
var body: some View {
2020-08-03 15:20:51 +00:00
TabView(selection: $viewModel.selectedTab) {
ForEach(MainNavigationViewModel.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-03 15:20:51 +00:00
.sheet(isPresented: $viewModel.presentingSettings) {
SettingsView(viewModel: viewModel.settingsViewModel())
.environmentObject(viewModel)
2020-07-31 21:40:57 +00:00
}
2020-08-04 00:48:22 +00:00
.onAppear(perform: viewModel.refreshIdentity)
.onReceive(NotificationCenter.default
.publisher(for: UIScene.willEnterForegroundNotification)
.map { _ in () },
perform: viewModel.refreshIdentity)
}
}
private extension TabNavigation {
2020-08-03 15:20:51 +00:00
func view(tab: MainNavigationViewModel.Tab) -> some View {
Group {
2020-08-03 15:20:51 +00:00
switch tab {
case .timelines:
TimelineView()
2020-08-03 15:20:51 +00:00
.navigationBarTitle(viewModel.handle, displayMode: .inline)
2020-07-31 21:40:57 +00:00
.navigationBarItems(
leading: Button {
2020-08-03 15:20:51 +00:00
viewModel.presentingSettings.toggle()
2020-07-31 21:40:57 +00:00
} label: {
2020-08-03 15:20:51 +00:00
KFImage(viewModel.image,
2020-07-31 21:40:57 +00:00
options: [
.processor(
DownsamplingImageProcessor(size: CGSize(width: 28, height: 28))
),
2020-08-02 00:15:45 +00:00
.scaleFactor(Screen.scale),
2020-07-31 21:40:57 +00:00
.cacheOriginalImage
])
.placeholder { Image(systemName: "gear") }
.renderingMode(.original)
2020-08-03 15:20:51 +00:00
.clipShape(Circle())
2020-07-31 21:40:57 +00:00
})
2020-08-03 15:20:51 +00:00
default: Text(tab.title)
}
}
}
}
2020-07-31 21:40:57 +00:00
#if DEBUG
struct TabNavigation_Previews: PreviewProvider {
static var previews: some View {
2020-08-04 00:48:22 +00:00
TabNavigation(viewModel: .development)
}
}
2020-07-31 21:40:57 +00:00
#endif