// 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.new, ); class AddChallengeViewModel extends AsyncNotifier { ChallengeRepository get _repository => ref.read(challengeRepositoryProvider); @override Future 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 submitChallenge() async { if (state.value == null) return; state = const AsyncLoading(); state = await AsyncValue.guard( () => _repository.addChallenge(state.value!), ); } }