challenge_add_vm.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // features/challenge/add/add_challenge_viewmodel.dart
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:japp_flutter/core/models/challenge_model.dart';
  4. import 'package:japp_flutter/core/proviers/repository_providers.dart';
  5. import 'package:japp_flutter/core/repositories/challenge_repository.dart';
  6. final addChallengeProvider =
  7. AsyncNotifierProvider<AddChallengeViewModel, ChallengeModel>(
  8. AddChallengeViewModel.new,
  9. );
  10. class AddChallengeViewModel extends AsyncNotifier<ChallengeModel> {
  11. ChallengeRepository get _repository => ref.read(challengeRepositoryProvider);
  12. @override
  13. Future<ChallengeModel> build() async {
  14. // 初始化为空挑战模板
  15. return ChallengeModel(
  16. id: null,
  17. title: '',
  18. description: '',
  19. startDate: DateTime.now(),
  20. endDate: DateTime.now().add(const Duration(days: 30)),
  21. actionType: 1,
  22. cover: '',
  23. sort: 200,
  24. status: 1,
  25. difficulty: 1,
  26. remark: '',
  27. finishDate: null,
  28. parentId: 1,
  29. planFinishDate: null
  30. );
  31. }
  32. /// 更新挑战标题
  33. void updateTitle(String title) {
  34. state = AsyncData(state.value!.copyWith(title: title));
  35. }
  36. /// 更新挑战描述
  37. void updateDescription(String description) {
  38. state = AsyncData(state.value!.copyWith(description: description));
  39. }
  40. /// 更新目标类型
  41. void updateActionType(int actionType) {
  42. state = AsyncData(state.value!.copyWith(actionType: actionType));
  43. }
  44. /// 更新日期范围
  45. void updateDateRange(DateTime start, DateTime end) {
  46. state = AsyncData(state.value!.copyWith(startDate: start, endDate: end));
  47. }
  48. /// 更新封面
  49. void updateCover(String cover) {
  50. state = AsyncData(state.value!.copyWith(cover: cover));
  51. }
  52. /// 更新排序
  53. void updateSort(int sort) {
  54. state = AsyncData(state.value!.copyWith(sort: sort));
  55. }
  56. /// 更新挑战状态
  57. void updateStatus(int status) {
  58. state = AsyncData(state.value!.copyWith(status: status));
  59. }
  60. /// 更新难度级别
  61. void updateDifficulty(int difficulty) {
  62. state = AsyncData(state.value!.copyWith(difficulty: difficulty));
  63. }
  64. /// 更新备注
  65. void updateRemark(String remark) {
  66. state = AsyncData(state.value!.copyWith(remark: remark));
  67. }
  68. /// 更新所属父目标
  69. void updateParentId(int parentId) {
  70. state = AsyncData(state.value!.copyWith(parentId: parentId));
  71. }
  72. /// 更新计划完成时间
  73. void updatePlanFinishDate(DateTime planFinishDate) {
  74. state = AsyncData(state.value!.copyWith(planFinishDate: planFinishDate));
  75. }
  76. /// 更新完成时间
  77. void updateFinishDate(DateTime finishDate) {
  78. state = AsyncData(state.value!.copyWith(finishDate: finishDate));
  79. }
  80. /// 提交新挑战
  81. Future<void> submitChallenge() async {
  82. if (state.value == null) return;
  83. state = const AsyncLoading();
  84. state = await AsyncValue.guard(
  85. () => _repository.addChallenge(state.value!),
  86. );
  87. }
  88. }