71 lines
2.2 KiB
Swift
71 lines
2.2 KiB
Swift
//
|
||
// SplitsView.swift
|
||
// Workouts
|
||
//
|
||
// Created by rzen on 7/17/25 at 6:55 PM.
|
||
//
|
||
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
||
//
|
||
|
||
import SwiftUI
|
||
import SwiftData
|
||
|
||
struct SplitsView: View {
|
||
@Environment(\.modelContext) private var modelContext
|
||
@Environment(\.dismiss) private var dismiss
|
||
|
||
@State var splits: [Split] = []
|
||
|
||
@State private var showingAddSheet: Bool = false
|
||
@State private var allowSorting: Bool = true
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
ScrollView {
|
||
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 16) {
|
||
SortableForEach($splits, allowReordering: $allowSorting) { split, dragging in
|
||
NavigationLink {
|
||
SplitDetailView(split: split)
|
||
} label: {
|
||
SplitItem(
|
||
name: split.name,
|
||
color: Color.color(from: split.color),
|
||
systemImageName: split.systemImage,
|
||
exerciseCount: split.exercises?.count ?? 0
|
||
)
|
||
.overlay(dragging ? Color.white.opacity(0.8) : Color.clear)
|
||
}
|
||
}
|
||
}
|
||
.padding()
|
||
}
|
||
.navigationTitle("Splits")
|
||
.onAppear(perform: loadSplits)
|
||
.toolbar {
|
||
ToolbarItem(placement: .navigationBarTrailing) {
|
||
Button(action: { showingAddSheet.toggle() }) {
|
||
Image(systemName: "plus")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.sheet (isPresented: $showingAddSheet) {
|
||
SplitAddEditView(model: Split(name: "New Split"))
|
||
}
|
||
|
||
}
|
||
|
||
func loadSplits () {
|
||
do {
|
||
self.splits = try modelContext.fetch(FetchDescriptor<Split>(
|
||
sortBy: [
|
||
SortDescriptor(\Split.order),
|
||
SortDescriptor(\Split.name)
|
||
]
|
||
))
|
||
} catch {
|
||
print("ERROR: failed to load splits \(error)")
|
||
}
|
||
}
|
||
}
|