Initial draft

This commit is contained in:
Dennis Nemec
2025-09-20 16:14:06 +02:00
commit b19a6e1cd4
219 changed files with 10317 additions and 0 deletions

View File

@ -0,0 +1,106 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/cars/repository/cars_repository.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
import '../../../model/car.dart';
import 'cars_event.dart';
import 'cars_state.dart';
class CarsBloc extends Bloc<CarEvents, CarsState> {
CarsRepository repository;
OperationBloc opBloc;
CarsBloc({required this.repository, required this.opBloc})
: super(CarsInitial()) {
on<CarAdd>(_carAdd);
on<CarEdit>(_carEdit);
on<CarDelete>(_carDelete);
on<CarLoad>(_carLoad);
}
Future<void> _carLoad(CarLoad event, Emitter<CarsState> emit) async {
try {
emit(CarsLoading());
List<Car> cars = await repository.getAll(event.teamId);
emit(CarsLoaded(cars: cars, teamId: event.teamId));
} catch (e) {
emit(CarsLoadingFailed());
}
}
Future<void> _carAdd(CarAdd event, Emitter<CarsState> emit) async {
final currentState = state;
try {
opBloc.add(LoadOperation());
Car newCar = await repository.add(event.teamId, event.plate);
if (currentState is CarsLoaded) {
emit(
currentState.copyWith(
cars: List<Car>.from(currentState.cars)..add(newCar),
),
);
}
opBloc.add(FinishOperation(message: "Auto erfolgreich hinzugefügt"));
} catch (e) {
opBloc.add(FailOperation(message: "Fehler beim Hinzufügen eines Autos"));
}
}
Future<void> _carEdit(CarEdit event, Emitter<CarsState> emit) async {
final currentState = state;
try {
opBloc.add(LoadOperation());
await repository.edit(event.teamId, event.newCar);
if (currentState is CarsLoaded) {
emit(
currentState.copyWith(
cars:
List<Car>.from(currentState.cars).map((car) {
if (car.id == event.newCar.id) {
return event.newCar;
}
return car;
}).toList(),
),
);
}
opBloc.add(FinishOperation(message: "Auto erfolgreich editiert"));
} catch (e) {
opBloc.add(FailOperation(message: "Fehler beim Editieren des Autos"));
}
}
Future<void> _carDelete(CarDelete event, Emitter<CarsState> emit) async {
final currentState = state;
try {
opBloc.add(LoadOperation());
await repository.delete(event.carId, event.teamId);
if (currentState is CarsLoaded) {
emit(
CarsLoaded(
cars: [
...currentState.cars.where(
(car) => car.id != int.parse(event.carId),
),
],
teamId: currentState.teamId,
),
);
}
opBloc.add(FinishOperation(message: "Auto erfolgreich gelöscht"));
} catch (e) {
opBloc.add(FailOperation(message: "Fehler beim Löschen des Autos"));
}
}
}

View File

@ -0,0 +1,30 @@
import '../../../model/car.dart';
abstract class CarEvents {}
class CarLoad extends CarEvents {
String teamId;
CarLoad({required this.teamId});
}
class CarEdit extends CarEvents {
Car newCar;
String teamId;
CarEdit({required this.newCar, required this.teamId});
}
class CarAdd extends CarEvents {
String teamId;
String plate;
CarAdd({required this.teamId, required this.plate});
}
class CarDelete extends CarEvents {
String carId;
String teamId;
CarDelete({required this.carId, required this.teamId});
}

View File

@ -0,0 +1,23 @@
import 'package:hl_lieferservice/model/car.dart';
abstract class CarsState {}
class CarsInitial extends CarsState {}
class CarsLoading extends CarsState {}
class CarsLoadingFailed extends CarsState {}
class CarsLoaded extends CarsState {
List<Car> cars;
String teamId;
CarsLoaded({required this.cars, required this.teamId});
CarsLoaded copyWith({List<Car>? cars, String? teamId}) {
return CarsLoaded(
cars: cars ?? this.cars,
teamId: teamId ?? this.teamId,
);
}
}

View File

@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
import '../../../model/car.dart';
import 'car_dialog.dart';
class CarCard extends StatelessWidget {
final Car car;
final Function(Car car) onDelete;
final Function(Car car, String newName) onEdit;
const CarCard({
super.key,
required this.car,
required this.onEdit,
required this.onDelete,
});
@override
Widget build(BuildContext context) {
return Card(
child: Padding(
padding: const EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 10),
child: Icon(
Icons.local_shipping,
size: 32,
color: Theme.of(context).primaryColor,
),
),
Padding(
padding: const EdgeInsets.only(left: 10),
child: Text(car.plate),
),
],
),
Row(
children: [
IconButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return CarDialog(
onAction: (plate) {
onEdit(car, plate);
},
action: CarAction.edit,
initialPlateValue: car.plate,
);
},
);
},
icon: Icon(Icons.edit, color: Theme.of(context).primaryColor),
),
IconButton(
onPressed: () {
onDelete(car);
},
icon: const Icon(Icons.delete, color: Colors.redAccent),
),
],
),
],
),
),
);
}
}

