| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // features/challenge/add/add_challenge_viewmodel.dart
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'package:japp_flutter/core/models/challenge_model.dart';
- import 'package:japp_flutter/core/proviers/repository_providers.dart';
- import 'package:japp_flutter/core/repositories/challenge_repository.dart';
- final addChallengeProvider =
- AsyncNotifierProvider<AddChallengeViewModel, ChallengeModel>(
- AddChallengeViewModel.new,
- );
- class AddChallengeViewModel extends AsyncNotifier<ChallengeModel> {
- ChallengeRepository get _repository => ref.read(challengeRepositoryProvider);
- @override
- Future<ChallengeModel> build() async {
- // 初始化为空挑战模板
- return ChallengeModel(
- id: null,
- title: '',
- description: '',
- startDate: DateTime.now(),
- endDate: DateTime.now().add(const Duration(days: 30)),
- actionType: 1,
- cover: '',
- sort: 200,
- status: 1,
- difficulty: 1,
- remark: '',
- finishDate: null,
- parentId: 1,
- planFinishDate: null
- );
- }
- /// 更新挑战标题
- void updateTitle(String title) {
- state = AsyncData(state.value!.copyWith(title: title));
- }
- /// 更新挑战描述
- void updateDescription(String description) {
- state = AsyncData(state.value!.copyWith(description: description));
- }
- /// 更新目标类型
- void updateActionType(int actionType) {
- state = AsyncData(state.value!.copyWith(actionType: actionType));
- }
- /// 更新日期范围
- void updateDateRange(DateTime start, DateTime end) {
- state = AsyncData(state.value!.copyWith(startDate: start, endDate: end));
- }
- /// 更新封面
- void updateCover(String cover) {
- state = AsyncData(state.value!.copyWith(cover: cover));
- }
- /// 更新排序
- void updateSort(int sort) {
- state = AsyncData(state.value!.copyWith(sort: sort));
- }
-
- /// 更新挑战状态
- void updateStatus(int status) {
- state = AsyncData(state.value!.copyWith(status: status));
- }
- /// 更新难度级别
- void updateDifficulty(int difficulty) {
- state = AsyncData(state.value!.copyWith(difficulty: difficulty));
- }
- /// 更新备注
- void updateRemark(String remark) {
- state = AsyncData(state.value!.copyWith(remark: remark));
- }
- /// 更新所属父目标
- void updateParentId(int parentId) {
- state = AsyncData(state.value!.copyWith(parentId: parentId));
- }
- /// 更新计划完成时间
- void updatePlanFinishDate(DateTime planFinishDate) {
- state = AsyncData(state.value!.copyWith(planFinishDate: planFinishDate));
- }
- /// 更新完成时间
- void updateFinishDate(DateTime finishDate) {
- state = AsyncData(state.value!.copyWith(finishDate: finishDate));
- }
- /// 提交新挑战
- Future<void> submitChallenge() async {
- if (state.value == null) return;
- state = const AsyncLoading();
- state = await AsyncValue.guard(
- () => _repository.addChallenge(state.value!),
- );
- }
- }
|