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';
|
2021-01-09 02:11:30 +01:00
|
|
|
import 'package:flutter/services.dart';
|
2020-09-02 00:05:42 +02:00
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2021-01-24 20:01:55 +01:00
|
|
|
import 'package:lemmy_api_client/v2.dart';
|
2020-09-02 00:05:42 +02:00
|
|
|
|
2020-09-21 20:19:14 +02:00
|
|
|
import '../hooks/logged_in_action.dart';
|
2021-01-09 17:34:24 +01:00
|
|
|
import '../hooks/refreshable.dart';
|
2020-09-17 18:38:03 +02:00
|
|
|
import '../hooks/stores.dart';
|
2020-12-04 13:57:37 +01:00
|
|
|
import '../util/more_icon.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;
|
2020-12-31 14:58:23 +01:00
|
|
|
final String instanceHost;
|
2020-09-02 00:05:42 +02:00
|
|
|
final PostView post;
|
|
|
|
|
2021-01-09 17:34:24 +01:00
|
|
|
const FullPostPage({@required this.id, @required this.instanceHost})
|
2020-09-03 13:02:38 +02:00
|
|
|
: assert(id != null),
|
2020-12-31 14:58:23 +01:00
|
|
|
assert(instanceHost != null),
|
2020-09-03 13:02:38 +02:00
|
|
|
post = null;
|
|
|
|
FullPostPage.fromPostView(this.post)
|
2021-01-24 20:01:55 +01:00
|
|
|
: id = post.post.id,
|
2020-12-31 14:58:23 +01:00
|
|
|
instanceHost = post.instanceHost;
|
2020-09-02 00:05:42 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-09-17 18:37:27 +02:00
|
|
|
final accStore = useAccountsStore();
|
2021-01-24 20:01:55 +01:00
|
|
|
final fullPostRefreshable =
|
|
|
|
useRefreshable(() => LemmyApiV2(instanceHost).run(GetPost(
|
|
|
|
id: id,
|
|
|
|
auth: accStore.defaultTokenFor(instanceHost)?.raw,
|
|
|
|
)));
|
2020-12-31 14:58:23 +01:00
|
|
|
final loggedInAction = useLoggedInAction(instanceHost);
|
2020-09-21 13:30:47 +02:00
|
|
|
final newComments = useState(const <CommentView>[]);
|
2020-09-09 19:23:41 +02:00
|
|
|
|
2021-01-09 17:34:24 +01:00
|
|
|
// FALLBACK VIEW
|
|
|
|
if (!fullPostRefreshable.snapshot.hasData && this.post == null) {
|
2020-09-09 19:23:41 +02:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(),
|
|
|
|
body: Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2021-01-09 17:34:24 +01:00
|
|
|
if (fullPostRefreshable.snapshot.hasError)
|
|
|
|
Text(fullPostRefreshable.snapshot.error.toString())
|
2020-09-09 19:23:41 +02:00
|
|
|
else
|
|
|
|
const CircularProgressIndicator(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// VARIABLES
|
|
|
|
|
2021-01-09 17:34:24 +01:00
|
|
|
final post = fullPostRefreshable.snapshot.hasData
|
2021-01-24 20:01:55 +01:00
|
|
|
? fullPostRefreshable.snapshot.data.postView
|
2021-01-09 17:34:24 +01:00
|
|
|
: this.post;
|
2020-09-09 19:23:41 +02:00
|
|
|
|
2021-01-09 17:34:24 +01:00
|
|
|
final fullPost = fullPostRefreshable.snapshot.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
|
|
|
|
2021-01-09 02:11:30 +01:00
|
|
|
refresh() async {
|
|
|
|
await HapticFeedback.mediumImpact();
|
|
|
|
|
|
|
|
try {
|
2021-01-09 17:34:24 +01:00
|
|
|
await fullPostRefreshable.refresh();
|
2021-01-09 02:11:30 +01:00
|
|
|
// ignore: avoid_catches_without_on_clauses
|
|
|
|
} catch (e) {
|
|
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(e.toString()),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 20:01:55 +01:00
|
|
|
sharePost() => Share.text('Share post', 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(
|
2021-01-03 19:43:39 +01:00
|
|
|
leading: const BackButton(),
|
2020-09-09 19:23:41 +02:00
|
|
|
actions: [
|
2021-01-03 19:43:39 +01:00
|
|
|
IconButton(icon: const Icon(Icons.share), onPressed: sharePost),
|
2020-09-17 22:50:18 +02:00
|
|
|
SavePostButton(post),
|
2020-09-09 19:23:41 +02:00
|
|
|
IconButton(
|
2020-12-04 13:57:37 +01:00
|
|
|
icon: Icon(moreIcon),
|
2021-01-24 20:01:55 +01:00
|
|
|
onPressed: () => PostWidget.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()),
|
2021-01-03 19:43:39 +01:00
|
|
|
child: const Icon(Icons.comment)),
|
2021-01-09 02:11:30 +01:00
|
|
|
body: RefreshIndicator(
|
|
|
|
onRefresh: refresh,
|
|
|
|
child: ListView(
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
children: [
|
2021-01-24 20:01:55 +01:00
|
|
|
PostWidget(post, fullPost: true),
|
2021-01-09 17:34:24 +01:00
|
|
|
if (fullPostRefreshable.snapshot.hasData)
|
2021-01-09 02:11:30 +01:00
|
|
|
CommentSection(
|
|
|
|
newComments.value.followedBy(fullPost.comments).toList(),
|
2021-01-24 20:01:55 +01:00
|
|
|
postCreatorId: fullPost.postView.creator.id)
|
2021-01-09 17:34:24 +01:00
|
|
|
else if (fullPostRefreshable.snapshot.hasError)
|
2021-01-09 02:11:30 +01:00
|
|
|
Padding(
|
|
|
|
padding:
|
|
|
|
const EdgeInsets.symmetric(horizontal: 10, vertical: 30),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.error),
|
2021-01-09 17:34:24 +01:00
|
|
|
Text('Error: ${fullPostRefreshable.snapshot.error}')
|
2021-01-09 02:11:30 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else
|
|
|
|
const Padding(
|
|
|
|
padding: EdgeInsets.only(top: 40),
|
|
|
|
child: Center(child: CircularProgressIndicator()),
|
2020-09-17 18:34:03 +02:00
|
|
|
),
|
2021-01-09 02:11:30 +01:00
|
|
|
],
|
|
|
|
),
|
2020-09-09 19:23:41 +02:00
|
|
|
));
|
2020-09-02 00:05:42 +02:00
|
|
|
}
|
|
|
|
}
|