vehicle_apply_controller.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'vehicle_model.dart';
  3. import 'vehicle_api.dart';
  4. class VehicleApplyState {
  5. final VehicleModel vehicle;
  6. final bool isSubmitting;
  7. const VehicleApplyState({required this.vehicle, this.isSubmitting = false});
  8. VehicleApplyState copyWith({VehicleModel? vehicle, bool? isSubmitting}) =>
  9. VehicleApplyState(vehicle: vehicle ?? this.vehicle, isSubmitting: isSubmitting ?? this.isSubmitting);
  10. }
  11. class VehicleApplyController extends StateNotifier<VehicleApplyState> {
  12. final VehicleApi _api;
  13. VehicleApplyController(this._api)
  14. : super(VehicleApplyState(vehicle: VehicleModel(
  15. id: '', applicationNo: '', applicantId: '', applicantName: '',
  16. deptId: '', deptName: '', purpose: '',
  17. startTime: DateTime.now(), endTime: DateTime.now().add(const Duration(hours: 2)),
  18. origin: '', destination: '', reason: '',
  19. createTime: DateTime.now(), updateTime: DateTime.now(),
  20. )));
  21. void updatePurpose(String v) => state = state.copyWith(vehicle: state.vehicle.copyWith(purpose: v));
  22. void updateVehicleType(String v) => state = state.copyWith(vehicle: state.vehicle.copyWith(vehicleType: v));
  23. void updateStartTime(DateTime t) => state = state.copyWith(vehicle: state.vehicle.copyWith(startTime: t));
  24. void updateEndTime(DateTime t) => state = state.copyWith(vehicle: state.vehicle.copyWith(endTime: t));
  25. void updateOrigin(String v) => state = state.copyWith(vehicle: state.vehicle.copyWith(origin: v));
  26. void updateDestination(String v) => state = state.copyWith(vehicle: state.vehicle.copyWith(destination: v));
  27. void updatePassengerCount(int v) => state = state.copyWith(vehicle: state.vehicle.copyWith(passengerCount: v));
  28. void updateDriver(String v) => state = state.copyWith(vehicle: state.vehicle.copyWith(driver: v));
  29. void updateEstimatedMileage(double v) => state = state.copyWith(vehicle: state.vehicle.copyWith(estimatedMileage: v));
  30. void updateReason(String v) => state = state.copyWith(vehicle: state.vehicle.copyWith(reason: v));
  31. Future<bool> submit() async {
  32. state = state.copyWith(isSubmitting: true);
  33. try { await _api.submit(state.vehicle.copyWith(status: 'pending')); return true; }
  34. catch (_) { return false; }
  35. finally { state = state.copyWith(isSubmitting: false); }
  36. }
  37. Future<bool> saveDraft() async {
  38. state = state.copyWith(isSubmitting: true);
  39. try { await _api.saveDraft(state.vehicle); return true; }
  40. catch (_) { return false; }
  41. finally { state = state.copyWith(isSubmitting: false); }
  42. }
  43. }
  44. final vehicleApplyProvider = StateNotifierProvider.autoDispose.family<VehicleApplyController, VehicleApplyState, String?>((ref, editId) {
  45. return VehicleApplyController(ref.read(vehicleApiProvider));
  46. });