feat: major app overhaul — API fixes, glassmorphism UI, health dashboard, notifications
API Integration: - Fix logHabit: send "date" instead of "completed_at" - Fix FinanceCategory: "icon" → "emoji" to match API - Fix task priorities: remove level 4, keep 1-3 matching API - Fix habit frequencies: map monthly/interval → "custom" for API - Add token refresh (401 → auto retry with new token) - Add proper error handling (remove try? in save functions, show errors in UI) - Add date field to savings transactions - Add MonthlyPaymentDetail and OverduePayment models - Fix habit completedToday: compute on client from logs (API doesn't return it) - Filter habits by day of week on client (daily/weekly/monthly/interval) Design System (glassmorphism): - New DesignSystem.swift: Theme colors, GlassCard modifier, GlowIcon, GlowStatCard - Custom tab bar with per-tab glow colors (VStack layout, not ZStack overlay) - Deep dark background #06060f across all views - Glass cards with gradient fill + stroke throughout app - App icon: glassmorphism style with teal glow Health Dashboard: - Compact ReadinessBanner with recommendation text - 8 metric tiles: sleep, HR, HRV, steps, SpO2, respiratory rate, energy, distance - Each tile with status indicator (good/ok/bad) and hint text - Heart rate card (min/avg/max) - Weekly trends card (averages) - Recovery score (weighted: 40% sleep, 35% HRV, 25% RHR) - Tips card with actionable recommendations - Sleep detail view with hypnogram (step chart of phases) - Sleep segments timeline from HealthKit (deep/rem/core/awake with exact times) - Line chart replacing bar chart for weekly data - Collect respiratory_rate and sleep phases with timestamps from HealthKit - Background sync every ~30min via BGProcessingTask Notifications: - NotificationService for local push notifications - Morning/evening reminders with native DatePicker (wheel) - Payment reminders: 5 days, 1 day, and day-of for recurring savings - Notification settings in Settings tab UI Fixes: - Fix color picker overflow: HStack → LazyVGrid 5 columns - Fix sheet headers: shorter text, proper padding - Fix task/habit toggle: separate tap zones (checkbox vs edit) - Fix deprecated onChange syntax for iOS 17+ - Savings overview: real monthly payments and detailed overdues from API - Settings: timezone as Menu picker, removed Telegram/server notifications sections - All sheets use .presentationDetents([.large]) Config: - project.yml: real DEVELOPMENT_TEAM, HealthKit + BackgroundModes capabilities - Info.plist: BGTaskScheduler + UIBackgroundModes - Assets.xcassets with AppIcon - CLAUDE.md project documentation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,19 +5,21 @@ struct AddTransactionView: View {
|
||||
@EnvironmentObject var authManager: AuthManager
|
||||
let categories: [FinanceCategory]
|
||||
let onAdded: () async -> Void
|
||||
|
||||
|
||||
@State private var amount = ""
|
||||
@State private var description = ""
|
||||
@State private var type = "expense"
|
||||
@State private var selectedCategoryId: Int? = nil
|
||||
@State private var date = Date()
|
||||
@State private var isLoading = false
|
||||
@State private var errorMessage: String?
|
||||
|
||||
var filteredCategories: [FinanceCategory] { categories.filter { $0.type == type } }
|
||||
var isExpense: Bool { type == "expense" }
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(hex: "0a0a1a").ignoresSafeArea()
|
||||
Color(hex: "06060f").ignoresSafeArea()
|
||||
|
||||
VStack(spacing: 0) {
|
||||
// Handle
|
||||
@@ -91,7 +93,16 @@ struct AddTransactionView: View {
|
||||
.foregroundColor(.white).padding(14)
|
||||
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07)))
|
||||
}
|
||||
|
||||
|
||||
// Date
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Label("Дата", systemImage: "calendar").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
||||
DatePicker("", selection: $date, displayedComponents: .date)
|
||||
.labelsHidden()
|
||||
.colorInvert()
|
||||
.colorMultiply(Color(hex: "0D9488"))
|
||||
}
|
||||
|
||||
// Categories
|
||||
if !filteredCategories.isEmpty {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
@@ -100,7 +111,7 @@ struct AddTransactionView: View {
|
||||
ForEach(filteredCategories) { cat in
|
||||
Button(action: { selectedCategoryId = selectedCategoryId == cat.id ? nil : cat.id }) {
|
||||
HStack(spacing: 6) {
|
||||
Text(cat.icon ?? "").font(.callout)
|
||||
Text(cat.emoji ?? "").font(.callout)
|
||||
Text(cat.name).font(.caption).lineLimit(1)
|
||||
}
|
||||
.foregroundColor(selectedCategoryId == cat.id ? .black : .white)
|
||||
@@ -115,21 +126,38 @@ struct AddTransactionView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
if let err = errorMessage {
|
||||
Text(err)
|
||||
.font(.caption).foregroundColor(Color(hex: "ff4757"))
|
||||
.padding(10)
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(RoundedRectangle(cornerRadius: 10).fill(Color(hex: "ff4757").opacity(0.1)))
|
||||
}
|
||||
}
|
||||
.padding(20)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func save() {
|
||||
guard let a = Double(amount.replacingOccurrences(of: ",", with: ".")) else { return }
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
let df = DateFormatter(); df.dateFormat = "yyyy-MM-dd"
|
||||
let dateStr = df.string(from: date)
|
||||
Task {
|
||||
let req = CreateTransactionRequest(amount: a, categoryId: selectedCategoryId, description: description.isEmpty ? nil : description, type: type)
|
||||
try? await APIService.shared.createTransaction(token: authManager.token, request: req)
|
||||
await onAdded()
|
||||
await MainActor.run { isPresented = false }
|
||||
do {
|
||||
let req = CreateTransactionRequest(amount: a, categoryId: selectedCategoryId, description: description.isEmpty ? nil : description, type: type, date: dateStr)
|
||||
try await APIService.shared.createTransaction(token: authManager.token, request: req)
|
||||
await onAdded()
|
||||
await MainActor.run { isPresented = false }
|
||||
} catch {
|
||||
await MainActor.run {
|
||||
errorMessage = error.localizedDescription
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ struct FinanceView: View {
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(hex: "0a0a1a").ignoresSafeArea()
|
||||
Color(hex: "06060f").ignoresSafeArea()
|
||||
VStack(spacing: 0) {
|
||||
// Header with month picker
|
||||
HStack {
|
||||
@@ -37,6 +37,7 @@ struct FinanceView: View {
|
||||
Text("Обзор").tag(0)
|
||||
Text("Транзакции").tag(1)
|
||||
Text("Аналитика").tag(2)
|
||||
Text("Категории").tag(3)
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
.padding(.horizontal)
|
||||
@@ -45,7 +46,8 @@ struct FinanceView: View {
|
||||
switch selectedTab {
|
||||
case 0: FinanceOverviewTab(month: selectedMonth, year: selectedYear)
|
||||
case 1: FinanceTransactionsTab(month: selectedMonth, year: selectedYear)
|
||||
default: FinanceAnalyticsTab(month: selectedMonth, year: selectedYear)
|
||||
case 2: FinanceAnalyticsTab(month: selectedMonth, year: selectedYear)
|
||||
default: FinanceCategoriesTab()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,7 +107,7 @@ struct FinanceOverviewTab: View {
|
||||
let pct = (cat.total ?? 0) / max(total, 1)
|
||||
VStack(spacing: 4) {
|
||||
HStack {
|
||||
Text(cat.icon ?? "💸").font(.subheadline)
|
||||
Text(cat.emoji ?? "💸").font(.subheadline)
|
||||
Text(cat.categoryName ?? "—").font(.callout).foregroundColor(.white)
|
||||
Spacer()
|
||||
Text(formatAmt(cat.total ?? 0)).font(.callout.bold()).foregroundColor(Color(hex: "ff4757"))
|
||||
@@ -193,8 +195,8 @@ struct FinanceOverviewTab: View {
|
||||
.padding(.top, 8)
|
||||
}
|
||||
.task { await load() }
|
||||
.onChange(of: month) { _ in Task { await load() } }
|
||||
.onChange(of: year) { _ in Task { await load() } }
|
||||
.onChange(of: month) { Task { await load() } }
|
||||
.onChange(of: year) { Task { await load() } }
|
||||
.refreshable { await load(refresh: true) }
|
||||
}
|
||||
|
||||
@@ -266,6 +268,7 @@ struct FinanceTransactionsTab: View {
|
||||
@State private var categories: [FinanceCategory] = []
|
||||
@State private var isLoading = true
|
||||
@State private var showAdd = false
|
||||
@State private var editingTransaction: FinanceTransaction?
|
||||
|
||||
var groupedByDay: [(key: String, value: [FinanceTransaction])] {
|
||||
let grouped = Dictionary(grouping: transactions) { $0.dateOnly }
|
||||
@@ -291,6 +294,7 @@ struct FinanceTransactionsTab: View {
|
||||
FinanceTxRow(transaction: tx, categories: categories)
|
||||
.listRowBackground(Color.clear)
|
||||
.listRowSeparator(.hidden)
|
||||
.onTapGesture { editingTransaction = tx }
|
||||
}
|
||||
.onDelete { idx in
|
||||
let toDelete = idx.map { section.value[$0] }
|
||||
@@ -321,13 +325,22 @@ struct FinanceTransactionsTab: View {
|
||||
.padding(.trailing, 20)
|
||||
}
|
||||
.task { await load() }
|
||||
.onChange(of: month) { _ in Task { await load() } }
|
||||
.onChange(of: year) { _ in Task { await load() } }
|
||||
.onChange(of: month) { Task { await load() } }
|
||||
.onChange(of: year) { Task { await load() } }
|
||||
.sheet(isPresented: $showAdd) {
|
||||
AddTransactionView(isPresented: $showAdd, categories: categories) { await load(refresh: true) }
|
||||
.presentationDetents([.medium, .large])
|
||||
.presentationDragIndicator(.visible)
|
||||
.presentationBackground(Color(hex: "0a0a1a"))
|
||||
.presentationBackground(Color(hex: "06060f"))
|
||||
}
|
||||
.sheet(item: $editingTransaction) { tx in
|
||||
EditTransactionView(isPresented: .constant(true), transaction: tx, categories: categories) {
|
||||
editingTransaction = nil
|
||||
await load(refresh: true)
|
||||
}
|
||||
.presentationDetents([.medium, .large])
|
||||
.presentationDragIndicator(.visible)
|
||||
.presentationBackground(Color(hex: "06060f"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -360,7 +373,7 @@ struct FinanceTxRow: View {
|
||||
Circle()
|
||||
.fill((isIncome ? Color(hex: "0D9488") : Color(hex: "ff4757")).opacity(0.12))
|
||||
.frame(width: 40, height: 40)
|
||||
Text(cat?.icon ?? (isIncome ? "💰" : "💸")).font(.title3)
|
||||
Text(cat?.emoji ?? (isIncome ? "💰" : "💸")).font(.title3)
|
||||
}
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text(transaction.description ?? cat?.name ?? "Операция")
|
||||
@@ -434,8 +447,8 @@ struct FinanceAnalyticsTab: View {
|
||||
.padding(.top, 8)
|
||||
}
|
||||
.task { await load() }
|
||||
.onChange(of: month) { _ in Task { await load() } }
|
||||
.onChange(of: year) { _ in Task { await load() } }
|
||||
.onChange(of: month) { Task { await load() } }
|
||||
.onChange(of: year) { Task { await load() } }
|
||||
.refreshable { await load(refresh: true) }
|
||||
}
|
||||
|
||||
@@ -481,3 +494,346 @@ struct MonthComparisonCard: View {
|
||||
}
|
||||
func formatAmt(_ v: Double) -> String { String(format: "%.0f ₽", v) }
|
||||
}
|
||||
|
||||
// MARK: - EditTransactionView
|
||||
|
||||
struct EditTransactionView: View {
|
||||
@Binding var isPresented: Bool
|
||||
@EnvironmentObject var authManager: AuthManager
|
||||
let transaction: FinanceTransaction
|
||||
let categories: [FinanceCategory]
|
||||
let onSaved: () async -> Void
|
||||
|
||||
@State private var amount: String
|
||||
@State private var description: String
|
||||
@State private var type: String
|
||||
@State private var selectedCategoryId: Int?
|
||||
@State private var date: Date
|
||||
@State private var isLoading = false
|
||||
|
||||
var filteredCategories: [FinanceCategory] { categories.filter { $0.type == type } }
|
||||
var isExpense: Bool { type == "expense" }
|
||||
|
||||
init(isPresented: Binding<Bool>, transaction: FinanceTransaction, categories: [FinanceCategory], onSaved: @escaping () async -> Void) {
|
||||
self._isPresented = isPresented
|
||||
self.transaction = transaction
|
||||
self.categories = categories
|
||||
self.onSaved = onSaved
|
||||
self._amount = State(initialValue: String(format: "%.0f", transaction.amount))
|
||||
self._description = State(initialValue: transaction.description ?? "")
|
||||
self._type = State(initialValue: transaction.type)
|
||||
self._selectedCategoryId = State(initialValue: transaction.categoryId)
|
||||
let df = DateFormatter(); df.dateFormat = "yyyy-MM-dd"
|
||||
let d = transaction.date.flatMap { df.date(from: String($0.prefix(10))) } ?? Date()
|
||||
self._date = State(initialValue: d)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(hex: "06060f").ignoresSafeArea()
|
||||
VStack(spacing: 0) {
|
||||
RoundedRectangle(cornerRadius: 3)
|
||||
.fill(Color.white.opacity(0.2)).frame(width: 40, height: 4).padding(.top, 12)
|
||||
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(amount.isEmpty ? Color(hex: "8888aa") : Color(hex: "00d4aa")).fontWeight(.semibold) }
|
||||
}.disabled(amount.isEmpty || isLoading)
|
||||
}
|
||||
.padding(.horizontal, 20).padding(.vertical, 16)
|
||||
Divider().background(Color.white.opacity(0.1))
|
||||
ScrollView {
|
||||
VStack(spacing: 20) {
|
||||
HStack(spacing: 0) {
|
||||
Button(action: { type = "expense" }) {
|
||||
Text("Расход").font(.callout.bold())
|
||||
.foregroundColor(isExpense ? .black : Color(hex: "ff4757"))
|
||||
.frame(maxWidth: .infinity).padding(.vertical, 12)
|
||||
.background(isExpense ? Color(hex: "ff4757") : Color.clear)
|
||||
}
|
||||
Button(action: { type = "income" }) {
|
||||
Text("Доход").font(.callout.bold())
|
||||
.foregroundColor(!isExpense ? .black : Color(hex: "00d4aa"))
|
||||
.frame(maxWidth: .infinity).padding(.vertical, 12)
|
||||
.background(!isExpense ? Color(hex: "00d4aa") : Color.clear)
|
||||
}
|
||||
}
|
||||
.background(Color.white.opacity(0.07)).cornerRadius(12)
|
||||
|
||||
HStack {
|
||||
Text(isExpense ? "−" : "+").font(.title.bold())
|
||||
.foregroundColor(isExpense ? Color(hex: "ff4757") : Color(hex: "00d4aa"))
|
||||
TextField("0", text: $amount).keyboardType(.decimalPad)
|
||||
.font(.system(size: 36, weight: .bold)).foregroundColor(.white).multilineTextAlignment(.center)
|
||||
Text("₽").font(.title.bold()).foregroundColor(Color(hex: "8888aa"))
|
||||
}
|
||||
.padding(20)
|
||||
.background(RoundedRectangle(cornerRadius: 16).fill(Color.white.opacity(0.07)))
|
||||
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Label("Описание", systemImage: "text.alignleft").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
||||
TextField("Комментарий...", text: $description)
|
||||
.foregroundColor(.white).padding(14)
|
||||
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07)))
|
||||
}
|
||||
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Label("Дата", systemImage: "calendar").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
||||
DatePicker("", selection: $date, displayedComponents: .date)
|
||||
.labelsHidden().colorInvert().colorMultiply(Color(hex: "0D9488"))
|
||||
}
|
||||
|
||||
if !filteredCategories.isEmpty {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Label("Категория", systemImage: "tag.fill").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
||||
LazyVGrid(columns: [GridItem(.adaptive(minimum: 100))], spacing: 8) {
|
||||
ForEach(filteredCategories) { cat in
|
||||
Button(action: { selectedCategoryId = selectedCategoryId == cat.id ? nil : cat.id }) {
|
||||
HStack(spacing: 6) {
|
||||
Text(cat.emoji ?? "").font(.callout)
|
||||
Text(cat.name).font(.caption).lineLimit(1)
|
||||
}
|
||||
.foregroundColor(selectedCategoryId == cat.id ? .black : .white)
|
||||
.padding(.horizontal, 10).padding(.vertical, 8)
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(RoundedRectangle(cornerRadius: 10)
|
||||
.fill(selectedCategoryId == cat.id ? Color(hex: "00d4aa") : Color.white.opacity(0.07)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}.padding(20)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func save() {
|
||||
guard let a = Double(amount.replacingOccurrences(of: ",", with: ".")) else { return }
|
||||
isLoading = true
|
||||
let df = DateFormatter(); df.dateFormat = "yyyy-MM-dd"
|
||||
let dateStr = df.string(from: date)
|
||||
Task {
|
||||
let req = CreateTransactionRequest(amount: a, categoryId: selectedCategoryId,
|
||||
description: description.isEmpty ? nil : description,
|
||||
type: type, date: dateStr)
|
||||
try? await APIService.shared.updateTransaction(token: authManager.token, id: transaction.id, request: req)
|
||||
await onSaved()
|
||||
await MainActor.run { isPresented = false }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - FinanceCategoriesTab
|
||||
|
||||
struct FinanceCategoriesTab: View {
|
||||
@EnvironmentObject var authManager: AuthManager
|
||||
@State private var categories: [FinanceCategory] = []
|
||||
@State private var isLoading = true
|
||||
@State private var editingCategory: FinanceCategory?
|
||||
@State private var showAdd = false
|
||||
@State private var selectedType = "expense"
|
||||
|
||||
var filtered: [FinanceCategory] { categories.filter { $0.type == selectedType } }
|
||||
|
||||
var body: some View {
|
||||
ZStack(alignment: .bottomTrailing) {
|
||||
ScrollView {
|
||||
VStack(spacing: 12) {
|
||||
HStack(spacing: 0) {
|
||||
Button(action: { selectedType = "expense" }) {
|
||||
Text("Расходы").font(.callout.bold())
|
||||
.foregroundColor(selectedType == "expense" ? .black : Color(hex: "ff4757"))
|
||||
.frame(maxWidth: .infinity).padding(.vertical, 10)
|
||||
.background(selectedType == "expense" ? Color(hex: "ff4757") : Color.clear)
|
||||
}
|
||||
Button(action: { selectedType = "income" }) {
|
||||
Text("Доходы").font(.callout.bold())
|
||||
.foregroundColor(selectedType == "income" ? .black : Color(hex: "0D9488"))
|
||||
.frame(maxWidth: .infinity).padding(.vertical, 10)
|
||||
.background(selectedType == "income" ? Color(hex: "0D9488") : Color.clear)
|
||||
}
|
||||
}
|
||||
.background(Color.white.opacity(0.07)).cornerRadius(12)
|
||||
.padding(.horizontal)
|
||||
|
||||
if isLoading {
|
||||
ProgressView().tint(Color(hex: "0D9488")).padding(.top, 40)
|
||||
} else if filtered.isEmpty {
|
||||
EmptyState(icon: "tag", text: "Нет категорий")
|
||||
} else {
|
||||
ForEach(filtered) { cat in
|
||||
HStack(spacing: 12) {
|
||||
ZStack {
|
||||
Circle().fill(Color(hex: selectedType == "expense" ? "ff4757" : "0D9488").opacity(0.15))
|
||||
.frame(width: 40, height: 40)
|
||||
Text(cat.emoji ?? (selectedType == "expense" ? "💸" : "💰")).font(.title3)
|
||||
}
|
||||
Text(cat.name).font(.callout).foregroundColor(.white)
|
||||
Spacer()
|
||||
Button(action: { editingCategory = cat }) {
|
||||
Image(systemName: "pencil").foregroundColor(Color(hex: "8888aa"))
|
||||
}
|
||||
Button(action: { Task { await deleteCategory(cat) } }) {
|
||||
Image(systemName: "trash").foregroundColor(Color(hex: "ff4757").opacity(0.7))
|
||||
}
|
||||
}
|
||||
.padding(14)
|
||||
.background(RoundedRectangle(cornerRadius: 14).fill(Color.white.opacity(0.05)))
|
||||
.padding(.horizontal)
|
||||
}
|
||||
}
|
||||
Spacer(minLength: 80)
|
||||
}
|
||||
.padding(.top, 8)
|
||||
}
|
||||
.task { await load() }
|
||||
.refreshable { await load(refresh: true) }
|
||||
|
||||
Button(action: { showAdd = true }) {
|
||||
ZStack {
|
||||
Circle()
|
||||
.fill(LinearGradient(colors: [Color(hex: "0D9488"), Color(hex: "14b8a6")], startPoint: .topLeading, endPoint: .bottomTrailing))
|
||||
.frame(width: 56, height: 56)
|
||||
.shadow(color: Color(hex: "0D9488").opacity(0.4), radius: 8, y: 4)
|
||||
Image(systemName: "plus").font(.title2.bold()).foregroundColor(.white)
|
||||
}
|
||||
}
|
||||
.padding(.bottom, 90).padding(.trailing, 20)
|
||||
}
|
||||
.sheet(isPresented: $showAdd) {
|
||||
FinanceCategoryFormView(isPresented: $showAdd, category: nil, defaultType: selectedType) { await load(refresh: true) }
|
||||
.presentationDetents([.medium])
|
||||
.presentationDragIndicator(.visible)
|
||||
.presentationBackground(Color(hex: "06060f"))
|
||||
}
|
||||
.sheet(item: $editingCategory) { cat in
|
||||
FinanceCategoryFormView(isPresented: .constant(true), category: cat, defaultType: selectedType) {
|
||||
editingCategory = nil
|
||||
await load(refresh: true)
|
||||
}
|
||||
.presentationDetents([.medium])
|
||||
.presentationDragIndicator(.visible)
|
||||
.presentationBackground(Color(hex: "06060f"))
|
||||
}
|
||||
}
|
||||
|
||||
func load(refresh: Bool = false) async {
|
||||
if !refresh { isLoading = true }
|
||||
categories = (try? await APIService.shared.getFinanceCategories(token: authManager.token)) ?? []
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
func deleteCategory(_ cat: FinanceCategory) async {
|
||||
try? await APIService.shared.deleteFinanceCategory(token: authManager.token, id: cat.id)
|
||||
await load(refresh: true)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - FinanceCategoryFormView
|
||||
|
||||
struct FinanceCategoryFormView: View {
|
||||
@Binding var isPresented: Bool
|
||||
@EnvironmentObject var authManager: AuthManager
|
||||
let category: FinanceCategory?
|
||||
let defaultType: String
|
||||
let onSaved: () async -> Void
|
||||
|
||||
@State private var name = ""
|
||||
@State private var type: String
|
||||
@State private var emoji = ""
|
||||
@State private var isLoading = false
|
||||
|
||||
let emojis = ["💸","💰","🏠","🍔","🚗","🎓","💊","✈️","👗","🎮","📱","🛒","⚡","🐾","🎵","💄","🍺","🎁","🏋️","📚"]
|
||||
|
||||
init(isPresented: Binding<Bool>, category: FinanceCategory?, defaultType: String, onSaved: @escaping () async -> Void) {
|
||||
self._isPresented = isPresented
|
||||
self.category = category
|
||||
self.defaultType = defaultType
|
||||
self.onSaved = onSaved
|
||||
self._name = State(initialValue: category?.name ?? "")
|
||||
self._type = State(initialValue: category?.type ?? defaultType)
|
||||
self._emoji = State(initialValue: category?.emoji ?? "")
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(hex: "06060f").ignoresSafeArea()
|
||||
VStack(spacing: 0) {
|
||||
RoundedRectangle(cornerRadius: 3)
|
||||
.fill(Color.white.opacity(0.2)).frame(width: 40, height: 4).padding(.top, 12)
|
||||
HStack {
|
||||
Button("Отмена") { isPresented = false }.foregroundColor(Color(hex: "8888aa"))
|
||||
Spacer()
|
||||
Text(category == nil ? "Новая категория" : "Редактировать").font(.headline).foregroundColor(.white)
|
||||
Spacer()
|
||||
Button(action: save) {
|
||||
if isLoading { ProgressView().tint(Color(hex: "0D9488")).scaleEffect(0.8) }
|
||||
else { Text("Сохранить").foregroundColor(name.isEmpty ? Color(hex: "8888aa") : Color(hex: "0D9488")).fontWeight(.semibold) }
|
||||
}.disabled(name.isEmpty || isLoading)
|
||||
}
|
||||
.padding(.horizontal, 20).padding(.vertical, 16)
|
||||
Divider().background(Color.white.opacity(0.1))
|
||||
ScrollView {
|
||||
VStack(spacing: 16) {
|
||||
HStack(spacing: 0) {
|
||||
Button(action: { type = "expense" }) {
|
||||
Text("Расход").font(.callout.bold())
|
||||
.foregroundColor(type == "expense" ? .black : Color(hex: "ff4757"))
|
||||
.frame(maxWidth: .infinity).padding(.vertical, 10)
|
||||
.background(type == "expense" ? Color(hex: "ff4757") : Color.clear)
|
||||
}
|
||||
Button(action: { type = "income" }) {
|
||||
Text("Доход").font(.callout.bold())
|
||||
.foregroundColor(type == "income" ? .black : Color(hex: "0D9488"))
|
||||
.frame(maxWidth: .infinity).padding(.vertical, 10)
|
||||
.background(type == "income" ? Color(hex: "0D9488") : Color.clear)
|
||||
}
|
||||
}
|
||||
.background(Color.white.opacity(0.07)).cornerRadius(12)
|
||||
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Label("Название", systemImage: "pencil").font(.caption).foregroundColor(Color(hex: "8888aa"))
|
||||
TextField("Название категории", text: $name)
|
||||
.foregroundColor(.white).padding(14)
|
||||
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07)))
|
||||
}
|
||||
|
||||
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(emojis, id: \.self) { e in
|
||||
Button(action: { emoji = e }) {
|
||||
Text(e).font(.title3)
|
||||
.frame(width: 44, height: 44)
|
||||
.background(Circle().fill(emoji == e ? Color(hex: "0D9488").opacity(0.25) : Color.white.opacity(0.05)))
|
||||
.overlay(Circle().stroke(emoji == e ? Color(hex: "0D9488") : Color.clear, lineWidth: 2))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}.padding(20)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func save() {
|
||||
isLoading = true
|
||||
Task {
|
||||
let req = CreateFinanceCategoryRequest(name: name, type: type, emoji: emoji.isEmpty ? nil : emoji, budget: nil)
|
||||
if let cat = category {
|
||||
try? await APIService.shared.updateFinanceCategory(token: authManager.token, id: cat.id, request: req)
|
||||
} else {
|
||||
try? await APIService.shared.createFinanceCategory(token: authManager.token, request: req)
|
||||
}
|
||||
await onSaved()
|
||||
await MainActor.run { isPresented = false }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user