Files
pulse-mobile/PulseHealth/Views/Tasks/TasksView.swift

99 lines
3.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SwiftUI
struct TasksView: View {
@EnvironmentObject var authManager: AuthManager
@State private var tasks: [PulseTask] = []
@State private var isLoading = true
@State private var showAddTask = false
@State private var filter: TaskFilter = .pending
enum TaskFilter: String, CaseIterable {
case pending = "Активные"
case completed = "Выполненные"
case all = "Все"
}
var filteredTasks: [PulseTask] {
switch filter {
case .pending: return tasks.filter { !$0.completed }
case .completed: return tasks.filter { $0.completed }
case .all: return tasks
}
}
var body: some View {
ZStack {
Color(hex: "0a0a1a").ignoresSafeArea()
VStack(spacing: 0) {
// Header
HStack {
Text("Задачи").font(.title.bold()).foregroundColor(.white)
Spacer()
Button(action: { showAddTask = true }) {
Image(systemName: "plus.circle.fill").font(.title2).foregroundColor(Color(hex: "00d4aa"))
}
}.padding()
// Filter
Picker("", selection: $filter) {
ForEach(TaskFilter.allCases, id: \.self) { f in Text(f.rawValue).tag(f) }
}
.pickerStyle(.segmented)
.padding(.horizontal)
.padding(.bottom, 8)
if isLoading {
ProgressView().tint(Color(hex: "00d4aa")).padding(.top, 40)
Spacer()
} else if filteredTasks.isEmpty {
VStack(spacing: 12) {
Text("").font(.system(size: 50))
Text(filter == .pending ? "Нет активных задач" : "Нет задач")
.foregroundColor(Color(hex: "8888aa"))
}.padding(.top, 60)
Spacer()
} else {
List {
ForEach(filteredTasks) { task in
TaskRowView(task: task) { await completeTask(task) }
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
}
.onDelete { indices in
let tasksToDelete = indices.map { filteredTasks[$0] }
Task {
for task in tasksToDelete {
try? await APIService.shared.deleteTask(token: authManager.token, id: task.id)
}
await loadTasks()
}
}
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
}
}
}
.sheet(isPresented: $showAddTask) {
AddTaskView(isPresented: $showAddTask) { await loadTasks() }
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
.presentationBackground(Color(hex: "0a0a1a"))
}
.task { await loadTasks() }
.refreshable { await loadTasks(refresh: true) }
}
func loadTasks(refresh: Bool = false) async {
if !refresh { isLoading = true }
tasks = (try? await APIService.shared.getTasks(token: authManager.token)) ?? []
isLoading = false
}
func completeTask(_ task: PulseTask) async {
try? await APIService.shared.completeTask(token: authManager.token, id: task.id)
await loadTasks()
UIImpactFeedbackGenerator(style: .light).impactOccurred()
}
}