View File

@ -0,0 +1,92 @@
import 'package:flutter/material.dart';
enum CarAction { edit, add }
class CarDialog extends StatefulWidget {
const CarDialog({
super.key,
required this.onAction,
this.action = CarAction.add,
this.initialPlateValue = "",
});
final CarAction action;
final String initialPlateValue;
final Function(String) onAction;
@override
State<StatefulWidget> createState() => _CarDialogState();
}
class _CarDialogState extends State<CarDialog> {
bool _isLoading = false;
final _formKey = GlobalKey<FormState>();
late final TextEditingController _plateController;
@override
void initState() {
super.initState();
_plateController = TextEditingController.fromValue(
TextEditingValue(text: widget.initialPlateValue),
);
}
@override
Widget build(BuildContext context) {
return AlertDialog(
content: SizedBox(
height: 120,
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 10.0),
child: Text(
"Fahrzeug ${widget.action == CarAction.add ? "hinzufügen" : "bearbeiten"}",
style: const TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
),
TextFormField(
validator: (value) {
if (value == "") {
return "Kennzeichen darf nicht leer sein";
}
return null;
},
decoration: const InputDecoration(labelText: "Kennzeichen"),
controller: _plateController,
),
],
),
),
),
actions: [
OutlinedButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text("Abbrechen"),
),
FilledButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
widget.onAction(_plateController.text);
Navigator.of(context).pop();
}
},
child: Text(
widget.action == CarAction.add ? "Hinzufügen" : "Bearbeiten",
),
),
],
);
}
}

View File

@ -0,0 +1,78 @@
import 'package:hl_lieferservice/feature/cars/presentation/car_card.dart';
import '../../../model/car.dart';
import 'car_dialog.dart';
import 'package:flutter/material.dart';
class CarManagementOverview extends StatefulWidget {
final List<Car> cars;
final Function(String plate) onAdd;
final Function(String id) onDelete;
final Function(String id, String plate) onEdit;
const CarManagementOverview({
super.key,
required this.cars,
required this.onDelete,
required this.onEdit,
required this.onAdd,
});
@override
State<StatefulWidget> createState() => _CarManagementOverviewState();
}
class _CarManagementOverviewState extends State<CarManagementOverview> {
void _addCar() async {
showDialog(
context: context,
builder: (context) {
return CarDialog(onAction: widget.onAdd);
},
);
}
void _removeCar(Car car) async {
widget.onDelete(car.id.toString());
}
void _editCar(Car car, String newName) async {
widget.onEdit(car.id.toString(), newName);
}
Widget _buildCarOverview() {
return Padding(
padding: const EdgeInsets.all(10),
child: widget.cars.isEmpty ? const Center(child: Text("keine Fahrzeuge vorhanden")) : ListView.builder(
itemBuilder:
(context, index) => CarCard(
car: widget.cars[index],
onEdit: _editCar,
onDelete: _removeCar,
),
itemCount: widget.cars.length,
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Fahrzeugverwaltung",
style: Theme.of(context).textTheme.headlineMedium,
),
),
floatingActionButton: FloatingActionButton(
onPressed: _addCar,
backgroundColor: Theme.of(context).primaryColor,
child: Icon(
Icons.add,
color: Theme.of(context).colorScheme.onSecondary,
),
),
body: _buildCarOverview(),
);
}
}

View File

