add pull to refresh to full post

This commit is contained in:
krawieck 2021-01-09 02:11:30 +01:00
parent 23c6261a37
commit 51f0fe4ef4
1 changed files with 48 additions and 26 deletions

View File

@ -1,6 +1,7 @@
import 'package:esys_flutter_share/esys_flutter_share.dart'; import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/lemmy_api_client.dart'; import 'package:lemmy_api_client/lemmy_api_client.dart';
@ -18,8 +19,10 @@ class FullPostPage extends HookWidget {
final int id; final int id;
final String instanceHost; final String instanceHost;
final PostView post; final PostView post;
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
const FullPostPage({@required this.id, @required this.instanceHost}) FullPostPage({@required this.id, @required this.instanceHost})
: assert(id != null), : assert(id != null),
assert(instanceHost != null), assert(instanceHost != null),
post = null; post = null;
@ -35,7 +38,7 @@ class FullPostPage extends HookWidget {
.getPost(id: id, auth: accStore.defaultTokenFor(instanceHost)?.raw)); .getPost(id: id, auth: accStore.defaultTokenFor(instanceHost)?.raw));
final loggedInAction = useLoggedInAction(instanceHost); final loggedInAction = useLoggedInAction(instanceHost);
final newComments = useState(const <CommentView>[]); final newComments = useState(const <CommentView>[]);
final updatedPost = useState<FullPostView>(null);
// FALLBACK VIEW // FALLBACK VIEW
if (!fullPostSnap.hasData && this.post == null) { if (!fullPostSnap.hasData && this.post == null) {
@ -63,6 +66,21 @@ class FullPostPage extends HookWidget {
// FUNCTIONS // FUNCTIONS
refresh() async {
await HapticFeedback.mediumImpact();
try {
return await LemmyApi(instanceHost)
.v1
.getPost(id: id, auth: accStore.defaultTokenFor(instanceHost)?.raw);
// ignore: avoid_catches_without_on_clauses
} catch (e) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(e.toString()),
));
}
}
sharePost() => Share.text('Share post', post.apId, 'text/plain'); sharePost() => Share.text('Share post', post.apId, 'text/plain');
comment() async { comment() async {
@ -89,31 +107,35 @@ class FullPostPage extends HookWidget {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: loggedInAction((_) => comment()), onPressed: loggedInAction((_) => comment()),
child: const Icon(Icons.comment)), child: const Icon(Icons.comment)),
body: ListView( body: RefreshIndicator(
physics: const AlwaysScrollableScrollPhysics(), onRefresh: refresh,
children: [ key: _refreshIndicatorKey,
Post(post, fullPost: true), child: ListView(
if (fullPostSnap.hasData) physics: const AlwaysScrollableScrollPhysics(),
CommentSection( children: [
newComments.value.followedBy(fullPost.comments).toList(), Post(post, fullPost: true),
postCreatorId: fullPost.post.creatorId) if (fullPostSnap.hasData)
else if (fullPostSnap.hasError) CommentSection(
Padding( newComments.value.followedBy(fullPost.comments).toList(),
padding: postCreatorId: fullPost.post.creatorId)
const EdgeInsets.symmetric(horizontal: 10, vertical: 30), else if (fullPostSnap.hasError)
child: Column( Padding(
children: [ padding:
const Icon(Icons.error), const EdgeInsets.symmetric(horizontal: 10, vertical: 30),
Text('Error: ${fullPostSnap.error}') child: Column(
], children: [
const Icon(Icons.error),
Text('Error: ${fullPostSnap.error}')
],
),
)
else
const Padding(
padding: EdgeInsets.only(top: 40),
child: Center(child: CircularProgressIndicator()),
), ),
) ],
else ),
const Padding(
padding: EdgeInsets.only(top: 40),
child: Center(child: CircularProgressIndicator()),
),
],
)); ));
} }
} }