mirror of
https://github.com/koloideal/Casha.git
synced 2026-06-10 10:25:28 +03:00
update
This commit is contained in:
@@ -3,14 +3,15 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/transaction.dart';
|
||||
|
||||
class StorageService {
|
||||
static const _key = 'transactions';
|
||||
static const _transactionsKey = 'transactions';
|
||||
static const _budgetKey = 'monthly_budget';
|
||||
|
||||
final SharedPreferences _prefs;
|
||||
|
||||
StorageService(this._prefs);
|
||||
|
||||
List<Transaction> loadTransactions() {
|
||||
final raw = _prefs.getString(_key);
|
||||
final raw = _prefs.getString(_transactionsKey);
|
||||
if (raw == null) return [];
|
||||
final list = jsonDecode(raw) as List<dynamic>;
|
||||
return list
|
||||
@@ -20,7 +21,7 @@ class StorageService {
|
||||
|
||||
Future<void> saveTransactions(List<Transaction> transactions) async {
|
||||
final encoded = jsonEncode(transactions.map((t) => t.toJson()).toList());
|
||||
await _prefs.setString(_key, encoded);
|
||||
await _prefs.setString(_transactionsKey, encoded);
|
||||
}
|
||||
|
||||
Future<void> addTransaction(Transaction transaction) async {
|
||||
@@ -29,8 +30,29 @@ class StorageService {
|
||||
await saveTransactions(list);
|
||||
}
|
||||
|
||||
Future<void> updateTransaction(Transaction transaction) async {
|
||||
final list = loadTransactions();
|
||||
final index = list.indexWhere((t) => t.id == transaction.id);
|
||||
if (index != -1) {
|
||||
list[index] = transaction;
|
||||
await saveTransactions(list);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteTransaction(String id) async {
|
||||
final list = loadTransactions()..removeWhere((t) => t.id == id);
|
||||
await saveTransactions(list);
|
||||
}
|
||||
|
||||
double? loadBudget() {
|
||||
return _prefs.getDouble(_budgetKey);
|
||||
}
|
||||
|
||||
Future<void> saveBudget(double? budget) async {
|
||||
if (budget == null) {
|
||||
await _prefs.remove(_budgetKey);
|
||||
} else {
|
||||
await _prefs.setDouble(_budgetKey, budget);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user