diff --git a/lib/helpers/groups_helper.dart b/lib/helpers/groups_helper.dart index 42a75f5..9b226a6 100644 --- a/lib/helpers/groups_helper.dart +++ b/lib/helpers/groups_helper.dart @@ -100,6 +100,13 @@ class GroupsHelper { .map((f) => cast(f)) .toSet(); + /// Remove group membership + Future removeMembership(int groupID) async => + (await (APIRequest(uri: "groups/remove_membership", needLogin: true) + ..addInt("id", groupID)) + .exec()) + .isOK; + /// Turn an API entry into a group object Group _getGroupFromAPI(Map map) { return Group( diff --git a/lib/ui/screens/groups_list_screen.dart b/lib/ui/screens/groups_list_screen.dart index 707cdf1..3980000 100644 --- a/lib/ui/screens/groups_list_screen.dart +++ b/lib/ui/screens/groups_list_screen.dart @@ -1,5 +1,6 @@ import 'package:comunic/helpers/groups_helper.dart'; import 'package:comunic/lists/groups_list.dart'; +import 'package:comunic/models/group.dart'; import 'package:comunic/ui/widgets/group_icon_widget.dart'; import 'package:comunic/ui/widgets/safe_state.dart'; import 'package:comunic/utils/intl_utils.dart'; @@ -16,11 +17,11 @@ class GroupsListScreen extends StatefulWidget { } class _GroupsListScreenState extends SafeState { - /// The list of groups GroupsList _groups; - bool _error = false; + final _refreshIndicatorKey = GlobalKey(); + @override void initState() { super.initState(); @@ -47,6 +48,7 @@ class _GroupsListScreenState extends SafeState { // List of groups Expanded( child: RefreshIndicator( + key: _refreshIndicatorKey, onRefresh: () => this._refreshList(), child: _groups == null ? Container() @@ -55,6 +57,9 @@ class _GroupsListScreenState extends SafeState { .map((g) => ListTile( leading: GroupIcon(group: g), title: Text(g.displayName), + trailing: IconButton( + icon: Icon(Icons.delete), + onPressed: () => _deleteGroup(g)), )) .toList(), ), @@ -81,4 +86,19 @@ class _GroupsListScreenState extends SafeState { }); } } + + /// Remove a group from the list + void _deleteGroup(Group g) async { + if (!await showConfirmDialog( + context: context, + message: tr("Do you really want to delete this group membership ?"))) + return; + + if (!await GroupsHelper().removeMembership(g.id)) + showSimpleSnack( + context, tr("Could not remove your membership to this group!")); + + // Refresh the list of groups + _refreshIndicatorKey.currentState.show(); + } }