78 lines
3.2 KiB
Dart
78 lines
3.2 KiB
Dart
import 'dart:convert';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../models/breed_setting.dart';
|
|
import '../models/reservation.dart';
|
|
import '../models/customer.dart';
|
|
|
|
class StorageService {
|
|
static const String breedsKey = 'breed_settings_v2';
|
|
static const String reservationsKey = 'reservations_v2';
|
|
static const String customersKey = 'customers_v1';
|
|
static const String warehouseStockKey = 'warehouse_stock_v1';
|
|
static const String lastProductionUpdateKey = 'last_production_update_v1';
|
|
|
|
static Future<Map<String, double>> loadWarehouseStock() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(warehouseStockKey);
|
|
if (raw == null || raw.isEmpty) return {};
|
|
final map = jsonDecode(raw) as Map<String, dynamic>;
|
|
return map.map((k, v) => MapEntry(k, (v as num).toDouble()));
|
|
}
|
|
|
|
static Future<void> saveWarehouseStock(Map<String, double> stock) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(warehouseStockKey, jsonEncode(stock));
|
|
}
|
|
|
|
static Future<DateTime?> loadLastProductionUpdate() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(lastProductionUpdateKey);
|
|
if (raw == null || raw.isEmpty) return null;
|
|
return DateTime.parse(raw);
|
|
}
|
|
|
|
static Future<void> saveLastProductionUpdate(DateTime date) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(lastProductionUpdateKey, date.toIso8601String());
|
|
}
|
|
|
|
static Future<List<BreedSetting>> loadBreedSettings() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(breedsKey);
|
|
if (raw == null || raw.isEmpty) return [];
|
|
final list = jsonDecode(raw) as List;
|
|
return list.map((e) => BreedSetting.fromMap(Map<String, dynamic>.from(e))).toList();
|
|
}
|
|
|
|
static Future<void> saveBreedSettings(List<BreedSetting> items) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(breedsKey, jsonEncode(items.map((e) => e.toMap()).toList()));
|
|
}
|
|
|
|
static Future<List<Reservation>> loadReservations() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(reservationsKey);
|
|
if (raw == null || raw.isEmpty) return [];
|
|
final list = jsonDecode(raw) as List;
|
|
return list.map((e) => Reservation.fromMap(Map<String, dynamic>.from(e))).toList();
|
|
}
|
|
|
|
static Future<void> saveReservations(List<Reservation> items) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(reservationsKey, jsonEncode(items.map((e) => e.toMap()).toList()));
|
|
}
|
|
|
|
static Future<List<Customer>> loadCustomers() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final raw = prefs.getString(customersKey);
|
|
if (raw == null || raw.isEmpty) return [];
|
|
final list = jsonDecode(raw) as List;
|
|
return list.map((e) => Customer.fromMap(Map<String, dynamic>.from(e))).toList();
|
|
}
|
|
|
|
static Future<void> saveCustomers(List<Customer> items) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(customersKey, jsonEncode(items.map((e) => e.toMap()).toList()));
|
|
}
|
|
}
|