Files
omnivore/apple/OmnivoreKit/Sources/App/Views/PrimaryContentView.swift
2023-09-12 13:06:50 +08:00

118 lines
2.9 KiB
Swift

import Models
import Services
import SwiftUI
import Views
@MainActor public struct PrimaryContentView: View {
let categories = [
PrimaryContentCategory.feed,
PrimaryContentCategory.profile
]
public var body: some View {
innerBody
}
public var innerBody: some View {
#if os(iOS)
if UIDevice.isIPad {
return AnyView(splitView)
} else {
return AnyView(LibraryTabView())
// .navigationViewStyle(.stack)
// .navigationBarTitleDisplayMode(.inline)
}
#else
return AnyView(splitView)
#endif
}
#if os(macOS)
private var splitView: some View {
NavigationView {
PrimaryContentCategory.feed.destinationView
Text(LocalText.navigationSelectLink)
}
.accentColor(.appGrayTextContrast)
}
#endif
#if os(iOS)
private var splitView: some View {
NavigationView {
// The first column is the sidebar.
PrimaryContentSidebar(categories: categories)
// Second column is the Primary Nav Stack
PrimaryContentCategory.feed.destinationView
}
.navigationBarTitleDisplayMode(.inline)
.accentColor(.appGrayTextContrast)
.introspectSplitViewController {
$0.preferredSplitBehavior = .tile
$0.preferredPrimaryColumnWidth = 160
$0.presentsWithGesture = false
$0.displayModeButtonVisibility = .always
}
}
#endif
}
@MainActor struct PrimaryContentSidebar: View {
@State private var addLinkPresented = false
@State private var selectedCategory: PrimaryContentCategory?
let categories: [PrimaryContentCategory]
var innerBody: some View {
List {
ForEach(categories, id: \.self) { category in
NavigationLink(
destination: category.destinationView,
tag: category,
selection: $selectedCategory,
label: { category.listLabel }
)
#if os(iOS)
.listRowBackground(
category == selectedCategory
? Color.appGraySolid.opacity(0.4).cornerRadius(8)
: Color.clear.cornerRadius(8)
)
#endif
}
Button(action: { addLinkPresented = true }, label: {
Label("Add Link", systemImage: "plus.circle")
})
}
.dynamicTypeSize(.small ... .large)
.listStyle(.sidebar)
.sheet(isPresented: $addLinkPresented) {
NavigationView {
LibraryAddLinkView()
}
}
}
var body: some View {
#if os(iOS)
innerBody
#elseif os(macOS)
innerBody
.frame(minWidth: 200)
.toolbar {
ToolbarItem {
Button(
action: {
NSApp.keyWindow?.firstResponder?.tryToPerform(
#selector(NSSplitViewController.toggleSidebar(_:)), with: nil
)
},
label: { Label(LocalText.navigationSelectSidebarToggle, systemImage: "sidebar.left") }
)
}
}
#endif
}
}