Added components to article
This commit is contained in:
@ -24,7 +24,7 @@ class CarSelectBloc extends Bloc<CarSelectEvent, CarSelectState> {
|
||||
try {
|
||||
emit(CarSelectLoading());
|
||||
|
||||
final CarSelection? stored = await repository.getSelection();
|
||||
final CarSelection? stored = await repository.getSelection(event.userId);
|
||||
final today = DateTime.now();
|
||||
|
||||
final bool validForToday =
|
||||
@ -72,6 +72,7 @@ class CarSelectBloc extends Bloc<CarSelectEvent, CarSelectState> {
|
||||
try {
|
||||
final today = DateTime.now();
|
||||
await repository.saveSelection(
|
||||
event.userId,
|
||||
CarSelection(
|
||||
date: today,
|
||||
selectedCarId: event.car.id,
|
||||
|
||||
@ -2,14 +2,20 @@ import 'package:hl_lieferservice/model/car.dart';
|
||||
|
||||
abstract class CarSelectEvent {}
|
||||
|
||||
/// Fired at app startup to check if a car has already been selected for today.
|
||||
class CarSelectLoad extends CarSelectEvent {}
|
||||
/// Fired at app startup to check if a car has already been selected for today
|
||||
/// for the given user.
|
||||
class CarSelectLoad extends CarSelectEvent {
|
||||
final String userId;
|
||||
|
||||
CarSelectLoad({required this.userId});
|
||||
}
|
||||
|
||||
/// Fired when the driver confirms their car choice for the day.
|
||||
class CarSelectConfirm extends CarSelectEvent {
|
||||
final String userId;
|
||||
final Car car;
|
||||
|
||||
CarSelectConfirm({required this.car});
|
||||
CarSelectConfirm({required this.userId, required this.car});
|
||||
}
|
||||
|
||||
/// Fired when the driver wants to switch to a different car.
|
||||
@ -22,4 +28,4 @@ class CarSelectCancel extends CarSelectEvent {
|
||||
final Car car;
|
||||
|
||||
CarSelectCancel({required this.car});
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_state.dart';
|
||||
import 'package:hl_lieferservice/feature/car_selection/bloc/bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/car_selection/bloc/events.dart';
|
||||
import 'package:hl_lieferservice/feature/car_selection/bloc/state.dart';
|
||||
@ -18,7 +20,12 @@ class _CarSelectionEnforcerState extends State<CarSelectionEnforcer> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
context.read<CarSelectBloc>().add(CarSelectLoad());
|
||||
final authState = context.read<AuthBloc>().state;
|
||||
if (authState is Authenticated) {
|
||||
context
|
||||
.read<CarSelectBloc>()
|
||||
.add(CarSelectLoad(userId: authState.user.number));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@ -53,8 +60,14 @@ class _CarSelectionEnforcerState extends State<CarSelectionEnforcer> {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FilledButton(
|
||||
onPressed: () =>
|
||||
context.read<CarSelectBloc>().add(CarSelectLoad()),
|
||||
onPressed: () {
|
||||
final authState = context.read<AuthBloc>().state;
|
||||
if (authState is Authenticated) {
|
||||
context.read<CarSelectBloc>().add(
|
||||
CarSelectLoad(userId: authState.user.number),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: const Text("Erneut versuchen"),
|
||||
),
|
||||
],
|
||||
|
||||
@ -52,7 +52,13 @@ class _CarSelectionPageState extends State<CarSelectionPage> {
|
||||
|
||||
void _onConfirm() {
|
||||
if (_selectedCar == null) return;
|
||||
context.read<CarSelectBloc>().add(CarSelectConfirm(car: _selectedCar!));
|
||||
final authState = context.read<AuthBloc>().state as Authenticated;
|
||||
context.read<CarSelectBloc>().add(
|
||||
CarSelectConfirm(
|
||||
userId: authState.user.number,
|
||||
car: _selectedCar!,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCarList(List<Car> cars) {
|
||||
|
||||
@ -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