f050d10a1d
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
48 lines
1.6 KiB
Dart
48 lines
1.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:rental_income_tracker/models/app_settings.dart';
|
|
import 'package:rental_income_tracker/models/monthly_rent_record.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class StorageService {
|
|
static const _settingsKey = 'app_settings';
|
|
static const _recordsKey = 'monthly_records';
|
|
|
|
Future<AppSettings> loadSettings() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(_settingsKey);
|
|
if (raw == null || raw.isEmpty) {
|
|
return AppSettings.defaults();
|
|
}
|
|
|
|
return AppSettings.fromJson(jsonDecode(raw) as Map<String, dynamic>);
|
|
}
|
|
|
|
Future<void> saveSettings(AppSettings settings) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(_settingsKey, jsonEncode(settings.toJson()));
|
|
}
|
|
|
|
Future<Map<String, MonthlyRentRecord>> loadRecords() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(_recordsKey);
|
|
if (raw == null || raw.isEmpty) {
|
|
return <String, MonthlyRentRecord>{};
|
|
}
|
|
|
|
final decoded = jsonDecode(raw) as Map<String, dynamic>;
|
|
return decoded.map((key, value) {
|
|
return MapEntry(
|
|
key,
|
|
MonthlyRentRecord.fromJson(value as Map<String, dynamic>),
|
|
);
|
|
});
|
|
}
|
|
|
|
Future<void> saveRecords(Map<String, MonthlyRentRecord> records) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final encoded = records.map((key, value) => MapEntry(key, value.toJson()));
|
|
await prefs.setString(_recordsKey, jsonEncode(encoded));
|
|
}
|
|
}
|