2020-09-02 00:05:42 +02:00
|
|
|
import 'package:esys_flutter_share/esys_flutter_share.dart';
|
2020-09-21 20:19:14 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-09-02 00:05:42 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2020-09-03 13:02:38 +02:00
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
2020-09-02 00:05:42 +02:00
|
|
|
|
2020-09-21 20:19:14 +02:00
|
|
|
import '../hooks/logged_in_action.dart';
|
2020-09-17 18:34:03 +02:00
|
|
|
import '../hooks/memo_future.dart';
|
2020-09-17 18:38:03 +02:00
|
|
|
import '../hooks/stores.dart';
|
2020-09-19 00:40:47 +02:00
|
|
|
import '../util/extensions/api.dart';
|
2020-09-02 00:05:42 +02:00
|
|
|
import '../widgets/comment_section.dart';
|
|
|
|
import '../widgets/post.dart';
|
2020-09-17 18:34:03 +02:00
|
|
|
import '../widgets/save_post_button.dart';
|
2020-09-21 13:30:47 +02:00
|
|
|
import '../widgets/write_comment.dart';
|
2020-09-02 00:05:42 +02:00
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// Displays a post with its comment section
|
2020-09-02 00:05:42 +02:00
|
|
|
class FullPostPage extends HookWidget {
|
2020-09-17 18:34:03 +02:00
|
|
|
final int id;
|
|
|
|
final String instanceUrl;
|
2020-09-02 00:05:42 +02:00
|
|
|
final PostView post;
|
|
|
|
|
2020-09-17 18:34:03 +02:00
|
|
|
FullPostPage({@required this.id, @required this.instanceUrl})
|
2020-09-03 13:02:38 +02:00
|
|
|
: assert(id != null),
|
|
|
|
assert(instanceUrl != null),
|
|
|
|
post = null;
|
|
|
|
FullPostPage.fromPostView(this.post)
|
2020-09-17 18:34:03 +02:00
|
|
|
: id = post.id,
|
|
|
|
instanceUrl = post.instanceUrl;
|
2020-09-02 00:05:42 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-09-17 18:37:27 +02:00
|
|
|
final accStore = useAccountsStore();
|
|
|
|
final fullPostSnap = useMemoFuture(() => LemmyApi(instanceUrl)
|
|
|
|
.v1
|
|
|
|
.getPost(id: id, auth: accStore.defaultTokenFor(instanceUrl)?.raw));
|
2020-09-21 20:19:14 +02:00
|
|
|
final loggedInAction = useLoggedInAction(instanceUrl);
|
2020-09-21 13:30:47 +02:00
|
|
|
final newComments = useState(const <CommentView>[]);
|
2020-09-09 19:23:41 +02:00
|
|
|
|
|
|
|
// FALLBACK VIEW
|
|
|
|
|
|
|
|
if (!fullPostSnap.hasData && this.post == null) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(),
|
|
|
|
body: Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
if (fullPostSnap.hasError)
|
|
|
|
Text(fullPostSnap.error.toString())
|
|
|
|
else
|
|
|
|
const CircularProgressIndicator(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// VARIABLES
|
|
|
|
|
|
|
|
final post = fullPostSnap.hasData ? fullPostSnap.data.post : this.post;
|
|
|
|
|
2020-09-03 13:56:37 +02:00
|
|
|
final fullPost = fullPostSnap.data;
|
2020-09-02 00:05:42 +02:00
|
|
|
|
2020-09-09 19:23:41 +02:00
|
|
|
// FUNCTIONS
|
2020-09-02 00:05:42 +02:00
|
|
|
|
2020-09-09 19:23:41 +02:00
|
|
|
sharePost() => Share.text('Share post', post.apId, 'text/plain');
|
2020-09-02 00:05:42 +02:00
|
|
|
|
2020-09-21 13:30:47 +02:00
|
|
|
comment() async {
|
2020-09-21 20:18:57 +02:00
|
|
|
final newComment = await showCupertinoModalPopup<CommentView>(
|
2020-09-21 13:30:47 +02:00
|
|
|
context: context,
|
2020-09-21 20:18:57 +02:00
|
|
|
builder: (_) => WriteComment.toPost(post),
|
2020-09-21 13:30:47 +02:00
|
|
|
);
|
|
|
|
if (newComment != null) {
|
|
|
|
newComments.value = [...newComments.value, newComment];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 00:05:42 +02:00
|
|
|
return Scaffold(
|
2020-09-09 19:23:41 +02:00
|
|
|
appBar: AppBar(
|
|
|
|
leading: BackButton(),
|
|
|
|
actions: [
|
|
|
|
IconButton(icon: Icon(Icons.share), onPressed: sharePost),
|
2020-09-17 22:50:18 +02:00
|
|
|
SavePostButton(post),
|
2020-09-09 19:23:41 +02:00
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.more_vert),
|
2020-09-09 21:23:48 +02:00
|
|
|
onPressed: () => Post.showMoreMenu(context, post)),
|
2020-09-09 19:23:41 +02:00
|
|
|
],
|
|
|
|
),
|
2020-09-21 13:30:47 +02:00
|
|
|
floatingActionButton: FloatingActionButton(
|
2020-09-21 20:19:14 +02:00
|
|
|
onPressed: loggedInAction((_) => comment()),
|
|
|
|
child: Icon(Icons.comment)),
|
2020-09-09 19:23:41 +02:00
|
|
|
body: ListView(
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
children: [
|
|
|
|
Post(post, fullPost: true),
|
|
|
|
if (fullPostSnap.hasData)
|
2020-09-21 13:30:47 +02:00
|
|
|
CommentSection(
|
|
|
|
newComments.value.followedBy(fullPost.comments).toList(),
|
2020-09-09 19:23:41 +02:00
|
|
|
postCreatorId: fullPost.post.creatorId)
|
2020-09-17 18:34:03 +02:00
|
|
|
else if (fullPostSnap.hasError)
|
|
|
|
Padding(
|
|
|
|
padding:
|
|
|
|
const EdgeInsets.symmetric(horizontal: 10, vertical: 30),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Icon(Icons.error),
|
|
|
|
Text('Error: ${fullPostSnap.error}')
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
2020-09-09 19:23:41 +02:00
|
|
|
else
|
|
|
|
Container(
|
|
|
|
child: Center(child: CircularProgressIndicator()),
|
|
|
|
padding: EdgeInsets.only(top: 40),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
));
|
2020-09-02 00:05:42 +02:00
|
|
|
}
|
|
|
|
}
|