|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +struct MyExercisesView: View { |
| 4 | + @EnvironmentObject var appState: AppState |
| 5 | + @EnvironmentObject var navigation: AppNavigationCoordinator |
| 6 | + |
| 7 | + @State private var exercises: [Exercise] = [] |
| 8 | + @State private var isLoading = true |
| 9 | + @State private var selectedStatus: ExerciseFilter = .all |
| 10 | + |
| 11 | + enum ExerciseFilter: String, CaseIterable, Identifiable { |
| 12 | + case all, pending, verified |
| 13 | + var id: String { rawValue } |
| 14 | + |
| 15 | + var localized: String { |
| 16 | + switch self { |
| 17 | + case .all: return L10n.homeMyExercisesFilterAll |
| 18 | + case .pending: return L10n.homeMyExercisesFilterPending |
| 19 | + case .verified: return L10n.homeMyExercisesFilterVerified |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + var filteredExercises: [Exercise] { |
| 25 | + switch selectedStatus { |
| 26 | + case .all: return exercises |
| 27 | + case .pending: return exercises.filter { $0.isPending } |
| 28 | + case .verified: return exercises.filter { $0.isVerified } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + var body: some View { |
| 33 | + VStack(spacing: 0) { |
| 34 | + // Filter Picker |
| 35 | + Picker(L10n.commonCategory, selection: $selectedStatus) { |
| 36 | + ForEach(ExerciseFilter.allCases) { filter in |
| 37 | + Text(filter.localized).tag(filter) |
| 38 | + } |
| 39 | + } |
| 40 | + .pickerStyle(.segmented) |
| 41 | + .padding() |
| 42 | + .background(Color.dmBackground) |
| 43 | + |
| 44 | + ZStack { |
| 45 | + Color.dmBackground.ignoresSafeArea() |
| 46 | + |
| 47 | + if isLoading { |
| 48 | + ProgressView() |
| 49 | + } else if exercises.isEmpty { |
| 50 | + emptyState |
| 51 | + } else { |
| 52 | + List { |
| 53 | + ForEach(filteredExercises) { exercise in |
| 54 | + exerciseRow(exercise) |
| 55 | + .listRowInsets(EdgeInsets(top: 8, leading: 16, bottom: 8, trailing: 16)) |
| 56 | + .listRowSeparator(.hidden) |
| 57 | + .listRowBackground(Color.clear) |
| 58 | + .onTapGesture { |
| 59 | + navigation.pushHome(.exerciseDetail(id: exercise.id)) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + .listStyle(.plain) |
| 64 | + .refreshable { |
| 65 | + await loadExercises() |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + .navigationTitle(L10n.homeMyExercisesTitle) |
| 71 | + .navigationBarTitleDisplayMode(.inline) |
| 72 | + .onAppear { |
| 73 | + Task { |
| 74 | + await loadExercises() |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private var emptyState: some View { |
| 80 | + VStack(spacing: 20) { |
| 81 | + Image(systemName: "doc.text.fill") |
| 82 | + .font(.system(size: 60)) |
| 83 | + .foregroundStyle(Color.dmTextSecondary.opacity(0.3)) |
| 84 | + |
| 85 | + Text(L10n.homeMyExercisesEmptyTitle) |
| 86 | + .font(DMFont.heading5()) |
| 87 | + |
| 88 | + Text(L10n.homeMyExercisesEmptyDesc) |
| 89 | + .font(DMFont.bodySm()) |
| 90 | + .foregroundStyle(Color.dmTextSecondary) |
| 91 | + .multilineTextAlignment(.center) |
| 92 | + .padding(.horizontal, 40) |
| 93 | + |
| 94 | + Button { |
| 95 | + navigation.pushCommunity(.createExercise) |
| 96 | + } label: { |
| 97 | + Text(L10n.homeMyExercisesCreateCta) |
| 98 | + .primaryButton() |
| 99 | + } |
| 100 | + .padding(.horizontal, 60) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private func exerciseRow(_ exercise: Exercise) -> some View { |
| 105 | + VStack(alignment: .leading, spacing: 12) { |
| 106 | + HStack { |
| 107 | + Text(exercise.categoryEnum?.displayName ?? exercise.category) |
| 108 | + .font(DMFont.microUppercase()) |
| 109 | + .padding(.horizontal, 8) |
| 110 | + .padding(.vertical, 4) |
| 111 | + .background(Color.dmPrimary.opacity(0.1)) |
| 112 | + .foregroundStyle(Color.dmPrimary) |
| 113 | + .cornerRadius(DMRadius.sm) |
| 114 | + |
| 115 | + Spacer() |
| 116 | + |
| 117 | + statusBadge(exercise.status) |
| 118 | + } |
| 119 | + |
| 120 | + Text(exercise.title) |
| 121 | + .font(DMFont.bodyMdMedium()) |
| 122 | + .lineLimit(2) |
| 123 | + |
| 124 | + HStack(spacing: 16) { |
| 125 | + Label("\(exercise.votesCount)", systemImage: "arrow.up.circle.fill") |
| 126 | + Label("\(exercise.commentsCount ?? 0)", systemImage: "bubble.right.fill") |
| 127 | + Spacer() |
| 128 | + Text(exercise.createdAt.formatted(date: .abbreviated, time: .omitted)) |
| 129 | + } |
| 130 | + .font(DMFont.caption()) |
| 131 | + .foregroundStyle(Color.dmTextSecondary) |
| 132 | + } |
| 133 | + .padding(DMSpacing.md) |
| 134 | + .background(Color.dmSurface) |
| 135 | + .cornerRadius(DMRadius.lg) |
| 136 | + .shadow(color: .black.opacity(0.03), radius: 5, y: 2) |
| 137 | + } |
| 138 | + |
| 139 | + @ViewBuilder |
| 140 | + private func statusBadge(_ status: String) -> some View { |
| 141 | + let isVerified = status == "verified" |
| 142 | + HStack(spacing: 4) { |
| 143 | + Circle() |
| 144 | + .fill(isVerified ? Color.dmSuccess : Color.dmWarning) |
| 145 | + .frame(width: 8, height: 8) |
| 146 | + Text(isVerified ? L10n.homeMyExercisesStatusVerified : L10n.homeMyExercisesStatusPending) |
| 147 | + .font(DMFont.captionBold()) |
| 148 | + .foregroundStyle(isVerified ? Color.dmSuccess : Color.dmWarning) |
| 149 | + } |
| 150 | + .padding(.horizontal, 8) |
| 151 | + .padding(.vertical, 4) |
| 152 | + .background((isVerified ? Color.dmSuccess : Color.dmWarning).opacity(0.1)) |
| 153 | + .cornerRadius(DMRadius.pill) |
| 154 | + } |
| 155 | + |
| 156 | + private func loadExercises() async { |
| 157 | + guard let userId = appState.currentUser?.id else { |
| 158 | + isLoading = false |
| 159 | + return |
| 160 | + } |
| 161 | + isLoading = true |
| 162 | + let result = await appState.exerciseRepository.fetchExercisesByAuthor(userId: userId) |
| 163 | + switch result { |
| 164 | + case .success(let fetched): |
| 165 | + exercises = fetched |
| 166 | + case .failure(let error): |
| 167 | + print("Error loading my exercises: \(error)") |
| 168 | + } |
| 169 | + isLoading = false |
| 170 | + } |
| 171 | +} |
0 commit comments