103 lines
4.2 KiB
Swift
103 lines
4.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
|
||
|
||
@Query(sort: [
|
||
SortDescriptor(\Split.order),
|
||
SortDescriptor(\Split.name)
|
||
]) private var splits: [Split]
|
||
|
||
@State private var showingAddSheet: Bool = false
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
ScrollView {
|
||
LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible())], spacing: 16) {
|
||
ForEach(splits) { split in
|
||
NavigationLink {
|
||
SplitExercisesListView(model: split)
|
||
} label: {
|
||
VStack {
|
||
ZStack(alignment: .bottom) {
|
||
// Golden ratio rectangle (1:1.618)
|
||
RoundedRectangle(cornerRadius: 12)
|
||
.fill(
|
||
LinearGradient(
|
||
gradient: Gradient(colors: [split.getColor(), split.getColor().darker(by: 0.2)]),
|
||
startPoint: .topLeading,
|
||
endPoint: .bottomTrailing
|
||
)
|
||
)
|
||
.aspectRatio(1.618, contentMode: .fit)
|
||
.shadow(radius: 2)
|
||
|
||
VStack {
|
||
// Icon in the center
|
||
Image(systemName: split.systemImage)
|
||
.font(.system(size: 40, weight: .medium))
|
||
.foregroundColor(.white)
|
||
.offset(y: -15)
|
||
|
||
// Name at the bottom inside the rectangle
|
||
Text(split.name)
|
||
.font(.headline)
|
||
.foregroundColor(.white)
|
||
.lineLimit(1)
|
||
.padding(.horizontal, 8)
|
||
.padding(.bottom, 8)
|
||
}
|
||
}
|
||
|
||
// Exercise count below the rectangle
|
||
Text("\(split.exercises?.count ?? 0) exercises")
|
||
.font(.caption)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
}
|
||
}
|
||
.onMove(perform: { indices, destination in
|
||
var splitArray = Array(splits)
|
||
splitArray.move(fromOffsets: indices, toOffset: destination)
|
||
for (index, split) in splitArray.enumerated() {
|
||
split.order = index
|
||
}
|
||
if let modelContext = splitArray.first?.modelContext {
|
||
do {
|
||
try modelContext.save()
|
||
} catch {
|
||
print("Error saving after reordering: \(error)")
|
||
}
|
||
}
|
||
})
|
||
|
||
}
|
||
.padding()
|
||
}
|
||
.navigationTitle("Splits")
|
||
}
|
||
.toolbar {
|
||
ToolbarItem(placement: .navigationBarTrailing) {
|
||
Button(action: { showingAddSheet.toggle() }) {
|
||
Image(systemName: "plus")
|
||
}
|
||
}
|
||
}
|
||
.sheet (isPresented: $showingAddSheet) {
|
||
SplitAddEditView(model: Split(name: "New Split"))
|
||
}
|
||
|
||
}
|
||
}
|