feat: add habit creation, savings tabs, fix dashboard, fix tab order
This commit is contained in:
@@ -1,37 +1,56 @@
|
||||
import SwiftUI
|
||||
|
||||
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
|
||||
Picker("", selection: $selectedTab) {
|
||||
Text("Обзор").tag(0)
|
||||
Text("Транзакции").tag(1)
|
||||
}
|
||||
.pickerStyle(.segmented)
|
||||
.padding(.horizontal)
|
||||
.padding(.bottom, 12)
|
||||
|
||||
if selectedTab == 0 {
|
||||
SavingsOverviewTab()
|
||||
} else {
|
||||
SavingsTransactionsTab()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SavingsOverviewTab: View {
|
||||
@EnvironmentObject var authManager: AuthManager
|
||||
@State private var categories: [SavingsCategory] = []
|
||||
@State private var stats: SavingsStats?
|
||||
@State private var isLoading = true
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
Color(hex: "0a0a1a").ignoresSafeArea()
|
||||
VStack(spacing: 0) {
|
||||
HStack {
|
||||
Text("Накопления").font(.title.bold()).foregroundColor(.white)
|
||||
Spacer()
|
||||
}.padding()
|
||||
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,6 +69,97 @@ struct SavingsView: View {
|
||||
}
|
||||
}
|
||||
|
||||
struct SavingsTransactionsTab: View {
|
||||
@EnvironmentObject var authManager: AuthManager
|
||||
@State private var transactions: [SavingsTransaction] = []
|
||||
@State private var isLoading = 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)
|
||||
}
|
||||
}
|
||||
.listStyle(.plain)
|
||||
.scrollContentBackground(.hidden)
|
||||
}
|
||||
}
|
||||
.task { await loadData() }
|
||||
.refreshable { await loadData(refresh: true) }
|
||||
}
|
||||
|
||||
func loadData(refresh: Bool = false) async {
|
||||
if !refresh { isLoading = true }
|
||||
transactions = (try? await APIService.shared.getSavingsTransactions(token: authManager.token)) ?? []
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
|
||||
struct SavingsTransactionRow: 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))
|
||||
.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"))
|
||||
}
|
||||
|
||||
VStack(alignment: .leading, spacing: 3) {
|
||||
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"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Text("\(transaction.isDeposit ? "+" : "-")\(formatAmount(transaction.amount))")
|
||||
.font(.callout.bold())
|
||||
.foregroundColor(transaction.isDeposit ? Color(hex: "00d4aa") : 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])"
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SavingsTotalCard
|
||||
|
||||
struct SavingsTotalCard: View {
|
||||
let stats: SavingsStats
|
||||
|
||||
@@ -98,6 +208,8 @@ struct SavingsTotalCard: View {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SavingsCategoryCard
|
||||
|
||||
struct SavingsCategoryCard: View {
|
||||
let category: SavingsCategory
|
||||
@State private var appeared = false
|
||||
|
||||
Reference in New Issue
Block a user