AutoCat2/AutoCat2SUI/Screens/Main/MainView.swift
2023-01-11 00:47:25 +03:00

137 lines
4.3 KiB
Swift

//
// MainView.swift
// AutoCat2SUI
//
// Created by Selim Mustafaev on 24.07.2022.
//
import SwiftUI
import AutoCatCore
struct SidebarSection: Identifiable {
let name: String
let filters: [Filter]
let id = UUID()
func contains(filter id: UUID) -> Bool {
filters.contains { $0.id == id }
}
}
struct MainView: View {
@FetchRequest(entity: CDVehicle.entity(), sortDescriptors: []) var vehicles: FetchedResults<CDVehicle>
@State private var selectedFilterId: UUID?
@State var selectedVehicle: CDVehicle?
@State private var checkSheetPresented = false
@State private var searchText = ""
@State var columnVisibility = NavigationSplitViewVisibility.all
private let sections: [SidebarSection] = [
SidebarSection(name: "History", filters: [
.allLocal,
.unrecognized,
.outdated
]),
SidebarSection(name: "Remote", filters: [
.allRemote
])
]
var selectedFilter: Filter? {
guard let filterId = selectedFilterId else {
return nil
}
return sections.first(where: { $0.contains(filter: filterId) })?
.filters.first(where: { $0.id == filterId })
}
var body: some View {
NavigationSplitView(columnVisibility: $columnVisibility) {
List(selection: $selectedFilterId) {
ForEach(sections) { section in
Section(section.name) {
ForEach(section.filters) { filter in
Label(filter.name, systemImage: filter.iconName)
.badge(vehicles.filter(filter.match).count)
}
}
}
}
.navigationSplitViewColumnWidth(180)
.toolbar {
ToolbarItemGroup(placement: .primaryAction) {
Spacer()
Button {
checkSheetPresented = true
} label: {
Image(systemName: "plus")
}
.sheet(isPresented: $checkSheetPresented) {
CheckNumber()
}
}
}
} content: {
if let filter = selectedFilter {
let filtered = vehicles.filter(filter.match)
if filtered.isEmpty {
VStack {
Image(systemName: filter.iconName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 64)
.foregroundColor(.secondary)
Text("Nothing here")
.font(.system(size: 18))
.foregroundColor(.secondary)
}
} else {
VehiclesListView(vehicles: filtered, selection: $selectedVehicle)
.toolbar {
ToolbarItem {
Button(action: {}) {
Image(systemName: "line.3.horizontal.decrease.circle")
}
}
}
.navigationTitle(filter.name)
.navigationSubtitle("\(filtered.count) vehicles")
}
} else {
EmptyView()
}
} detail: {
if let vehicle = selectedVehicle {
VehicleDetailView(vehicle: vehicle)
.toolbar {
ToolbarItem(placement: .automatic) {
Button(action: {}) {
Image(systemName: "square.and.arrow.up")
}
}
}
.navigationTitle(vehicle.brand?.name?.original ?? "")
.navigationSubtitle("\(vehicle.year)")
.searchable(text: $searchText, placement: .toolbar)
} else {
EmptyView()
}
}
.navigationSplitViewStyle(.balanced)
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}