lemmur-app-android/lib/pages/full_post/full_post_store.dart

103 lines
2.6 KiB
Dart
Raw Normal View History

2021-09-11 01:04:15 +02:00
import 'package:lemmy_api_client/v3.dart';
import 'package:mobx/mobx.dart';
2021-10-01 23:03:42 +02:00
import '../../comment_tree.dart';
2021-09-11 01:04:15 +02:00
import '../../util/async_store.dart';
import '../../widgets/post/post_store.dart';
2021-09-11 01:04:15 +02:00
part 'full_post_store.g.dart';
class FullPostStore = _FullPostStore with _$FullPostStore;
abstract class _FullPostStore with Store {
final int postId;
final String instanceHost;
_FullPostStore({
this.postStore,
required this.postId,
required this.instanceHost,
});
// ignore: unused_element
_FullPostStore.fromPostView(PostView postView)
: postId = postView.post.id,
instanceHost = postView.instanceHost,
postStore = PostStore(postView);
// ignore: unused_element
_FullPostStore.fromPostStore(PostStore this.postStore)
: postId = postStore.postView.post.id,
instanceHost = postStore.postView.instanceHost;
2021-09-11 01:04:15 +02:00
@observable
FullPostView? fullPostView;
@observable
2021-10-01 23:03:42 +02:00
ObservableList<CommentView> newComments = ObservableList<CommentView>();
@observable
CommentSortType sorting = CommentSortType.hot;
@observable
PostStore? postStore;
final fullPostState = AsyncStore<FullPostView>();
final communityBlockingState = AsyncStore<BlockedCommunity>();
2021-10-01 23:03:42 +02:00
@action
// ignore: use_setters_to_change_properties
void updateSorting(CommentSortType sort) {
sorting = sort;
}
@computed
List<CommentTree>? get commentTree {
if (fullPostView == null) return null;
return CommentTree.fromList(fullPostView!.comments);
}
@computed
List<CommentTree>? get sortedCommentTree {
return commentTree?..sortBy(sorting);
}
2021-09-11 01:04:15 +02:00
@computed
PostView? get postView => postStore?.postView;
2021-09-11 01:04:15 +02:00
@computed
Iterable<CommentView>? get comments =>
fullPostView?.comments.followedBy(newComments);
2021-09-11 01:04:15 +02:00
@action
Future<void> refresh([Jwt? token]) async {
final result = await fullPostState.runLemmy(
instanceHost, GetPost(id: postId, auth: token?.raw));
2021-09-11 01:04:15 +02:00
if (result != null) {
postStore ??= PostStore(result.postView);
fullPostView = result;
postStore!.updatePostView(result.postView);
2021-09-11 01:04:15 +02:00
}
}
@action
Future<void> blockCommunity(Jwt token) async {
final result = await communityBlockingState.runLemmy(
instanceHost,
BlockCommunity(
communityId: fullPostView!.communityView.community.id,
block: !fullPostView!.communityView.blocked,
auth: token.raw));
if (result != null) {
fullPostView =
fullPostView!.copyWith(communityView: result.communityView);
}
}
@action
2021-09-11 01:04:15 +02:00
void addComment(CommentView commentView) =>
newComments.insert(0, commentView);
}