vehicle_apply_controller.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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(
  10. vehicle: vehicle ?? this.vehicle,
  11. isSubmitting: isSubmitting ?? this.isSubmitting,
  12. );
  13. }
  14. class VehicleApplyController extends StateNotifier<VehicleApplyState> {
  15. final VehicleApi _api;
  16. VehicleApplyController(this._api)
  17. : super(
  18. VehicleApplyState(
  19. vehicle: VehicleModel(
  20. id: '',
  21. applicationNo: '',
  22. applicantId: '',
  23. applicantName: '',
  24. deptId: '',
  25. deptName: '',
  26. purpose: '',
  27. startTime: DateTime.now(),
  28. endTime: DateTime.now().add(const Duration(hours: 2)),
  29. origin: '',
  30. destination: '',
  31. reason: '',
  32. createTime: DateTime.now(),
  33. updateTime: DateTime.now(),
  34. ),
  35. ),
  36. );
  37. void updatePurpose(String v) =>
  38. state = state.copyWith(vehicle: state.vehicle.copyWith(purpose: v));
  39. void updateVehicleType(String v) =>
  40. state = state.copyWith(vehicle: state.vehicle.copyWith(vehicleType: v));
  41. void updateStartTime(DateTime t) =>
  42. state = state.copyWith(vehicle: state.vehicle.copyWith(startTime: t));
  43. void updateEndTime(DateTime t) =>
  44. state = state.copyWith(vehicle: state.vehicle.copyWith(endTime: t));
  45. void updateOrigin(String v) =>
  46. state = state.copyWith(vehicle: state.vehicle.copyWith(origin: v));
  47. void updateDestination(String v) =>
  48. state = state.copyWith(vehicle: state.vehicle.copyWith(destination: v));
  49. void updatePassengerCount(int v) => state = state.copyWith(
  50. vehicle: state.vehicle.copyWith(passengerCount: v),
  51. );
  52. void updateDriver(String v) =>
  53. state = state.copyWith(vehicle: state.vehicle.copyWith(driver: v));
  54. void updateEstimatedMileage(double v) => state = state.copyWith(
  55. vehicle: state.vehicle.copyWith(estimatedMileage: v),
  56. );
  57. void updateReason(String v) =>
  58. state = state.copyWith(vehicle: state.vehicle.copyWith(reason: v));
  59. Future<bool> submit() async {
  60. state = state.copyWith(isSubmitting: true);
  61. try {
  62. await _api.submit(state.vehicle.copyWith(status: 'pending'));
  63. return true;
  64. } catch (_) {
  65. return false;
  66. } finally {
  67. state = state.copyWith(isSubmitting: false);
  68. }
  69. }
  70. Future<bool> saveDraft() async {
  71. state = state.copyWith(isSubmitting: true);
  72. try {
  73. await _api.saveDraft(state.vehicle);
  74. return true;
  75. } catch (_) {
  76. return false;
  77. } finally {
  78. state = state.copyWith(isSubmitting: false);
  79. }
  80. }
  81. }
  82. final vehicleApplyProvider = StateNotifierProvider.autoDispose
  83. .family<VehicleApplyController, VehicleApplyState, String?>((ref, editId) {
  84. return VehicleApplyController(ref.read(vehicleApiProvider));
  85. });