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';
|
2021-10-01 23:39:43 +02:00
|
|
|
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;
|
|
|
|
|
2021-10-24 14:19:12 +02:00
|
|
|
_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;
|
|
|
|
|
2021-10-24 14:19:12 +02:00
|
|
|
@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
|
2021-09-15 00:23:55 +02:00
|
|
|
PostView? get postView => postStore?.postView;
|
2021-09-11 01:04:15 +02:00
|
|
|
|
|
|
|
@computed
|
2021-10-24 14:19:12 +02:00
|
|
|
Iterable<CommentView>? get comments =>
|
|
|
|
fullPostView?.comments.followedBy(newComments);
|
2021-09-11 01:04:15 +02:00
|
|
|
|
|
|
|
@action
|
2021-09-15 00:23:55 +02:00
|
|
|
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;
|
2021-10-24 14:19:12 +02:00
|
|
|
postStore!.updatePostView(result.postView);
|
2021-09-11 01:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 23:48:04 +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);
|
|
|
|
}
|