@ -0,0 +1,82 @@
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/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_management.dart';
import 'package:hl_lieferservice/model/car.dart';
class CarManagementPage extends StatefulWidget {
const CarManagementPage({super.key});
@override
State<StatefulWidget> createState() => _CarManagementPageState();
}
class _CarManagementPageState extends State<CarManagementPage> {
late Authenticated _authState;
@override
void initState() {
super.initState();
// Load cars
_authState = context.read<AuthBloc>().state as Authenticated;
context.read<CarsBloc>().add(CarLoad(teamId: _authState.teamId));
}
void _add(String plate) {
context.read<CarsBloc>().add(
CarAdd(teamId: _authState.teamId, plate: plate),
);
}
void _remove(String id) {
context.read<CarsBloc>().add(
CarDelete(carId: id, teamId: _authState.teamId),
);
}
void _edit(String id, String plate) {
context.read<CarsBloc>().add(
CarEdit(
newCar: Car(id: int.parse(id), plate: plate),
teamId: _authState.teamId,
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocBuilder<CarsBloc, CarsState>(
builder: (context, state) {
debugPrint('BlocBuilder rebuilding with state: $state');
if (state is CarsLoading) {
return Center(child: const CircularProgressIndicator());
}
if (state is CarsLoaded) {
return CarManagementOverview(
cars: state.cars,
onEdit: _edit,
onAdd: _add,
onDelete: _remove,
);
}
if (state is CarsLoadingFailed) {
return Center(
child: const Text("Fahrzeuge konnten nicht geladen werden"),
);
}
return Container();
},
),
);
}
}

View File

@ -0,0 +1,25 @@
import 'package:hl_lieferservice/feature/cars/service/cars_service.dart';
import '../../../model/car.dart';
class CarsRepository {
CarService service;
CarsRepository({required this.service});
Future<List<Car>> getAll(String teamId) async {
return service.getCars(int.parse(teamId));
}
Future<void> delete(String carId, String teamId) async {
return service.removeCar(int.parse(carId), int.parse(teamId));
}
Future<void> edit(String teamId, Car newCar) async {
return service.editCar(newCar);
}
Future<Car> add(String teamId, String plate) async {
return service.addCar(plate, int.parse(teamId));
}
}

View File

@ -0,0 +1,146 @@
import 'dart:convert';
import 'package:docuframe/docuframe.dart' as df;
import 'package:flutter/cupertino.dart';
import 'package:hl_lieferservice/services/erpframe.dart';
import '../../../dto/basic_response.dart';
import '../../../dto/car_add.dart';
import '../../../dto/car_add_response.dart';
import '../../../dto/car_get_response.dart';
import '../../../model/car.dart';
class CarService extends ErpFrameService {
CarService({required super.config});
Future<Car> addCar(String plate, int teamId) async {
df.LoginSession? session;
try {
session = await getSession();
df.DocuFrameMacroResponse response =
await df.Macro(config: dfConfig, session: session).execute(
"_web_addCar",
parameter: CarAddDTO.make(teamId, plate).toJson()
as Map<String, dynamic>);
Map<String, dynamic> responseJson = jsonDecode(response.body!);
debugPrint(responseJson.toString());
CarAddResponseDTO responseDto = CarAddResponseDTO.fromJson(responseJson);
if (responseDto.succeeded == true) {
return Car(
id: int.parse(responseDto.car.id), plate: responseDto.car.plate);
} else {
throw responseDto.message;
}
} catch (e, st) {
debugPrint("ERROR WHILE ADDING CAR");
debugPrint(e.toString());
debugPrint(st.toString());
rethrow;
} finally {
await logout(session);
}
}
Future<void> editCar(Car car) async {
df.LoginSession? session;
try {
session = await getSession();
debugPrint(car.plate);
debugPrint(car.id.toString());
df.DocuFrameMacroResponse response =
await df.Macro(config: dfConfig, session: session).execute(
"_web_editCar",
parameter: {"id": car.id, "plate": car.plate});
Map<String, dynamic> responseJson = jsonDecode(response.body!);
debugPrint(responseJson.toString());
BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);
if (responseDto.succeeded == true) {
return;
} else {
throw responseDto.message;
}
} catch (e, st) {
debugPrint("ERROR WHILE EDITING CAR ${car.id}");
debugPrint("$e");
debugPrint(st.toString());
rethrow;
} finally {
await logout(session);
}
}
Future<void> removeCar(int carId, int teamId) async {
df.LoginSession? session;
try {
session = await getSession();
df.DocuFrameMacroResponse response =
await df.Macro(config: dfConfig, session: session).execute(
"_web_removeCar",
parameter: {"team_id": teamId, "id": carId});
Map<String, dynamic> responseJson = jsonDecode(response.body!);
debugPrint(responseJson.toString());
BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);
if (responseDto.succeeded == true) {
return;
} else {
throw responseDto.message;
}
} catch (e, st) {
debugPrint("ERROR WHILE REMOVING CAR");
debugPrint(e.toString());
debugPrint(st.toString());
rethrow;
} finally {
await logout(session);
}
}
Future<List<Car>> getCars(int teamId) async {
df.LoginSession? session;
try {
session = await getSession();
df.DocuFrameMacroResponse response =
await df.Macro(config: dfConfig, session: session)
.execute("_web_getCars", parameter: {"team_id": teamId});
debugPrint(teamId.toString());
Map<String, dynamic> responseJson = jsonDecode(response.body!);
debugPrint("RESPONSE");
debugPrint(responseJson.toString());
CarGetResponseDTO responseDto = CarGetResponseDTO.fromJson(responseJson);
if (responseDto.succeeded == true) {
return responseDto.cars!
.map((carDto) => Car(id: int.parse(carDto.id), plate: carDto.plate))
.toList();
} else {
throw responseDto.message;
}
} catch (e, st) {
debugPrint("ERROR WHILE FETCHING CARS");
debugPrint(e.toString());
debugPrint(st.toString());
rethrow;
} finally {
await logout(session);
}
}
}