This commit is contained in:
2026-03-20 10:32:36 +03:00
parent 99d985ca45
commit 047d5bdf36
17 changed files with 982 additions and 246 deletions
+87
View File
@@ -1,10 +1,15 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
import '../models/transaction.dart';
const _uuid = Uuid();
class StorageService {
static const _transactionsKey = 'transactions';
static const _budgetKey = 'monthly_budget';
static const _currencyKey = 'currency_symbol';
static const _themeKey = 'is_dark_mode';
final SharedPreferences _prefs;
@@ -55,4 +60,86 @@ class StorageService {
await _prefs.setDouble(_budgetKey, budget);
}
}
String loadCurrency() {
return _prefs.getString(_currencyKey) ?? '\$';
}
Future<void> saveCurrency(String symbol) async {
await _prefs.setString(_currencyKey, symbol);
}
bool loadThemeMode() {
return _prefs.getBool(_themeKey) ?? true; // default dark
}
Future<void> saveThemeMode(bool isDark) async {
await _prefs.setBool(_themeKey, isDark);
}
// Process recurring transactions
Future<void> processRecurringTransactions() async {
final transactions = loadTransactions();
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
bool hasChanges = false;
for (final tx in transactions) {
if (tx.recurrence == RecurrenceType.none) continue;
final lastOccurrence = tx.lastOccurrence ?? tx.date;
final lastDate = DateTime(
lastOccurrence.year,
lastOccurrence.month,
lastOccurrence.day,
);
bool shouldCreate = false;
switch (tx.recurrence) {
case RecurrenceType.daily:
shouldCreate = today.isAfter(lastDate);
break;
case RecurrenceType.weekly:
final daysDiff = today.difference(lastDate).inDays;
shouldCreate = daysDiff >= 7;
break;
case RecurrenceType.monthly:
shouldCreate = (today.year > lastDate.year ||
(today.year == lastDate.year &&
today.month > lastDate.month)) &&
today.day >= lastDate.day;
break;
case RecurrenceType.none:
break;
}
if (shouldCreate) {
// Create new occurrence
final newTx = Transaction(
id: _uuid.v4(),
amount: tx.amount,
category: tx.category,
type: tx.type,
date: today,
note: tx.note,
recurrence: tx.recurrence,
lastOccurrence: today,
);
transactions.add(newTx);
// Update original transaction's lastOccurrence
final index = transactions.indexWhere((t) => t.id == tx.id);
if (index != -1) {
transactions[index] = tx.copyWith(lastOccurrence: today);
}
hasChanges = true;
}
}
if (hasChanges) {
await saveTransactions(transactions);
}
}
}