124 lines
3.7 KiB
Swift
124 lines
3.7 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 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: "trash")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
EmptyView()
|
|
}
|
|
} detail: {
|
|
if let vehicle = selectedVehicle {
|
|
VehicleDetailView(vehicle: vehicle)
|
|
} else {
|
|
EmptyView()
|
|
}
|
|
}
|
|
.navigationSplitViewStyle(.balanced)
|
|
|
|
}
|
|
}
|
|
|
|
struct MainView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
MainView()
|
|
}
|
|
}
|