feat: full Pulse Mobile implementation - all modules

- Phase 0: project.yml fixes (CODE_SIGN_ENTITLEMENTS confirmed)
- Phase 1: Enhanced models (HabitModels, TaskModels, FinanceModels, SavingsModels, UserModels)
- Phase 1: Enhanced APIService with all endpoints (habits/log/stats, tasks/uncomplete, finance/analytics, savings/*)
- Phase 2: DashboardView rewrite - day progress bar, 4 stat cards, habit/task lists with Undo (3 sec)
- Phase 3: TrackerView - HabitListView (streak badge, swipe delete, archive), TaskListView (priority, overdue), StatisticsView (heatmap 84 days, line chart, bar chart via Swift Charts)
- Phase 4: FinanceView rewrite - month picker, summary card, top expenses progress bars, pie chart, line chart, transactions by day, analytics tab with bar chart + month comparison
- Phase 5: SavingsView rewrite - overview with overdue block, categories tab with type icons, operations tab with category filter + add sheet
- Phase 6: SettingsView - dark/light theme, profile edit, telegram chat id, notifications toggle + time, timezone picker, logout
- Added: AddHabitView with weekly day selector + interval days
- Added: AddTaskView with icon/color/due date picker
- Haptic feedback on all toggle actions
This commit is contained in:
Cosmo
2026-03-25 17:14:59 +00:00
parent 17b82a874f
commit e7af51af10
18 changed files with 2959 additions and 628 deletions

View File

@@ -1,65 +1,160 @@
import SwiftUI
// MARK: - SavingsView
struct SavingsView: View {
@EnvironmentObject var authManager: AuthManager
@State private var selectedTab = 0
var body: some View {
ZStack {
Color(hex: "0a0a1a").ignoresSafeArea()
VStack(spacing: 0) {
// Header
HStack {
Text("Накопления").font(.title.bold()).foregroundColor(.white)
Spacer()
}.padding()
// Segment control
}
.padding(.horizontal)
.padding(.top, 16)
.padding(.bottom, 12)
Picker("", selection: $selectedTab) {
Text("Обзор").tag(0)
Text("Транзакции").tag(1)
Text("Категории").tag(1)
Text("Операции").tag(2)
}
.pickerStyle(.segmented)
.padding(.horizontal)
.padding(.bottom, 12)
if selectedTab == 0 {
SavingsOverviewTab()
} else {
SavingsTransactionsTab()
switch selectedTab {
case 0: SavingsOverviewTab2()
case 1: SavingsCategoriesTab()
default: SavingsOperationsTab()
}
}
}
}
}
struct SavingsOverviewTab: View {
// MARK: - SavingsOverviewTab2
struct SavingsOverviewTab2: View {
@EnvironmentObject var authManager: AuthManager
@State private var categories: [SavingsCategory] = []
@State private var stats: SavingsStats?
@State private var isLoading = true
var recurringCategories: [SavingsCategory] { categories.filter { $0.isRecurring == true } }
var hasOverdue: Bool { (stats?.overdueCount ?? 0) > 0 }
var body: some View {
Group {
if isLoading {
ProgressView().tint(Color(hex: "00d4aa")).padding(.top, 40)
Spacer()
} else {
ScrollView {
VStack(spacing: 16) {
if let s = stats { SavingsTotalCard(stats: s) }
VStack(spacing: 10) {
ForEach(categories) { cat in SavingsCategoryCard(category: cat) }
}.padding(.horizontal)
ScrollView {
VStack(spacing: 16) {
if isLoading {
ProgressView().tint(Color(hex: "0D9488")).padding(.top, 40)
} else {
// Total Balance Card
if let s = stats {
VStack(spacing: 16) {
VStack(spacing: 6) {
Text("Общий баланс").font(.subheadline).foregroundColor(Color(hex: "8888aa"))
Text(formatAmt(s.totalBalance ?? 0))
.font(.system(size: 36, weight: .bold)).foregroundColor(.white)
}
HStack {
VStack(spacing: 4) {
Text("Пополнения").font(.caption).foregroundColor(Color(hex: "8888aa"))
Text("+\(formatAmt(s.totalDeposits ?? 0))")
.font(.callout.bold()).foregroundColor(Color(hex: "0D9488"))
}
Spacer()
VStack(spacing: 4) {
Text("Снятия").font(.caption).foregroundColor(Color(hex: "8888aa"))
Text("-\(formatAmt(s.totalWithdrawals ?? 0))")
.font(.callout.bold()).foregroundColor(Color(hex: "ff4757"))
}
Spacer()
VStack(spacing: 4) {
Text("Категорий").font(.caption).foregroundColor(Color(hex: "8888aa"))
Text("\(s.categoriesCount ?? 0)")
.font(.callout.bold()).foregroundColor(.white)
}
}
}
.padding(20)
.background(LinearGradient(colors: [Color(hex: "1a1a3e"), Color(hex: "12122a")], startPoint: .topLeading, endPoint: .bottomTrailing))
.cornerRadius(20)
.overlay(RoundedRectangle(cornerRadius: 20).stroke(Color(hex: "0D9488").opacity(0.3), lineWidth: 1))
.padding(.horizontal)
}
// Overdue block
if hasOverdue, let s = stats {
HStack(spacing: 12) {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(Color(hex: "ff4757"))
.font(.title3)
VStack(alignment: .leading, spacing: 2) {
Text("Просроченные платежи").font(.callout.bold()).foregroundColor(Color(hex: "ff4757"))
Text("\(s.overdueCount ?? 0) платежей на сумму \(formatAmt(s.overdueAmount ?? 0))")
.font(.caption).foregroundColor(.white.opacity(0.7))
}
Spacer()
}
.padding(16)
.background(RoundedRectangle(cornerRadius: 14).fill(Color(hex: "ff4757").opacity(0.12)))
.overlay(RoundedRectangle(cornerRadius: 14).stroke(Color(hex: "ff4757").opacity(0.3), lineWidth: 1))
.padding(.horizontal)
}
// Categories progress
if !categories.isEmpty {
VStack(alignment: .leading, spacing: 10) {
Text("Прогресс по категориям")
.font(.subheadline.bold()).foregroundColor(.white).padding(.horizontal)
ForEach(categories.filter { $0.isClosed != true }) { cat in
SavingsCategoryCard(category: cat)
}
}
}
// Monthly payments
if !recurringCategories.isEmpty {
VStack(alignment: .leading, spacing: 10) {
Text("Ежемесячные платежи")
.font(.subheadline.bold()).foregroundColor(.white).padding(.horizontal)
ForEach(recurringCategories) { cat in
HStack(spacing: 12) {
ZStack {
Circle().fill(Color(hex: cat.colorHex).opacity(0.15)).frame(width: 40, height: 40)
Image(systemName: cat.icon).foregroundColor(Color(hex: cat.colorHex)).font(.body)
}
VStack(alignment: .leading, spacing: 2) {
Text(cat.name).font(.callout).foregroundColor(.white)
if let day = cat.recurringDay {
Text("\(day) числа каждого месяца").font(.caption2).foregroundColor(Color(hex: "8888aa"))
}
}
Spacer()
Text(formatAmt(cat.recurringAmount ?? 0))
.font(.callout.bold()).foregroundColor(Color(hex: cat.colorHex))
}
.padding(14)
.background(RoundedRectangle(cornerRadius: 14).fill(Color.white.opacity(0.04)))
.padding(.horizontal)
}
}
}
}
Spacer(minLength: 80)
}
.padding(.top, 8)
}
.task { await loadData() }
.refreshable { await loadData(refresh: true) }
.task { await load() }
.refreshable { await load(refresh: true) }
}
func loadData(refresh: Bool = false) async {
func load(refresh: Bool = false) async {
if !refresh { isLoading = true }
async let cats = APIService.shared.getSavingsCategories(token: authManager.token)
async let st = APIService.shared.getSavingsStats(token: authManager.token)
@@ -67,228 +162,466 @@ struct SavingsOverviewTab: View {
stats = try? await st
isLoading = false
}
func formatAmt(_ v: Double) -> String {
v >= 1_000_000 ? String(format: "%.2f млн ₽", v / 1_000_000) : String(format: "%.0f ₽", v)
}
}
struct SavingsTransactionsTab: View {
// MARK: - SavingsCategoriesTab
struct SavingsCategoriesTab: View {
@EnvironmentObject var authManager: AuthManager
@State private var transactions: [SavingsTransaction] = []
@State private var categories: [SavingsCategory] = []
@State private var isLoading = true
@State private var showAdd = false
@State private var editingCategory: SavingsCategory?
var active: [SavingsCategory] { categories.filter { $0.isClosed != true } }
var closed: [SavingsCategory] { categories.filter { $0.isClosed == true } }
var body: some View {
Group {
if isLoading {
ProgressView().tint(Color(hex: "00d4aa")).padding(.top, 40)
Spacer()
} else if transactions.isEmpty {
VStack(spacing: 12) {
Text("💸").font(.system(size: 50))
Text("Нет транзакций").foregroundColor(Color(hex: "8888aa"))
}.padding(.top, 60)
Spacer()
} else {
List {
ForEach(transactions) { tx in
SavingsTransactionRow(transaction: tx)
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
ZStack(alignment: .bottomTrailing) {
Group {
if isLoading {
ProgressView().tint(Color(hex: "0D9488")).padding(.top, 40)
Spacer()
} else if active.isEmpty {
VStack { EmptyState(icon: "building.columns", text: "Нет категорий"); Spacer() }
} else {
List {
Section(header: Text("Активные").foregroundColor(Color(hex: "8888aa"))) {
ForEach(active) { cat in
SavingsCategoryRow(category: cat)
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
.onTapGesture { editingCategory = cat }
}
.onDelete { idx in
let toDelete = idx.map { active[$0] }
Task {
for c in toDelete { try? await APIService.shared.deleteSavingsCategory(token: authManager.token, id: c.id) }
await load(refresh: true)
}
}
}
if !closed.isEmpty {
Section(header: Text("Закрытые").foregroundColor(Color(hex: "8888aa"))) {
ForEach(closed) { cat in
SavingsCategoryRow(category: cat)
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
}
}
}
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
}
.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)
}
.task { await load() }
.sheet(isPresented: $showAdd) {
AddSavingsCategoryView(isPresented: $showAdd) { await load(refresh: true) }
.presentationDetents([.large])
.presentationDragIndicator(.visible)
.presentationBackground(Color(hex: "0a0a1a"))
}
.task { await loadData() }
.refreshable { await loadData(refresh: true) }
}
func loadData(refresh: Bool = false) async {
func load(refresh: Bool = false) async {
if !refresh { isLoading = true }
transactions = (try? await APIService.shared.getSavingsTransactions(token: authManager.token)) ?? []
categories = (try? await APIService.shared.getSavingsCategories(token: authManager.token)) ?? []
isLoading = false
}
}
struct SavingsTransactionRow: View {
// MARK: - SavingsCategoryRow
struct SavingsCategoryRow: View {
let category: SavingsCategory
var body: some View {
HStack(spacing: 12) {
ZStack {
Circle().fill(Color(hex: category.colorHex).opacity(0.15)).frame(width: 44, height: 44)
Image(systemName: category.icon).foregroundColor(Color(hex: category.colorHex)).font(.title3)
}
VStack(alignment: .leading, spacing: 3) {
HStack(spacing: 4) {
Text(category.typeEmoji)
Text(category.name).font(.callout.bold()).foregroundColor(.white)
}
Text(category.typeLabel).font(.caption).foregroundColor(Color(hex: "8888aa"))
}
Spacer()
Text(formatAmt(category.currentAmount ?? 0))
.font(.callout.bold()).foregroundColor(Color(hex: category.colorHex))
}
.padding(14)
.background(RoundedRectangle(cornerRadius: 14).fill(Color.white.opacity(0.04)))
.padding(.horizontal)
.padding(.vertical, 2)
}
func formatAmt(_ v: Double) -> String {
v >= 1_000_000 ? String(format: "%.2f млн ₽", v / 1_000_000) : String(format: "%.0f ₽", v)
}
}
// MARK: - AddSavingsCategoryView
struct AddSavingsCategoryView: View {
@Binding var isPresented: Bool
@EnvironmentObject var authManager: AuthManager
let onAdded: () async -> Void
@State private var name = ""
@State private var isDeposit = false
@State private var isRecurring = false
@State private var isAccount = false
@State private var recurringAmount = ""
@State private var interestRate = ""
@State private var isLoading = false
var body: some View {
ZStack {
Color(hex: "0a0a1a").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: "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) {
fieldLabel("Название") {
TextField("Например: На машину", text: $name)
.foregroundColor(.white).padding(14)
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07)))
}
VStack(alignment: .leading, spacing: 8) {
Label("Тип", systemImage: "tag.fill").font(.caption).foregroundColor(Color(hex: "8888aa"))
HStack(spacing: 8) {
TypeButton(label: "💰 Накопление", selected: !isDeposit && !isRecurring && !isAccount) { isDeposit = false; isRecurring = false; isAccount = false }
TypeButton(label: "🏦 Вклад", selected: isDeposit) { isDeposit = true; isRecurring = false; isAccount = false }
}
HStack(spacing: 8) {
TypeButton(label: "🔄 Регулярные", selected: isRecurring) { isDeposit = false; isRecurring = true; isAccount = false }
TypeButton(label: "🏧 Счёт", selected: isAccount) { isDeposit = false; isRecurring = false; isAccount = true }
}
}
if isRecurring {
fieldLabel("Сумма / мес. (₽)") {
TextField("0", text: $recurringAmount).keyboardType(.decimalPad)
.foregroundColor(.white).padding(14)
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07)))
}
}
if isDeposit {
fieldLabel("Ставка (%)") {
TextField("0.0", text: $interestRate).keyboardType(.decimalPad)
.foregroundColor(.white).padding(14)
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.07)))
}
}
}.padding(20)
}
}
}
}
@ViewBuilder func fieldLabel<Content: View>(_ label: String, @ViewBuilder content: () -> Content) -> some View {
VStack(alignment: .leading, spacing: 8) {
Label(label, systemImage: "pencil").font(.caption).foregroundColor(Color(hex: "8888aa"))
content()
}
}
func save() {
isLoading = true
Task {
var params: [String: Any] = ["name": name, "is_deposit": isDeposit, "is_recurring": isRecurring, "is_account": isAccount]
if isRecurring, let a = Double(recurringAmount) { params["recurring_amount"] = a }
if isDeposit, let r = Double(interestRate) { params["interest_rate"] = r }
if let body = try? JSONSerialization.data(withJSONObject: params) {
try? await APIService.shared.createSavingsCategory(token: authManager.token, body: body)
}
await onAdded()
await MainActor.run { isPresented = false }
}
}
}
struct TypeButton: View {
let label: String
let selected: Bool
let action: () -> Void
var body: some View {
Button(action: action) {
Text(label).font(.caption.bold())
.foregroundColor(selected ? .black : .white)
.frame(maxWidth: .infinity).padding(.vertical, 10)
.background(RoundedRectangle(cornerRadius: 10).fill(selected ? Color(hex: "0D9488") : Color.white.opacity(0.07)))
}
}
}
// MARK: - SavingsOperationsTab
struct SavingsOperationsTab: View {
@EnvironmentObject var authManager: AuthManager
@State private var transactions: [SavingsTransaction] = []
@State private var categories: [SavingsCategory] = []
@State private var selectedCategoryId: Int? = nil
@State private var isLoading = true
@State private var showAdd = false
var filtered: [SavingsTransaction] {
guard let cid = selectedCategoryId else { return transactions }
return transactions.filter { $0.categoryId == cid }
}
var body: some View {
ZStack(alignment: .bottomTrailing) {
VStack(spacing: 0) {
// Filter pills
if !categories.isEmpty {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 8) {
FilterPill(label: "Все", selected: selectedCategoryId == nil) { selectedCategoryId = nil }
ForEach(categories.filter { $0.isClosed != true }) { cat in
FilterPill(label: cat.name, selected: selectedCategoryId == cat.id) { selectedCategoryId = cat.id }
}
}
.padding(.horizontal)
}
.padding(.bottom, 8)
}
if isLoading {
ProgressView().tint(Color(hex: "0D9488")).padding(.top, 40)
Spacer()
} else if filtered.isEmpty {
VStack { EmptyState(icon: "arrow.left.arrow.right", text: "Нет операций"); Spacer() }
} else {
List {
ForEach(filtered) { tx in
SavingsTransactionRow2(transaction: tx)
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
}
.onDelete { idx in
let toDelete = idx.map { filtered[$0] }
Task {
for t in toDelete { try? await APIService.shared.deleteSavingsTransaction(token: authManager.token, id: t.id) }
await load(refresh: true)
}
}
}
.listStyle(.plain)
.scrollContentBackground(.hidden)
}
}
.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)
}
.task { await load() }
.sheet(isPresented: $showAdd) {
AddSavingsTransactionView(isPresented: $showAdd, categories: categories) { await load(refresh: true) }
.presentationDetents([.medium, .large])
.presentationDragIndicator(.visible)
.presentationBackground(Color(hex: "0a0a1a"))
}
}
func load(refresh: Bool = false) async {
if !refresh { isLoading = true }
async let txs = APIService.shared.getSavingsTransactions(token: authManager.token, categoryId: selectedCategoryId)
async let cats = APIService.shared.getSavingsCategories(token: authManager.token)
transactions = (try? await txs) ?? []
categories = (try? await cats) ?? []
isLoading = false
}
}
struct FilterPill: View {
let label: String
let selected: Bool
let action: () -> Void
var body: some View {
Button(action: action) {
Text(label).font(.caption.bold())
.foregroundColor(selected ? .black : .white)
.padding(.horizontal, 14).padding(.vertical, 8)
.background(RoundedRectangle(cornerRadius: 20).fill(selected ? Color(hex: "0D9488") : Color.white.opacity(0.08)))
}
}
}
struct SavingsTransactionRow2: View {
let transaction: SavingsTransaction
var body: some View {
HStack(spacing: 12) {
ZStack {
Circle()
.fill((transaction.isDeposit ? Color(hex: "00d4aa") : Color(hex: "ff4757")).opacity(0.15))
.fill((transaction.isDeposit ? Color(hex: "0D9488") : Color(hex: "ff4757")).opacity(0.15))
.frame(width: 40, height: 40)
Image(systemName: transaction.isDeposit ? "arrow.down.circle.fill" : "arrow.up.circle.fill")
.foregroundColor(transaction.isDeposit ? Color(hex: "00d4aa") : Color(hex: "ff4757"))
.foregroundColor(transaction.isDeposit ? Color(hex: "0D9488") : Color(hex: "ff4757"))
}
VStack(alignment: .leading, spacing: 3) {
Text(transaction.categoryName ?? "Без категории")
.font(.callout).foregroundColor(.white)
VStack(alignment: .leading, spacing: 2) {
Text(transaction.categoryName ?? "Без категории").font(.callout).foregroundColor(.white)
HStack(spacing: 6) {
if let userName = transaction.userName {
Text(userName).font(.caption).foregroundColor(Color(hex: "8888aa"))
}
if let date = transaction.date {
Text(formatDate(date)).font(.caption2).foregroundColor(Color(hex: "8888aa"))
}
if let name = transaction.userName { Text(name).font(.caption).foregroundColor(Color(hex: "8888aa")) }
Text(transaction.dateFormatted).font(.caption2).foregroundColor(Color(hex: "8888aa"))
}
if let desc = transaction.description, !desc.isEmpty {
Text(desc).font(.caption2).foregroundColor(Color(hex: "8888aa"))
}
}
Spacer()
Text("\(transaction.isDeposit ? "+" : "-")\(formatAmount(transaction.amount))")
Text("\(transaction.isDeposit ? "+" : "-")\(formatAmt(transaction.amount))")
.font(.callout.bold())
.foregroundColor(transaction.isDeposit ? Color(hex: "00d4aa") : Color(hex: "ff4757"))
.foregroundColor(transaction.isDeposit ? Color(hex: "0D9488") : Color(hex: "ff4757"))
}
.padding(12)
.background(RoundedRectangle(cornerRadius: 12).fill(Color.white.opacity(0.04)))
.padding(.horizontal)
.padding(.vertical, 2)
}
func formatAmount(_ v: Double) -> String {
if v >= 1_000_000 { return String(format: "%.1f млн ₽", v / 1_000_000) }
return String(format: "%.0f ₽", v)
}
func formatDate(_ s: String) -> String {
let parts = s.prefix(10).split(separator: "-")
guard parts.count == 3 else { return String(s.prefix(10)) }
return "\(parts[2]).\(parts[1]).\(parts[0])"
}
func formatAmt(_ v: Double) -> String { v >= 1_000_000 ? String(format: "%.2f млн ₽", v / 1_000_000) : String(format: "%.0f ₽", v) }
}
// MARK: - SavingsTotalCard
// MARK: - AddSavingsTransactionView
struct AddSavingsTransactionView: View {
@Binding var isPresented: Bool
@EnvironmentObject var authManager: AuthManager
let categories: [SavingsCategory]
let onAdded: () async -> Void
@State private var amount = ""
@State private var description = ""
@State private var type = "deposit"
@State private var selectedCategoryId: Int? = nil
@State private var isLoading = false
var isDeposit: Bool { type == "deposit" }
struct SavingsTotalCard: View {
let stats: SavingsStats
var body: some View {
VStack(spacing: 20) {
VStack(spacing: 6) {
Text("Общий баланс").font(.subheadline).foregroundColor(Color(hex: "8888aa"))
Text(formatAmount(stats.totalBalance ?? 0))
.font(.system(size: 36, weight: .bold))
.foregroundColor(.white)
}
HStack(spacing: 0) {
VStack(spacing: 4) {
Text("Пополнения").font(.caption).foregroundColor(Color(hex: "8888aa"))
Text("+\(formatAmount(stats.totalDeposits ?? 0))").font(.callout.bold()).foregroundColor(Color(hex: "00d4aa"))
ZStack {
Color(hex: "0a0a1a").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: "0D9488")).scaleEffect(0.8) }
else { Text("Добавить").foregroundColor((amount.isEmpty || selectedCategoryId == nil) ? Color(hex: "8888aa") : Color(hex: "0D9488")).fontWeight(.semibold) }
}.disabled(amount.isEmpty || selectedCategoryId == nil || isLoading)
}
Spacer()
VStack(spacing: 4) {
Text("Снятия").font(.caption).foregroundColor(Color(hex: "8888aa"))
Text("-\(formatAmount(stats.totalWithdrawals ?? 0))").font(.callout.bold()).foregroundColor(Color(hex: "ff4757"))
}
Spacer()
VStack(spacing: 4) {
Text("Категорий").font(.caption).foregroundColor(Color(hex: "8888aa"))
Text("\(stats.categoriesCount ?? 0)").font(.callout.bold()).foregroundColor(.white)
}
}
}
.padding(20)
.background(
LinearGradient(colors: [Color(hex: "1a1a3e"), Color(hex: "12122a")], startPoint: .topLeading, endPoint: .bottomTrailing)
)
.cornerRadius(20)
.overlay(RoundedRectangle(cornerRadius: 20).stroke(Color(hex: "00d4aa").opacity(0.3), lineWidth: 1))
.padding(.horizontal)
}
func formatAmount(_ v: Double) -> String {
if v >= 1_000_000 {
return String(format: "%.2f млн ₽", v / 1_000_000)
} else if v >= 1000 {
return String(format: "%.0f ₽", v)
}
return String(format: "%.0f ₽", v)
}
}
// MARK: - SavingsCategoryCard
struct SavingsCategoryCard: View {
let category: SavingsCategory
@State private var appeared = false
var body: some View {
VStack(spacing: 12) {
HStack(spacing: 12) {
ZStack {
Circle()
.fill(Color(hex: category.color).opacity(0.2))
.frame(width: 46, height: 46)
Image(systemName: category.icon)
.foregroundColor(Color(hex: category.color))
.font(.title3)
}
VStack(alignment: .leading, spacing: 4) {
Text(category.name).font(.callout.bold()).foregroundColor(.white)
Text(category.typeLabel).font(.caption).foregroundColor(Color(hex: "8888aa"))
}
Spacer()
VStack(alignment: .trailing, spacing: 2) {
Text(formatAmount(category.currentAmount ?? 0))
.font(.callout.bold())
.foregroundColor(Color(hex: category.color))
if let end = category.depositEndDate {
Text("до \(formatDate(end))")
.font(.caption2)
.foregroundColor(Color(hex: "8888aa"))
}
}
}
if category.isDeposit == true, let target = category.depositAmount, target > 0 {
let progress = min((category.currentAmount ?? 0) / target, 1.0)
VStack(spacing: 4) {
GeometryReader { geo in
ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: 4).fill(Color.white.opacity(0.08))
RoundedRectangle(cornerRadius: 4)
.fill(LinearGradient(colors: [Color(hex: "ffa502"), Color(hex: "ff6b35")], startPoint: .leading, endPoint: .trailing))
.frame(width: geo.size.width * CGFloat(appeared ? progress : 0))
.animation(.easeInOut(duration: 0.8), value: appeared)
.padding(.horizontal, 20).padding(.vertical, 16)
Divider().background(Color.white.opacity(0.1))
ScrollView {
VStack(spacing: 20) {
// Type toggle
HStack(spacing: 0) {
Button(action: { type = "deposit" }) {
Text("Пополнение ↓")
.font(.callout.bold()).foregroundColor(isDeposit ? .black : Color(hex: "0D9488"))
.frame(maxWidth: .infinity).padding(.vertical, 12)
.background(isDeposit ? Color(hex: "0D9488") : Color.clear)
}
Button(action: { type = "withdrawal" }) {
Text("Снятие ↑")
.font(.callout.bold()).foregroundColor(!isDeposit ? .black : Color(hex: "ff4757"))
.frame(maxWidth: .infinity).padding(.vertical, 12)
.background(!isDeposit ? Color(hex: "ff4757") : Color.clear)
}
}
}
.frame(height: 6)
HStack {
Text("\(Int(progress * 100))%")
.font(.caption2).foregroundColor(Color(hex: "ffa502"))
Spacer()
Text("цель: \(formatAmount(target))")
.font(.caption2).foregroundColor(Color(hex: "8888aa"))
}
.background(Color.white.opacity(0.07)).cornerRadius(12)
HStack {
Text(isDeposit ? "+" : "").font(.title.bold()).foregroundColor(isDeposit ? Color(hex: "0D9488") : Color(hex: "ff4757"))
TextField("0", text: $amount).keyboardType(.decimalPad)
.font(.system(size: 32, 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: "tag.fill").font(.caption).foregroundColor(Color(hex: "8888aa"))
ForEach(categories.filter { $0.isClosed != true }) { cat in
Button(action: { selectedCategoryId = selectedCategoryId == cat.id ? nil : cat.id }) {
HStack(spacing: 10) {
Image(systemName: cat.icon).foregroundColor(Color(hex: cat.colorHex)).font(.body)
Text(cat.name).font(.callout).foregroundColor(.white)
Spacer()
if selectedCategoryId == cat.id {
Image(systemName: "checkmark").foregroundColor(Color(hex: "0D9488"))
}
}
.padding(12)
.background(RoundedRectangle(cornerRadius: 12).fill(selectedCategoryId == cat.id ? Color(hex: "0D9488").opacity(0.15) : Color.white.opacity(0.05)))
}
}
}
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)))
}
}.padding(20)
}
}
}
.padding(16)
.background(
RoundedRectangle(cornerRadius: 16)
.fill(Color.white.opacity(0.04))
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color(hex: category.color).opacity(0.15), lineWidth: 1))
)
.opacity(appeared ? 1 : 0)
.offset(y: appeared ? 0 : 20)
.onAppear {
withAnimation(.easeOut(duration: 0.4)) { appeared = true }
}
func save() {
guard let a = Double(amount.replacingOccurrences(of: ",", with: ".")),
let cid = selectedCategoryId else { return }
isLoading = true
Task {
let req = CreateSavingsTransactionRequest(categoryId: cid, amount: a, type: type, description: description.isEmpty ? nil : description)
try? await APIService.shared.createSavingsTransaction(token: authManager.token, request: req)
await onAdded()
await MainActor.run { isPresented = false }
}
}
func formatAmount(_ v: Double) -> String {
if v >= 1_000_000 { return String(format: "%.2f млн ₽", v / 1_000_000) }
return String(format: "%.0f ₽", v)
}
func formatDate(_ s: String) -> String {
let parts = s.prefix(10).split(separator: "-")
guard parts.count == 3 else { return s }
return "\(parts[2]).\(parts[1]).\(parts[0])"
}
}