diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index fc3ec9d..49b9165 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -84,6 +84,20 @@ class PostsHelper { } } + /// Get a single post information + Future getSingle(int postID) async { + final response = await APIRequest( + uri: "posts/get_single", + args: {"postID": postID.toString()}, + needLogin: true, + ).exec(); + + if (!response.isOK) + throw Exception("Could not get information about the post!"); + + return _apiToPost(response.getObject()); + } + /// Create a new post /// /// This function crash in case of error diff --git a/lib/ui/routes/single_post_route.dart b/lib/ui/routes/single_post_route.dart new file mode 100644 index 0000000..3cc4e0c --- /dev/null +++ b/lib/ui/routes/single_post_route.dart @@ -0,0 +1,62 @@ +import 'package:comunic/helpers/posts_helper.dart'; +import 'package:comunic/lists/posts_list.dart'; +import 'package:comunic/ui/widgets/posts_list_widget.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +/// Single post route +/// +/// @author Pierre HUBERT + +class SinglePostRoute extends StatelessWidget { + final int postID; + + const SinglePostRoute({ + Key key, + @required this.postID, + }) : assert(postID != null), + super(key: key); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(tr("Post")), + ), + body: _SinglePostRouteBody(postID: postID), + ); + } +} + +class _SinglePostRouteBody extends StatefulWidget { + final int postID; + + const _SinglePostRouteBody({ + Key key, + @required this.postID, + }) : assert(postID != null), + super(key: key); + + @override + __SinglePostRouteBodyState createState() => __SinglePostRouteBodyState(); +} + +class __SinglePostRouteBodyState extends State<_SinglePostRouteBody> { + Future _getPost() async { + try { + return PostsList()..add(await PostsHelper().getSingle(widget.postID)); + } on Exception catch (e) { + print(e); + return null; + } + } + + @override + Widget build(BuildContext context) { + return PostsListWidget( + getPostsList: _getPost, + showPostsTarget: true, + ); + } +} diff --git a/lib/ui/screens/notifications_screen.dart b/lib/ui/screens/notifications_screen.dart index 12e7f9d..8504eae 100644 --- a/lib/ui/screens/notifications_screen.dart +++ b/lib/ui/screens/notifications_screen.dart @@ -8,6 +8,7 @@ import 'package:comunic/models/notification.dart' as n; import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/utils/date_utils.dart'; import 'package:comunic/utils/intl_utils.dart'; +import 'package:comunic/utils/navigation_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; @@ -242,6 +243,7 @@ class _NotificationTile extends StatelessWidget { leading: AccountImageWidget( user: srcUser, ), + onTap: () => _onTap(context), title: Text(message), subtitle: Text(diffTimeFromNowToStr(notification.timeCreate)), trailing: PopupMenuButton<_PopupMenuActions>( @@ -263,4 +265,12 @@ class _NotificationTile extends StatelessWidget { break; } } + + void _onTap(BuildContext context) { + if (notification.onElemType == n.NotificationElementType.POST) { + openPostFullScreen(notification.onElemId, context); + } + + // TODO : mark the notification as seen + } } diff --git a/lib/ui/widgets/posts_list_widget.dart b/lib/ui/widgets/posts_list_widget.dart index e06e129..da4d427 100644 --- a/lib/ui/widgets/posts_list_widget.dart +++ b/lib/ui/widgets/posts_list_widget.dart @@ -76,9 +76,7 @@ class _PostsListWidgetState extends State { /// Load the list of posts Future _loadPostsList({bool getOlder = false}) async { - - if(_loading) - return; + if (_loading) return; _loading = true; diff --git a/lib/utils/navigation_utils.dart b/lib/utils/navigation_utils.dart index ef94bf6..ff7f93a 100644 --- a/lib/utils/navigation_utils.dart +++ b/lib/utils/navigation_utils.dart @@ -1,3 +1,4 @@ +import 'package:comunic/ui/routes/single_post_route.dart'; import 'package:comunic/ui/routes/user_page_route.dart'; import 'package:flutter/material.dart'; import 'package:meta/meta.dart'; @@ -14,8 +15,14 @@ void openUserPage({@required int userID, @required BuildContext context}) { Navigator.of(context).push( MaterialPageRoute( builder: (c) => UserPageRoute( - userID: userID, - ), + userID: userID, + ), ), ); } + +/// Open a post in full screen +void openPostFullScreen(int postID, BuildContext context) { + Navigator.of(context) + .push(MaterialPageRoute(builder: (c) => SinglePostRoute(postID: postID))); +}