overtime_apply_controller.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'overtime_model.dart';
  3. import 'overtime_api.dart';
  4. class OvertimeApplyState {
  5. final OvertimeModel overtime;
  6. final bool isSubmitting;
  7. const OvertimeApplyState({required this.overtime, this.isSubmitting = false});
  8. OvertimeApplyState copyWith({OvertimeModel? overtime, bool? isSubmitting}) =>
  9. OvertimeApplyState(
  10. overtime: overtime ?? this.overtime,
  11. isSubmitting: isSubmitting ?? this.isSubmitting,
  12. );
  13. }
  14. class OvertimeApplyController extends StateNotifier<OvertimeApplyState> {
  15. final OvertimeApi _api;
  16. OvertimeApplyController(this._api)
  17. : super(
  18. OvertimeApplyState(
  19. overtime: OvertimeModel(
  20. id: '',
  21. applicationNo: '',
  22. applicantId: '',
  23. applicantName: '',
  24. deptId: '',
  25. deptName: '',
  26. otDate: DateTime.now(),
  27. startTime: DateTime.now(),
  28. endTime: DateTime.now().add(const Duration(hours: 2)),
  29. otHours: 2.0,
  30. otType: '工作日加班',
  31. compensationType: '加班费',
  32. reason: '',
  33. createTime: DateTime.now(),
  34. updateTime: DateTime.now(),
  35. ),
  36. ),
  37. );
  38. void updateOtDate(DateTime d) =>
  39. state = state.copyWith(overtime: state.overtime.copyWith(otDate: d));
  40. void updateType(String t) =>
  41. state = state.copyWith(overtime: state.overtime.copyWith(otType: t));
  42. void updateCompensation(String c) => state = state.copyWith(
  43. overtime: state.overtime.copyWith(compensationType: c),
  44. );
  45. void updateStartTime(DateTime t) {
  46. state = state.copyWith(overtime: state.overtime.copyWith(startTime: t));
  47. _recalc();
  48. }
  49. void updateEndTime(DateTime t) {
  50. state = state.copyWith(overtime: state.overtime.copyWith(endTime: t));
  51. _recalc();
  52. }
  53. void updateReason(String r) =>
  54. state = state.copyWith(overtime: state.overtime.copyWith(reason: r));
  55. void _recalc() {
  56. final d =
  57. state.overtime.endTime.difference(state.overtime.startTime).inMinutes /
  58. 60.0;
  59. state = state.copyWith(overtime: state.overtime.copyWith(otHours: d));
  60. }
  61. Future<bool> submit() async {
  62. state = state.copyWith(isSubmitting: true);
  63. try {
  64. await _api.submit(state.overtime.copyWith(status: 'pending'));
  65. return true;
  66. } catch (_) {
  67. return false;
  68. } finally {
  69. state = state.copyWith(isSubmitting: false);
  70. }
  71. }
  72. Future<bool> saveDraft() async {
  73. state = state.copyWith(isSubmitting: true);
  74. try {
  75. await _api.saveDraft(state.overtime);
  76. return true;
  77. } catch (_) {
  78. return false;
  79. } finally {
  80. state = state.copyWith(isSubmitting: false);
  81. }
  82. }
  83. }
  84. final overtimeApplyProvider = StateNotifierProvider.autoDispose
  85. .family<OvertimeApplyController, OvertimeApplyState, String?>((
  86. ref,
  87. editId,
  88. ) {
  89. return OvertimeApplyController(ref.read(overtimeApiProvider));
  90. });