163 lines
8.5 KiB
Swift
163 lines
8.5 KiB
Swift
import SwiftUI
|
|
|
|
struct AddHabitView: View {
|
|
@Binding var isPresented: Bool
|
|
@EnvironmentObject var authManager: AuthManager
|
|
let onAdded: () async -> Void
|
|
|
|
@State private var name = ""
|
|
@State private var description = ""
|
|
@State private var frequency = "daily"
|
|
@State private var selectedIcon = "🔥"
|
|
@State private var selectedColor = "#00d4aa"
|
|
@State private var isLoading = false
|
|
|
|
let frequencies: [(String, String, String)] = [
|
|
("daily", "Каждый день", "calendar"),
|
|
("weekly", "Каждую неделю", "calendar.badge.clock"),
|
|
("monthly", "Каждый месяц", "calendar.badge.plus")
|
|
]
|
|
|
|
let icons = ["🔥", "💪", "🏃", "📚", "💧", "🧘", "🎯", "⭐️", "🌟", "✅", "🏋️", "🚴", "🍎", "😴", "🧠", "🎨", "🎵", "💊", "🌿", "💰"]
|
|
|
|
let colors = ["#00d4aa", "#7c3aed", "#ff4757", "#ffa502", "#6366f1", "#ec4899", "#14b8a6", "#f59e0b", "#10b981", "#3b82f6"]
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
Color(hex: "0a0a1a").ignoresSafeArea()
|
|
|
|
VStack(spacing: 0) {
|
|
// Handle
|
|
RoundedRectangle(cornerRadius: 3)
|
|
.fill(Color.white.opacity(0.2))
|
|
.frame(width: 40, height: 4)
|
|
.padding(.top, 12)
|
|
|
|
// Header
|
|
HStack {
|
|
Button("Отмена") { isPresented = false }
|
|
.foregroundColor(Color(hex: "8888aa"))
|
|
Spacer()
|
|
Text("Новая привычка").font(.headline).foregroundColor(.white)
|
|
Spacer()
|
|
Button(action: save) {
|
|
if isLoading { ProgressView().tint(Color(hex: "00d4aa")).scaleEffect(0.8) }
|
|
else { Text("Добавить").foregroundColor(name.isEmpty ? Color(hex: "8888aa") : Color(hex: "00d4aa")).fontWeight(.semibold) }
|
|
}
|
|
.disabled(name.isEmpty || isLoading)
|
|
}
|
|
.padding(.horizontal, 20).padding(.vertical, 16)
|
|
|
|
Divider().background(Color.white.opacity(0.1))
|
|
|
|
ScrollView {
|
|
VStack(spacing: 20) {
|
|
// Preview
|
|
HStack(spacing: 16) {
|
|
ZStack {
|
|
Circle()
|
|
.fill(Color(hex: String(selectedColor.dropFirst())).opacity(0.25))
|
|
.frame(width: 56, height: 56)
|
|
Text(selectedIcon).font(.title2)
|
|
}
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text(name.isEmpty ? "Название привычки" : name)
|
|
.font(.callout.bold())
|
|
.foregroundColor(name.isEmpty ? Color(hex: "8888aa") : .white)
|
|
Text(frequencies.first { $0.0 == frequency }?.1 ?? "").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
|
}
|
|
Spacer()
|
|
}
|
|
.padding(16)
|
|
.background(RoundedRectangle(cornerRadius: 16).fill(Color.white.opacity(0.05)))
|
|
|
|
// Name
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Label("Название", systemImage: "pencil").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
|
TextField("Например: Читать 30 минут", text: $name)
|
|
.foregroundColor(.white).padding(14)
|
|
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07)))
|
|
}
|
|
|
|
// Frequency
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Label("Периодичность", systemImage: "calendar").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
|
VStack(spacing: 6) {
|
|
ForEach(frequencies, id: \.0) { f in
|
|
Button(action: { frequency = f.0 }) {
|
|
HStack {
|
|
Image(systemName: f.2).foregroundColor(frequency == f.0 ? Color(hex: "00d4aa") : Color(hex: "8888aa"))
|
|
Text(f.1).foregroundColor(frequency == f.0 ? .white : Color(hex: "8888aa"))
|
|
Spacer()
|
|
if frequency == f.0 {
|
|
Image(systemName: "checkmark").foregroundColor(Color(hex: "00d4aa"))
|
|
}
|
|
}
|
|
.padding(14)
|
|
.background(RoundedRectangle(cornerRadius: 12).fill(frequency == f.0 ? Color(hex: "00d4aa").opacity(0.15) : Color.white.opacity(0.05)))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Icon picker
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Label("Иконка", systemImage: "face.smiling").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
|
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 5), spacing: 8) {
|
|
ForEach(icons, id: \.self) { icon in
|
|
Button(action: { selectedIcon = icon }) {
|
|
Text(icon).font(.title2)
|
|
.frame(width: 44, height: 44)
|
|
.background(Circle().fill(selectedIcon == icon ? Color(hex: "00d4aa").opacity(0.25) : Color.white.opacity(0.05)))
|
|
.overlay(Circle().stroke(selectedIcon == icon ? Color(hex: "00d4aa") : Color.clear, lineWidth: 2))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Color picker
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Label("Цвет", systemImage: "paintpalette").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
|
HStack(spacing: 10) {
|
|
ForEach(colors, id: \.self) { color in
|
|
Button(action: { selectedColor = color }) {
|
|
Circle()
|
|
.fill(Color(hex: String(color.dropFirst())))
|
|
.frame(width: 32, height: 32)
|
|
.overlay(Circle().stroke(.white, lineWidth: selectedColor == color ? 2 : 0))
|
|
.scaleEffect(selectedColor == color ? 1.15 : 1.0)
|
|
.animation(.easeInOut(duration: 0.15), value: selectedColor)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.padding(20)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func save() {
|
|
isLoading = true
|
|
Task {
|
|
var req = URLRequest(url: URL(string: "https://api.digital-home.site/habits")!)
|
|
req.httpMethod = "POST"
|
|
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
req.setValue("Bearer \(authManager.token)", forHTTPHeaderField: "Authorization")
|
|
let body: [String: Any] = [
|
|
"name": name,
|
|
"description": description,
|
|
"frequency": frequency,
|
|
"icon": selectedIcon,
|
|
"color": selectedColor,
|
|
"target_count": 1
|
|
]
|
|
req.httpBody = try? JSONSerialization.data(withJSONObject: body)
|
|
_ = try? await URLSession.shared.data(for: req)
|
|
await onAdded()
|
|
await MainActor.run { isPresented = false }
|
|
}
|
|
}
|
|
}
|