Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:akoon/data/model/goal.dart';
- import 'package:akoon/data/model/result.dart';
- import 'package:akoon/data/repository/goals_repository.dart';
- import 'package:akoon/infinite_list/infinite_list_view_model.dart';
- import 'package:akoon/service/global_event_bus.dart';
- import 'package:get_it/get_it.dart';
- import 'package:injectable/injectable.dart';
- import 'package:mobx/mobx.dart';
- part 'goals_view_model.g.dart';
- @singleton
- class GoalsViewModel = _GoalsViewModel with _$GoalsViewModel;
- abstract class _GoalsViewModel extends InfiniteListViewModel<Goal>
- with Store
- implements Disposable {
- final GoalsRepository _goalsRepository;
- final disposeBag = <CancelEvent>[];
- _GoalsViewModel(
- this._goalsRepository,
- ) {
- disposeBag.add(
- GlobalEventBus.subscribe(
- receiver: this,
- eventName: GlobalEvent.goalAdded.name,
- onEvent: (event) {
- refresh();
- },
- ),
- );
- disposeBag.add(GlobalEventBus.subscribe(
- receiver: this,
- eventName: GlobalEvent.goalUpdated.name,
- onEvent: (event) {
- final goal = event?['goal'] as Goal?;
- final id = event?['id'] as String?;
- _onGoalUpdateEvent(id, goal);
- },
- ));
- disposeBag.add(
- GlobalEventBus.subscribe(
- receiver: this,
- eventName: GlobalEvent.goalDeleted.name,
- onEvent: (event) {
- refresh();
- GlobalEventBus.sendEvent(
- name: GlobalEvent.refreshPreviousGoals.name,
- sender: this,
- );
- },
- ),
- );
- }
- @action
- void _onGoalUpdateEvent(String? id, Goal? goal) {
- if (id == null && goal == null) return;
- final goalIndex =
- state.items.indexWhere((element) => element.id == (id ?? goal?.id));
- if (goalIndex < 0) return;
- final newList = [...state.items];
- if (goal != null) {
- if (goal.achievedPercentage == 100) {
- refresh();
- GlobalEventBus.sendEvent(
- name: GlobalEvent.refreshPreviousGoals.name,
- sender: this,
- );
- return;
- }
- newList[goalIndex] = goal;
- setState(state.copyWith(items: newList));
- return;
- }
- if (id != null) {
- refreshGoal(id);
- }
- }
- @action
- Future<bool> refreshGoal(String id) async {
- final goalIndex = state.items.indexWhere((element) => element.id == id);
- if (goalIndex < 0) return false;
- final newList = [...state.items];
- final result = await _goalsRepository.getGoal(id);
- switch (result) {
- case ResultValue():
- newList[goalIndex] = result.value.data;
- setState(state.copyWith(items: newList));
- return true;
- case ResultException():
- return false;
- default:
- throw UnsupportedError(result.toString());
- }
- }
- @action
- Future<bool> deleteGoal({
- required String goalId,
- int? index,
- }) async {
- final oldList = _deleteGoalLocal(
- goalId: goalId,
- index: index,
- );
- final isOk = await _goalsRepository.deleteGoal(
- goalId,
- );
- if (isOk) {
- GlobalEventBus.sendEvent(
- name: GlobalEvent.goalDeleted.name,
- sender: this,
- );
- return true;
- }
- setState(state.copyWith(items: oldList));
- return false;
- }
- @action
- Future<bool> deleteGoalSoft({
- required String goalId,
- int? index,
- }) async {
- final oldList = _deleteGoalLocal(
- goalId: goalId,
- index: index,
- );
- final isOk = await _goalsRepository.deleteGoalSoft(
- goalId,
- );
- if (isOk) {
- GlobalEventBus.sendEvent(
- name: GlobalEvent.goalDeleted.name,
- sender: this,
- );
- return true;
- }
- setState(state.copyWith(items: oldList));
- return false;
- }
- List<Goal>? _deleteGoalLocal({
- required String goalId,
- int? index,
- }) {
- final oldList = state.items;
- if (index != null) {
- setState(state.copyWith(items: [...state.items]..removeAt(index)));
- return oldList;
- }
- final targetIndex =
- state.items.indexWhere((element) => element.id == goalId);
- setState(state.copyWith(items: [...state.items]..removeAt(targetIndex)));
- return oldList;
- }
- @override
- Future<Result<List<Goal>, Object?>> fetchData(
- int pageNumber,
- int pageSize,
- ) async {
- final result = await _goalsRepository.getGoals(
- limit: pageSize,
- page: pageNumber,
- );
- switch (result) {
- case ResultValue():
- return ResultValue(result.value.data);
- case ResultException():
- return result;
- default:
- throw UnsupportedError(result.toString());
- }
- }
- @override
- FutureOr onDispose() {
- for (final dispose in disposeBag) {
- dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement