118 lines
3.0 KiB
Swift
118 lines
3.0 KiB
Swift
|
//
|
||
|
// RepositoriesView.swift
|
||
|
// Piper ~ App
|
||
|
//
|
||
|
// Created by Javier Cicchelli on 14/10/2024.
|
||
|
// Copyright © 2024 Röck+Cöde. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import SwiftData
|
||
|
import SwiftUI
|
||
|
|
||
|
struct RepositoriesView: View {
|
||
|
|
||
|
// MARK: Properties
|
||
|
|
||
|
@Environment(\.modelContext) private var modelContext
|
||
|
|
||
|
@Query(sort: \Repository.addedAt)
|
||
|
private var repositories: [Repository]
|
||
|
|
||
|
@State private var viewModel: RepositoriesViewModel = .init()
|
||
|
|
||
|
// MARK: Body
|
||
|
|
||
|
var body: some View {
|
||
|
HStack(
|
||
|
alignment: .top,
|
||
|
spacing: Layout.spacingHorizontal
|
||
|
) {
|
||
|
Table(
|
||
|
repositories,
|
||
|
selection: $viewModel.rowsSelected
|
||
|
) {
|
||
|
TableColumn("settings.column.active.text") { repository in
|
||
|
HStack {
|
||
|
Toggle("settings.item.active.text", isOn: .init {
|
||
|
repository.active
|
||
|
} set: {
|
||
|
repository.active = $0
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
TableColumn("settings.column.name.text") { repository in
|
||
|
Text(repository.name)
|
||
|
.fontWeight(.semibold)
|
||
|
.foregroundStyle(.primary)
|
||
|
}
|
||
|
|
||
|
TableColumn("settings.column.folder.text") { repository in
|
||
|
Text(repository.folder)
|
||
|
.fontWeight(.regular)
|
||
|
.foregroundStyle(.secondary)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
VStack {
|
||
|
Button("settings.button.add-repository.text") {
|
||
|
viewModel.fileImporterOpened = true
|
||
|
}
|
||
|
.fileImporter(
|
||
|
isPresented: $viewModel.fileImporterOpened,
|
||
|
allowedContentTypes: [.pdf]
|
||
|
) {
|
||
|
viewModel.addRepository($0, into: modelContext)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// MARK: - Layout
|
||
|
|
||
|
private extension RepositoriesView {
|
||
|
enum Layout {
|
||
|
static let spacingHorizontal = CGFloat(16)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// MARK: - Previews
|
||
|
|
||
|
@available(macOS 15.0, *)
|
||
|
#Preview(
|
||
|
"Repositories view when no repositories found (macOS 15)",
|
||
|
traits: .emptyData
|
||
|
) {
|
||
|
RepositoriesView()
|
||
|
.scenePadding()
|
||
|
}
|
||
|
|
||
|
@available(macOS, obsoleted: 15)
|
||
|
#Preview("Repositories view when no repositories found (macOS 14)") {
|
||
|
MenuBarView()
|
||
|
.modelContainer(.preview)
|
||
|
.scenePadding()
|
||
|
}
|
||
|
|
||
|
@available(macOS 15.0, *)
|
||
|
#Preview(
|
||
|
"Repositories view when repositories found (macOS 15)",
|
||
|
traits: .sampleData
|
||
|
) {
|
||
|
RepositoriesView()
|
||
|
.scenePadding()
|
||
|
}
|
||
|
|
||
|
@available(macOS, obsoleted: 15)
|
||
|
#Preview("Repositories view when repositories found (macOS 14)") {
|
||
|
let container = ModelContainer.preview
|
||
|
|
||
|
Repository.samples(in: container)
|
||
|
|
||
|
return RepositoriesView()
|
||
|
.modelContainer(container)
|
||
|
.scenePadding()
|
||
|
}
|