expense_apply_controller.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import 'expense_model.dart';
  3. import 'expense_api.dart';
  4. class ExpenseApplyState {
  5. final ExpenseModel expense;
  6. final bool isSubmitting;
  7. const ExpenseApplyState({required this.expense, this.isSubmitting = false});
  8. ExpenseApplyState copyWith({ExpenseModel? expense, bool? isSubmitting}) {
  9. return ExpenseApplyState(
  10. expense: expense ?? this.expense,
  11. isSubmitting: isSubmitting ?? this.isSubmitting,
  12. );
  13. }
  14. }
  15. class ExpenseApplyController extends StateNotifier<ExpenseApplyState> {
  16. final ExpenseApi _api;
  17. ExpenseApplyController(this._api, {ExpenseModel? initial})
  18. : super(ExpenseApplyState(
  19. expense: initial ??
  20. ExpenseModel(
  21. id: '',
  22. reportNo: '',
  23. applicantId: '',
  24. applicantName: '',
  25. deptId: '',
  26. deptName: '',
  27. expenseType: '差旅费',
  28. totalAmount: 0.0,
  29. remark: '',
  30. createTime: DateTime.now(),
  31. updateTime: DateTime.now(),
  32. ),
  33. ));
  34. void updateType(String type) {
  35. state = state.copyWith(expense: state.expense.copyWith(expenseType: type));
  36. }
  37. void updateRemark(String remark) {
  38. state = state.copyWith(expense: state.expense.copyWith(remark: remark));
  39. }
  40. void addDetail(ExpenseDetailModel detail) {
  41. final details = [...state.expense.details, detail];
  42. state = state.copyWith(expense: state.expense.copyWith(details: details));
  43. }
  44. void removeDetail(int index) {
  45. final details = [...state.expense.details]..removeAt(index);
  46. state = state.copyWith(expense: state.expense.copyWith(details: details));
  47. }
  48. void recalculateAmount() {
  49. final total =
  50. state.expense.details.fold<double>(0, (sum, d) => sum + d.totalAmount);
  51. state =
  52. state.copyWith(expense: state.expense.copyWith(totalAmount: total));
  53. }
  54. Future<bool> submit() async {
  55. state = state.copyWith(isSubmitting: true);
  56. try {
  57. await _api.submit(state.expense.copyWith(status: 'pending'));
  58. return true;
  59. } catch (_) {
  60. return false;
  61. } finally {
  62. state = state.copyWith(isSubmitting: false);
  63. }
  64. }
  65. Future<bool> saveDraft() async {
  66. state = state.copyWith(isSubmitting: true);
  67. try {
  68. await _api.saveDraft(state.expense);
  69. return true;
  70. } catch (_) {
  71. return false;
  72. } finally {
  73. state = state.copyWith(isSubmitting: false);
  74. }
  75. }
  76. }
  77. final expenseApplyProvider =
  78. StateNotifierProvider.autoDispose.family<ExpenseApplyController,
  79. ExpenseApplyState, String?>((ref, editId) {
  80. final api = ref.watch(expenseApiProvider);
  81. return ExpenseApplyController(api);
  82. });