import 'package:lemmy_api_client/v3.dart'; import 'package:mobx/mobx.dart'; import '../../util/async_store.dart'; part 'blocks_store.g.dart'; class BlocksStore = _BlocksStore with _$BlocksStore; abstract class _BlocksStore with Store { final String instanceHost; final Jwt token; ObservableList blockedUsers; ObservableList blockedCommunities; final blockCommunityState = AsyncStore(); final blockUserState = AsyncStore(); final blocksState = AsyncStore(); @action void userUnblocked(int id) => blockedUsers.removeWhere((element) => element.id == id); @action void communityUnblocked(int id) => blockedCommunities.removeWhere((element) => element.id == id); @action Future blockUser(int id) async { final result = await blockUserState.runLemmy( instanceHost, BlockPerson(personId: id, block: true, auth: token.raw), ); if (result != null) blockedUsers.add(result.personView.person); } @action Future blockCommunity(int id) async { final result = await blockCommunityState.runLemmy( instanceHost, BlockCommunity(communityId: id, block: true, auth: token.raw), ); if (result != null) blockedCommunities.add(result.communityView.community); } @action Future refresh() async { final result = await blocksState.runLemmy(instanceHost, GetSite(auth: token.raw)); if (result != null) { blockedUsers = result.myUser!.personBlocks .map((e) => e.target) .toList() .asObservable(); blockedCommunities = result.myUser!.communityBlocks .map((e) => e.community) .toList() .asObservable(); } } _BlocksStore({required this.instanceHost, required this.token}) : blockedUsers = [].asObservable(), blockedCommunities = [].asObservable(); }