Added components to article
This commit is contained in:
@ -2,17 +2,19 @@ import 'package:hl_lieferservice/feature/cars/model/selection.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class CarSelectionRepository {
|
||||
static const _keyDate = 'car_selection_date';
|
||||
static const _keyCarId = 'car_selection_car_id';
|
||||
static const _keyCarPlate = 'car_selection_car_plate';
|
||||
static String _keyDate(String userId) => 'car_selection_${userId}_date';
|
||||
static String _keyCarId(String userId) => 'car_selection_${userId}_car_id';
|
||||
static String _keyCarPlate(String userId) =>
|
||||
'car_selection_${userId}_car_plate';
|
||||
|
||||
/// Returns the stored [CarSelection], or null if nothing has been saved yet.
|
||||
Future<CarSelection?> getSelection() async {
|
||||
/// Returns the stored [CarSelection] for the given user, or null if nothing
|
||||
/// has been saved yet for that user.
|
||||
Future<CarSelection?> getSelection(String userId) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
final dateString = prefs.getString(_keyDate);
|
||||
final carId = prefs.getInt(_keyCarId);
|
||||
final plate = prefs.getString(_keyCarPlate);
|
||||
final dateString = prefs.getString(_keyDate(userId));
|
||||
final carId = prefs.getInt(_keyCarId(userId));
|
||||
final plate = prefs.getString(_keyCarPlate(userId));
|
||||
|
||||
if (dateString == null || carId == null || plate == null) return null;
|
||||
|
||||
@ -23,12 +25,12 @@ class CarSelectionRepository {
|
||||
);
|
||||
}
|
||||
|
||||
/// Persists the given [selection] locally on this device.
|
||||
Future<void> saveSelection(CarSelection selection) async {
|
||||
/// Persists the given [selection] for the given user locally on this device.
|
||||
Future<void> saveSelection(String userId, CarSelection selection) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
await prefs.setString(_keyDate, selection.date.toIso8601String());
|
||||
await prefs.setInt(_keyCarId, selection.selectedCarId!);
|
||||
await prefs.setString(_keyCarPlate, selection.selectedCarPlate!);
|
||||
await prefs.setString(_keyDate(userId), selection.date.toIso8601String());
|
||||
await prefs.setInt(_keyCarId(userId), selection.selectedCarId!);
|
||||
await prefs.setString(_keyCarPlate(userId), selection.selectedCarPlate!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user