BIG FAT
This commit is contained in:
@ -0,0 +1,88 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/car_selection/repository/car_selection_repository.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/model/selection.dart';
|
||||
import 'package:hl_lieferservice/model/car.dart';
|
||||
|
||||
import 'events.dart';
|
||||
import 'state.dart';
|
||||
|
||||
class CarSelectBloc extends Bloc<CarSelectEvent, CarSelectState> {
|
||||
final CarSelectionRepository repository;
|
||||
|
||||
CarSelectBloc({required this.repository}) : super(CarSelectInitial()) {
|
||||
on<CarSelectLoad>(_load);
|
||||
on<CarSelectConfirm>(_confirm);
|
||||
on<CarSelectChange>(_change);
|
||||
on<CarSelectCancel>(_cancel);
|
||||
}
|
||||
|
||||
Future<void> _load(
|
||||
CarSelectLoad event,
|
||||
Emitter<CarSelectState> emit,
|
||||
) async {
|
||||
try {
|
||||
emit(CarSelectLoading());
|
||||
|
||||
final CarSelection? stored = await repository.getSelection();
|
||||
final today = DateTime.now();
|
||||
|
||||
final bool validForToday =
|
||||
stored != null &&
|
||||
stored.selectedCarId != null &&
|
||||
stored.selectedCarPlate != null &&
|
||||
stored.date.year == today.year &&
|
||||
stored.date.month == today.month &&
|
||||
stored.date.day == today.day;
|
||||
|
||||
if (validForToday) {
|
||||
emit(
|
||||
CarSelectComplete(
|
||||
selectedCar: Car(
|
||||
id: stored.selectedCarId!,
|
||||
plate: stored.selectedCarPlate!,
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(CarSelectRequired());
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint('CarSelectBloc._load failed: $e');
|
||||
debugPrint('Stacktrace: $st');
|
||||
emit(CarSelectFailed());
|
||||
}
|
||||
}
|
||||
|
||||
void _change(CarSelectChange event, Emitter<CarSelectState> emit) {
|
||||
final previousCar =
|
||||
state is CarSelectComplete ? (state as CarSelectComplete).selectedCar : null;
|
||||
emit(CarSelectRequired(previousCar: previousCar));
|
||||
}
|
||||
|
||||
void _cancel(CarSelectCancel event, Emitter<CarSelectState> emit) {
|
||||
// Restore without touching SharedPreferences — no tour reload needed.
|
||||
emit(CarSelectComplete(selectedCar: event.car));
|
||||
}
|
||||
|
||||
Future<void> _confirm(
|
||||
CarSelectConfirm event,
|
||||
Emitter<CarSelectState> emit,
|
||||
) async {
|
||||
try {
|
||||
final today = DateTime.now();
|
||||
await repository.saveSelection(
|
||||
CarSelection(
|
||||
date: today,
|
||||
selectedCarId: event.car.id,
|
||||
selectedCarPlate: event.car.plate,
|
||||
),
|
||||
);
|
||||
emit(CarSelectComplete(selectedCar: event.car));
|
||||
} catch (e, st) {
|
||||
debugPrint('CarSelectBloc._confirm failed: $e');
|
||||
debugPrint('Stacktrace: $st');
|
||||
emit(CarSelectFailed());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
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 when the driver confirms their car choice for the day.
|
||||
class CarSelectConfirm extends CarSelectEvent {
|
||||
final Car car;
|
||||
|
||||
CarSelectConfirm({required this.car});
|
||||
}
|
||||
|
||||
/// Fired when the driver wants to switch to a different car.
|
||||
/// Resets the selection so the enforcer shows the picker again.
|
||||
class CarSelectChange extends CarSelectEvent {}
|
||||
|
||||
/// Fired when the driver cancels the change and wants to keep the previous car.
|
||||
/// Restores [CarSelectComplete] without writing to SharedPreferences.
|
||||
class CarSelectCancel extends CarSelectEvent {
|
||||
final Car car;
|
||||
|
||||
CarSelectCancel({required this.car});
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
import 'package:hl_lieferservice/model/car.dart';
|
||||
|
||||
abstract class CarSelectState {}
|
||||
|
||||
class CarSelectInitial extends CarSelectState {}
|
||||
|
||||
class CarSelectLoading extends CarSelectState {}
|
||||
|
||||
/// No valid car selection exists for today — the driver must choose.
|
||||
/// [previousCar] is set when the driver triggered a manual change,
|
||||
/// allowing the page to pre-highlight the current car and offer a cancel.
|
||||
class CarSelectRequired extends CarSelectState {
|
||||
final Car? previousCar;
|
||||
|
||||
CarSelectRequired({this.previousCar});
|
||||
}
|
||||
|
||||
/// A car has been selected for today. The selection is persisted locally.
|
||||
class CarSelectComplete extends CarSelectState {
|
||||
final Car selectedCar;
|
||||
|
||||
CarSelectComplete({required this.selectedCar});
|
||||
}
|
||||
|
||||
class CarSelectFailed extends CarSelectState {}
|
||||
@ -0,0 +1,59 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hl_lieferservice/model/car.dart';
|
||||
|
||||
class CarSelectionCard extends StatelessWidget {
|
||||
final Car car;
|
||||
final bool isSelected;
|
||||
final VoidCallback onTap;
|
||||
|
||||
const CarSelectionCard({
|
||||
super.key,
|
||||
required this.car,
|
||||
required this.isSelected,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = Theme.of(context).primaryColor;
|
||||
|
||||
return Card(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
side: isSelected
|
||||
? BorderSide(color: color, width: 2)
|
||||
: BorderSide.none,
|
||||
),
|
||||
color: isSelected
|
||||
? color.withValues(alpha: 0.08)
|
||||
: null,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
onTap: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.local_shipping,
|
||||
size: 32,
|
||||
color: isSelected ? color : Colors.grey,
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Text(
|
||||
car.plate,
|
||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (isSelected)
|
||||
Icon(Icons.check_circle, color: color),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.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';
|
||||
import 'package:hl_lieferservice/feature/car_selection/presentation/car_selection_page.dart';
|
||||
|
||||
class CarSelectionEnforcer extends StatefulWidget {
|
||||
final Widget child;
|
||||
|
||||
const CarSelectionEnforcer({super.key, required this.child});
|
||||
|
||||
@override
|
||||
State<CarSelectionEnforcer> createState() => _CarSelectionEnforcerState();
|
||||
}
|
||||
|
||||
class _CarSelectionEnforcerState extends State<CarSelectionEnforcer> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
context.read<CarSelectBloc>().add(CarSelectLoad());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<CarSelectBloc, CarSelectState>(
|
||||
builder: (context, state) {
|
||||
// Show a full-screen spinner only while the persisted selection is
|
||||
// being read from SharedPreferences (at most one frame on cold start).
|
||||
if (state is CarSelectInitial || state is CarSelectLoading) {
|
||||
return const Scaffold(
|
||||
body: Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
if (state is CarSelectFailed) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 72,
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text(
|
||||
"Fehler beim Laden der Fahrzeugauswahl.",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FilledButton(
|
||||
onPressed: () =>
|
||||
context.read<CarSelectBloc>().add(CarSelectLoad()),
|
||||
child: const Text("Erneut versuchen"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// For both CarSelectRequired and CarSelectComplete, keep Home alive
|
||||
// in the widget tree so initState is never re-triggered. The selection
|
||||
// page is overlaid on top when a (re-)selection is required.
|
||||
return Stack(
|
||||
children: [
|
||||
widget.child,
|
||||
if (state is CarSelectRequired)
|
||||
Positioned.fill(
|
||||
child: CarSelectionPage(previousCar: state.previousCar),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
232
lib/feature/car_selection/presentation/car_selection_page.dart
Normal file
232
lib/feature/car_selection/presentation/car_selection_page.dart
Normal file
@ -0,0 +1,232 @@
|
||||
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';
|
||||
import 'package:hl_lieferservice/feature/car_selection/presentation/car_selection_card.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/bloc/cars_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/bloc/cars_event.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/bloc/cars_state.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/presentation/car_dialog.dart';
|
||||
import 'package:hl_lieferservice/model/car.dart';
|
||||
|
||||
class CarSelectionPage extends StatefulWidget {
|
||||
/// When set, the page is in "change" mode: the car is pre-highlighted
|
||||
/// and a cancel button is shown to revert without choosing a new car.
|
||||
final Car? previousCar;
|
||||
|
||||
const CarSelectionPage({super.key, this.previousCar});
|
||||
|
||||
@override
|
||||
State<CarSelectionPage> createState() => _CarSelectionPageState();
|
||||
}
|
||||
|
||||
class _CarSelectionPageState extends State<CarSelectionPage> {
|
||||
Car? _selectedCar;
|
||||
|
||||
bool get _isChanging => widget.previousCar != null;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_selectedCar = widget.previousCar;
|
||||
final authState = context.read<AuthBloc>().state as Authenticated;
|
||||
context.read<CarsBloc>().add(CarLoad(teamId: authState.user.number));
|
||||
}
|
||||
|
||||
void _onAddCar() {
|
||||
final authState = context.read<AuthBloc>().state as Authenticated;
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (_) => CarDialog(
|
||||
onAction: (plate) {
|
||||
context.read<CarsBloc>().add(
|
||||
CarAdd(teamId: authState.user.number, plate: plate),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onConfirm() {
|
||||
if (_selectedCar == null) return;
|
||||
context.read<CarSelectBloc>().add(CarSelectConfirm(car: _selectedCar!));
|
||||
}
|
||||
|
||||
Widget _buildCarList(List<Car> cars) {
|
||||
if (cars.isEmpty) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.local_shipping_outlined, size: 72, color: Colors.grey),
|
||||
const SizedBox(height: 24),
|
||||
Text(
|
||||
"Noch kein Fahrzeug vorhanden.",
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
"Füge zuerst ein Fahrzeug hinzu, bevor du fortfahren kannst.",
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Colors.grey,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
FilledButton.icon(
|
||||
onPressed: _onAddCar,
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text("Fahrzeug hinzufügen"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final authState = context.read<AuthBloc>().state as Authenticated;
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
context.read<CarsBloc>().add(
|
||||
CarLoad(teamId: authState.user.number, force: true),
|
||||
);
|
||||
},
|
||||
child: ListView.builder(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
itemCount: cars.length,
|
||||
itemBuilder: (context, index) {
|
||||
final car = cars[index];
|
||||
return CarSelectionCard(
|
||||
car: car,
|
||||
isSelected: _selectedCar?.id == car.id,
|
||||
onTap: () => setState(() => _selectedCar = car),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocListener<CarSelectBloc, CarSelectState>(
|
||||
listener: (context, state) {
|
||||
if (state is CarSelectFailed) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text("Fehler beim Speichern der Fahrzeugauswahl."),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: _isChanging
|
||||
? AppBar(
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: () => context.read<CarSelectBloc>().add(
|
||||
CarSelectCancel(car: widget.previousCar!),
|
||||
),
|
||||
),
|
||||
title: const Text("Fahrzeug wechseln"),
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
foregroundColor: Theme.of(context).colorScheme.onSecondary,
|
||||
)
|
||||
: null,
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (!_isChanging) ...[
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(20, 24, 20, 4),
|
||||
child: Text(
|
||||
"Fahrzeug auswählen",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(20, 4, 20, 16),
|
||||
child: Text(
|
||||
"Wähle das Fahrzeug aus, das du heute verwendest.",
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
Expanded(
|
||||
child: BlocBuilder<CarsBloc, CarsState>(
|
||||
builder: (context, state) {
|
||||
if (state is CarsLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (state is CarsLoaded) {
|
||||
return _buildCarList(state.cars);
|
||||
}
|
||||
|
||||
if (state is CarsLoadingFailed) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.error_outline,
|
||||
size: 72,
|
||||
color: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const Text(
|
||||
"Fehler beim Laden der Fahrzeuge.",
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
final authState =
|
||||
context.read<AuthBloc>().state
|
||||
as Authenticated;
|
||||
context.read<CarsBloc>().add(
|
||||
CarLoad(
|
||||
teamId: authState.user.number,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const Text("Erneut versuchen"),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
|
||||
child: SizedBox(
|
||||
width: double.infinity,
|
||||
child: FilledButton(
|
||||
onPressed: _selectedCar != null ? _onConfirm : null,
|
||||
child: const Text("Auswählen"),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
56
lib/feature/car_selection/presentation/selected_car_bar.dart
Normal file
56
lib/feature/car_selection/presentation/selected_car_bar.dart
Normal file
@ -0,0 +1,56 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.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';
|
||||
|
||||
class SelectedCarBar extends StatelessWidget {
|
||||
const SelectedCarBar({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<CarSelectBloc, CarSelectState>(
|
||||
builder: (context, state) {
|
||||
if (state is! CarSelectComplete) return const SizedBox.shrink();
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Divider(height: 1, thickness: 1),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.local_shipping,
|
||||
size: 20,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
state.selectedCar.plate,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
),
|
||||
),
|
||||
OutlinedButton.icon(
|
||||
onPressed: () =>
|
||||
context.read<CarSelectBloc>().add(CarSelectChange()),
|
||||
icon: const Icon(Icons.swap_horiz, size: 18),
|
||||
label: const Text("Wechseln"),
|
||||
style: OutlinedButton.styleFrom(
|
||||
visualDensity: VisualDensity.compact,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
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';
|
||||
|
||||
/// Returns the stored [CarSelection], or null if nothing has been saved yet.
|
||||
Future<CarSelection?> getSelection() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
final dateString = prefs.getString(_keyDate);
|
||||
final carId = prefs.getInt(_keyCarId);
|
||||
final plate = prefs.getString(_keyCarPlate);
|
||||
|
||||
if (dateString == null || carId == null || plate == null) return null;
|
||||
|
||||
return CarSelection(
|
||||
date: DateTime.parse(dateString),
|
||||
selectedCarId: carId,
|
||||
selectedCarPlate: plate,
|
||||
);
|
||||
}
|
||||
|
||||
/// Persists the given [selection] locally on this device.
|
||||
Future<void> saveSelection(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!);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user