57 lines
1.6 KiB
Swift
57 lines
1.6 KiB
Swift
//
|
||
// SplitPickerView.swift
|
||
// Workouts
|
||
//
|
||
// Created by rzen on 7/25/25 at 6:24 PM.
|
||
//
|
||
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
|
||
//
|
||
|
||
import SwiftUI
|
||
import SwiftData
|
||
|
||
struct SplitListView: View {
|
||
@Environment(\.modelContext) private var modelContext
|
||
|
||
@State var splits: [Split]
|
||
|
||
@State private var allowSorting: Bool = true
|
||
|
||
var body: some View {
|
||
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()
|
||
}
|
||
.onAppear(perform: loadSplits)
|
||
}
|
||
|
||
func loadSplits () {
|
||
print("Loading splits")
|
||
do {
|
||
self.splits = try modelContext.fetch(FetchDescriptor<Split>(
|
||
sortBy: [
|
||
SortDescriptor(\Split.order),
|
||
SortDescriptor(\Split.name)
|
||
]
|
||
))
|
||
print("Loaded \(splits.count) splits")
|
||
} catch {
|
||
print("ERROR: failed to load splits \(error)")
|
||
}
|
||
}
|
||
}
|