| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'vehicle_model.dart';
- import 'vehicle_api.dart';
- class VehicleApplyState {
- final VehicleModel vehicle;
- final bool isSubmitting;
- const VehicleApplyState({required this.vehicle, this.isSubmitting = false});
- VehicleApplyState copyWith({VehicleModel? vehicle, bool? isSubmitting}) =>
- VehicleApplyState(
- vehicle: vehicle ?? this.vehicle,
- isSubmitting: isSubmitting ?? this.isSubmitting,
- );
- }
- class VehicleApplyController extends StateNotifier<VehicleApplyState> {
- final VehicleApi _api;
- VehicleApplyController(this._api)
- : super(
- VehicleApplyState(
- vehicle: VehicleModel(
- id: '',
- applicationNo: '',
- applicantId: '',
- applicantName: '',
- deptId: '',
- deptName: '',
- purpose: '',
- startTime: DateTime.now(),
- endTime: DateTime.now().add(const Duration(hours: 2)),
- origin: '',
- destination: '',
- reason: '',
- createTime: DateTime.now(),
- updateTime: DateTime.now(),
- ),
- ),
- );
- void updatePurpose(String v) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(purpose: v));
- void updateVehicleType(String v) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(vehicleType: v));
- void updateStartTime(DateTime t) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(startTime: t));
- void updateEndTime(DateTime t) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(endTime: t));
- void updateOrigin(String v) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(origin: v));
- void updateDestination(String v) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(destination: v));
- void updatePassengerCount(int v) => state = state.copyWith(
- vehicle: state.vehicle.copyWith(passengerCount: v),
- );
- void updateDriver(String v) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(driver: v));
- void updateEstimatedMileage(double v) => state = state.copyWith(
- vehicle: state.vehicle.copyWith(estimatedMileage: v),
- );
- void updateReason(String v) =>
- state = state.copyWith(vehicle: state.vehicle.copyWith(reason: v));
- Future<bool> submit() async {
- state = state.copyWith(isSubmitting: true);
- try {
- await _api.submit(state.vehicle.copyWith(status: 'pending'));
- return true;
- } catch (_) {
- return false;
- } finally {
- state = state.copyWith(isSubmitting: false);
- }
- }
- Future<bool> saveDraft() async {
- state = state.copyWith(isSubmitting: true);
- try {
- await _api.saveDraft(state.vehicle);
- return true;
- } catch (_) {
- return false;
- } finally {
- state = state.copyWith(isSubmitting: false);
- }
- }
- }
- final vehicleApplyProvider = StateNotifierProvider.autoDispose
- .family<VehicleApplyController, VehicleApplyState, String?>((ref, editId) {
- return VehicleApplyController(ref.read(vehicleApiProvider));
- });
|