| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- // 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 updateDifficulty(int difficulty) {
- state = AsyncData(state.value!.copyWith(difficulty: difficulty));
- }
- /// 更新日期范围
- void updateDateRange(DateTime start, DateTime end) {
- state = AsyncData(state.value!.copyWith(startDate: start, endDate: end));
- }
- /// 提交新挑战
- Future<void> submitChallenge() async {
- if (state.value == null) return;
- state = const AsyncLoading();
- state = await AsyncValue.guard(
- () => _repository.addChallenge(state.value!),
- );
- }
- }
|