metatext/macOS/SidebarNavigation.swift

103 lines
3.7 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
2020-08-03 05:54:47 +00:00
import KingfisherSwiftUI
import struct Kingfisher.DownsamplingImageProcessor
import struct Kingfisher.RoundCornerImageProcessor
struct SidebarNavigation: View {
2020-08-04 00:48:22 +00:00
@StateObject var viewModel: MainNavigationViewModel
2020-08-04 20:26:09 +00:00
@EnvironmentObject var rootViewModel: RootViewModel
@Environment(\.displayScale) var displayScale: CGFloat
var sidebar: some View {
2020-08-03 15:20:51 +00:00
List(selection: $viewModel.selectedTab) {
ForEach(MainNavigationViewModel.Tab.allCases) { tab in
NavigationLink(destination: view(topLevelNavigation: tab)) {
Label(tab.title, systemImage: tab.systemImageName)
}
2020-08-03 15:20:51 +00:00
.accessibility(label: Text(tab.title))
.tag(tab)
}
}
2020-08-04 20:26:09 +00:00
.overlay(Pocket()
.environmentObject(viewModel)
.environmentObject(rootViewModel),
alignment: .bottom)
.listStyle(SidebarListStyle())
2020-08-04 00:48:22 +00:00
.onAppear(perform: viewModel.refreshIdentity)
.onReceive(NotificationCenter.default
.publisher(for: NSWindow.didBecomeKeyNotification)
.dropFirst()
.map { _ in () },
perform: viewModel.refreshIdentity)
}
var body: some View {
NavigationView {
sidebar
.frame(minWidth: 100, idealWidth: 150, maxWidth: 200, maxHeight: .infinity)
Text("Content")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
private extension SidebarNavigation {
2020-08-03 15:20:51 +00:00
func view(topLevelNavigation: MainNavigationViewModel.Tab) -> some View {
Group {
switch topLevelNavigation {
case .timelines:
TimelineView()
default: Text(topLevelNavigation.title)
}
}
}
2020-08-03 05:54:47 +00:00
struct Pocket: View {
2020-08-03 15:20:51 +00:00
@EnvironmentObject var viewModel: MainNavigationViewModel
2020-08-04 20:26:09 +00:00
@EnvironmentObject var rootViewModel: RootViewModel
2020-08-03 05:54:47 +00:00
var body: some View {
VStack(alignment: .leading, spacing: 0) {
Divider()
2020-08-03 15:20:51 +00:00
Button(action: { viewModel.presentingSettings.toggle() }) {
2020-08-04 20:26:09 +00:00
KFImage(viewModel.identity.image,
2020-08-03 05:54:47 +00:00
options: [
.processor(
DownsamplingImageProcessor(size: CGSize(width: 50, height: 50))
.append(another: RoundCornerImageProcessor(radius: .widthFraction(0.5)))
),
2020-08-04 20:26:09 +00:00
.scaleFactor(displayScale),
2020-08-03 05:54:47 +00:00
.cacheOriginalImage
])
.placeholder { Image(systemName: "gear") }
.renderingMode(.original)
.resizable()
.padding(6)
.contentShape(Rectangle())
}
.frame(width: 50, height: 50)
.accessibility(label: Text("Rewards"))
.padding(.vertical, 8)
.padding(.horizontal, 16)
.buttonStyle(PlainButtonStyle())
}
2020-08-03 15:20:51 +00:00
.sheet(isPresented: $viewModel.presentingSettings) {
SettingsView(viewModel: viewModel.settingsViewModel())
.environmentObject(viewModel)
2020-08-04 20:26:09 +00:00
.environmentObject(rootViewModel)
2020-08-03 05:54:47 +00:00
}
}
}
}
2020-08-02 22:23:01 +00:00
#if DEBUG
struct SidebarNavigation_Previews: PreviewProvider {
static var previews: some View {
2020-08-04 00:48:22 +00:00
SidebarNavigation(viewModel: .development)
2020-08-04 20:26:09 +00:00
.environmentObject(RootViewModel.development)
}
}
2020-08-02 22:23:01 +00:00
#endif