From add1712b7d128b584647e13d391ad5b61202ee60 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 15 Apr 2020 18:39:07 +0200 Subject: [PATCH] Can update following status of a group --- lib/helpers/groups_helper.dart | 8 +++ lib/models/group.dart | 4 +- .../screens/authorized_group_page_screen.dart | 16 +++++- .../screens/group_access_denied_screen.dart | 2 +- lib/ui/widgets/group_following_widget.dart | 55 +++++++++++++++++++ 5 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 lib/ui/widgets/group_following_widget.dart diff --git a/lib/helpers/groups_helper.dart b/lib/helpers/groups_helper.dart index 3852a82..5404ac9 100644 --- a/lib/helpers/groups_helper.dart +++ b/lib/helpers/groups_helper.dart @@ -148,6 +148,14 @@ class GroupsHelper { "accept": accept ? "true" : "false", }); + /// Update group following status + Future setFollowing(int groupID, bool follow) async => + (await (APIRequest(uri: "groups/set_following", needLogin: true) + ..addInt("groupID", groupID) + ..addBool("follow", follow)) + .exec()) + .isOK; + /// Get advanced information about the user Future getAdvancedInfo(int groupID) async { // Get advanced information about the user diff --git a/lib/models/group.dart b/lib/models/group.dart index b3dfabc..79bb0d6 100644 --- a/lib/models/group.dart +++ b/lib/models/group.dart @@ -29,7 +29,7 @@ class Group { final GroupRegistrationLevel registrationLevel; final GroupPostCreationLevel postCreationLevel; final String virtualDirectory; - final bool following; + bool following; Group({ @required this.id, @@ -54,7 +54,7 @@ class Group { get displayName => this.name; - bool get getIsAtLeastMember => + bool get isAtLeastMember => membershipLevel == GroupMembershipLevel.ADMINISTRATOR || membershipLevel == GroupMembershipLevel.MODERATOR || membershipLevel == GroupMembershipLevel.MEMBER; diff --git a/lib/ui/screens/authorized_group_page_screen.dart b/lib/ui/screens/authorized_group_page_screen.dart index 604c8dd..3f7acd1 100644 --- a/lib/ui/screens/authorized_group_page_screen.dart +++ b/lib/ui/screens/authorized_group_page_screen.dart @@ -1,4 +1,5 @@ import 'package:comunic/models/advanced_group_info.dart'; +import 'package:comunic/ui/widgets/group_following_widget.dart'; import 'package:comunic/ui/widgets/group_icon_widget.dart'; import 'package:comunic/ui/widgets/group_membership_widget.dart'; import 'package:flutter/material.dart'; @@ -50,9 +51,18 @@ class _AuthorizedGroupPageScreenState extends State { style: TextStyle(fontSize: 20), ), Spacer(), - GroupMembershipWidget( - group: _group, - onUpdated: () => widget.needRefresh(), + Column( + children: [ + GroupMembershipWidget( + group: _group, + onUpdated: () => widget.needRefresh(), + ), + Container(height: 10,), + GroupFollowingWidget( + group: _group, + onUpdated: () => widget.needRefresh(), + ), + ], ) ], ), diff --git a/lib/ui/screens/group_access_denied_screen.dart b/lib/ui/screens/group_access_denied_screen.dart index c0c1df0..9f5e821 100644 --- a/lib/ui/screens/group_access_denied_screen.dart +++ b/lib/ui/screens/group_access_denied_screen.dart @@ -95,7 +95,7 @@ class _GroupAccessDeniedScreenState extends SafeState { // Get information about a single group final group = await GroupsHelper().getSingle(widget.groupID, force: true); - if (group.getIsAtLeastMember) widget.onMembershipAcquired(); + if (group.isAtLeastMember) widget.onMembershipAcquired(); setState(() => _group = group); } catch (e) { diff --git a/lib/ui/widgets/group_following_widget.dart b/lib/ui/widgets/group_following_widget.dart new file mode 100644 index 0000000..c34115d --- /dev/null +++ b/lib/ui/widgets/group_following_widget.dart @@ -0,0 +1,55 @@ +import 'package:comunic/helpers/groups_helper.dart'; +import 'package:comunic/models/group.dart'; +import 'package:comunic/ui/widgets/safe_state.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:comunic/utils/ui_utils.dart'; +import 'package:flutter/material.dart'; + +/// Group following status widget +/// +/// @author Pierre Hubert + +class GroupFollowingWidget extends StatefulWidget { + final Group group; + final Function() onUpdated; + + const GroupFollowingWidget( + {Key key, @required this.group, @required this.onUpdated}) + : assert(group != null), + assert(onUpdated != null), + super(key: key); + + @override + _GroupFollowingWidgetState createState() => _GroupFollowingWidgetState(); +} + +class _GroupFollowingWidgetState extends SafeState { + Group get _group => widget.group; + + @override + Widget build(BuildContext context) { + if (!_group.isAtLeastMember) return Container(); + + return InkWell( + child: Text( + _group.following ? tr("Following") : tr("Follow"), + style: TextStyle(color: Colors.blue), + ), + onTap: () => _toggleFollowing(), + ); + } + + /// Toggle following status + void _toggleFollowing() async { + if (!await GroupsHelper().setFollowing(_group.id, !_group.following)) { + showSimpleSnack(context, tr("Could not update following status!")); + return; + } + + setState(() { + _group.following = !_group.following; + + this.widget.onUpdated(); + }); + } +}