Advertisement
hosamkora

Untitled

Jul 10th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:akoon/data/model/goal.dart';
  4. import 'package:akoon/data/model/result.dart';
  5. import 'package:akoon/data/repository/goals_repository.dart';
  6. import 'package:akoon/infinite_list/infinite_list_view_model.dart';
  7. import 'package:akoon/service/global_event_bus.dart';
  8. import 'package:get_it/get_it.dart';
  9. import 'package:injectable/injectable.dart';
  10. import 'package:mobx/mobx.dart';
  11.  
  12. part 'goals_view_model.g.dart';
  13.  
  14. @singleton
  15. class GoalsViewModel = _GoalsViewModel with _$GoalsViewModel;
  16.  
  17. abstract class _GoalsViewModel extends InfiniteListViewModel<Goal>
  18. with Store
  19. implements Disposable {
  20. final GoalsRepository _goalsRepository;
  21. final disposeBag = <CancelEvent>[];
  22.  
  23. _GoalsViewModel(
  24. this._goalsRepository,
  25. ) {
  26. disposeBag.add(
  27. GlobalEventBus.subscribe(
  28. receiver: this,
  29. eventName: GlobalEvent.goalAdded.name,
  30. onEvent: (event) {
  31. refresh();
  32. },
  33. ),
  34. );
  35.  
  36. disposeBag.add(GlobalEventBus.subscribe(
  37. receiver: this,
  38. eventName: GlobalEvent.goalUpdated.name,
  39. onEvent: (event) {
  40. final goal = event?['goal'] as Goal?;
  41. final id = event?['id'] as String?;
  42. _onGoalUpdateEvent(id, goal);
  43. },
  44. ));
  45.  
  46. disposeBag.add(
  47. GlobalEventBus.subscribe(
  48. receiver: this,
  49. eventName: GlobalEvent.goalDeleted.name,
  50. onEvent: (event) {
  51. refresh();
  52. GlobalEventBus.sendEvent(
  53. name: GlobalEvent.refreshPreviousGoals.name,
  54. sender: this,
  55. );
  56. },
  57. ),
  58. );
  59. }
  60. @action
  61. void _onGoalUpdateEvent(String? id, Goal? goal) {
  62. if (id == null && goal == null) return;
  63. final goalIndex =
  64. state.items.indexWhere((element) => element.id == (id ?? goal?.id));
  65. if (goalIndex < 0) return;
  66. final newList = [...state.items];
  67.  
  68. if (goal != null) {
  69. if (goal.achievedPercentage == 100) {
  70. refresh();
  71. GlobalEventBus.sendEvent(
  72. name: GlobalEvent.refreshPreviousGoals.name,
  73. sender: this,
  74. );
  75. return;
  76. }
  77. newList[goalIndex] = goal;
  78. setState(state.copyWith(items: newList));
  79. return;
  80. }
  81. if (id != null) {
  82. refreshGoal(id);
  83. }
  84. }
  85.  
  86. @action
  87. Future<bool> refreshGoal(String id) async {
  88. final goalIndex = state.items.indexWhere((element) => element.id == id);
  89. if (goalIndex < 0) return false;
  90. final newList = [...state.items];
  91.  
  92. final result = await _goalsRepository.getGoal(id);
  93. switch (result) {
  94. case ResultValue():
  95. newList[goalIndex] = result.value.data;
  96. setState(state.copyWith(items: newList));
  97. return true;
  98. case ResultException():
  99. return false;
  100. default:
  101. throw UnsupportedError(result.toString());
  102. }
  103. }
  104.  
  105. @action
  106. Future<bool> deleteGoal({
  107. required String goalId,
  108. int? index,
  109. }) async {
  110. final oldList = _deleteGoalLocal(
  111. goalId: goalId,
  112. index: index,
  113. );
  114.  
  115. final isOk = await _goalsRepository.deleteGoal(
  116. goalId,
  117. );
  118. if (isOk) {
  119. GlobalEventBus.sendEvent(
  120. name: GlobalEvent.goalDeleted.name,
  121. sender: this,
  122. );
  123. return true;
  124. }
  125. setState(state.copyWith(items: oldList));
  126. return false;
  127. }
  128.  
  129. @action
  130. Future<bool> deleteGoalSoft({
  131. required String goalId,
  132. int? index,
  133. }) async {
  134. final oldList = _deleteGoalLocal(
  135. goalId: goalId,
  136. index: index,
  137. );
  138.  
  139. final isOk = await _goalsRepository.deleteGoalSoft(
  140. goalId,
  141. );
  142. if (isOk) {
  143. GlobalEventBus.sendEvent(
  144. name: GlobalEvent.goalDeleted.name,
  145. sender: this,
  146. );
  147. return true;
  148. }
  149. setState(state.copyWith(items: oldList));
  150. return false;
  151. }
  152.  
  153. List<Goal>? _deleteGoalLocal({
  154. required String goalId,
  155. int? index,
  156. }) {
  157. final oldList = state.items;
  158. if (index != null) {
  159. setState(state.copyWith(items: [...state.items]..removeAt(index)));
  160. return oldList;
  161. }
  162.  
  163. final targetIndex =
  164. state.items.indexWhere((element) => element.id == goalId);
  165. setState(state.copyWith(items: [...state.items]..removeAt(targetIndex)));
  166. return oldList;
  167. }
  168.  
  169. @override
  170. Future<Result<List<Goal>, Object?>> fetchData(
  171. int pageNumber,
  172. int pageSize,
  173. ) async {
  174. final result = await _goalsRepository.getGoals(
  175. limit: pageSize,
  176. page: pageNumber,
  177. );
  178.  
  179. switch (result) {
  180. case ResultValue():
  181. return ResultValue(result.value.data);
  182. case ResultException():
  183. return result;
  184. default:
  185. throw UnsupportedError(result.toString());
  186. }
  187. }
  188.  
  189. @override
  190. FutureOr onDispose() {
  191. for (final dispose in disposeBag) {
  192. dispose();
  193. }
  194. }
  195. }
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement