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/group_membership_widget.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'; /// Groups list screen /// /// @author Pierre Hubert class GroupsListScreen extends StatefulWidget { @override _GroupsListScreenState createState() => _GroupsListScreenState(); } class _GroupsListScreenState extends SafeState { GroupsList _groups; bool _error = false; final _refreshIndicatorKey = GlobalKey(); @override void initState() { super.initState(); this._refreshList(); } @override Widget build(BuildContext context) { return Column( children: [ // Error buildErrorCard( tr("Could not load the list of groups!"), hide: !_error, actions: [ MaterialButton( child: Text(tr("Try again".toUpperCase())), onPressed: () => _refreshList(), ), ], ), // List of groups Expanded( child: RefreshIndicator( key: _refreshIndicatorKey, onRefresh: () => this._refreshList(), child: _groups == null ? Container() : ListView( children: _groups.values .map((g) => ListTile( leading: GroupIcon(group: g), title: Text(g.displayName), subtitle: GroupMembershipWidget(group: g), trailing: IconButton( icon: Icon(Icons.delete), onPressed: () => _deleteGroup(g)), )) .toList(), ), )) ], ); } /// Refresh the list of groups Future _refreshList() async { try { final list = await GroupsHelper().getListUser(); final groups = await GroupsHelper().getListOrThrow(list, force: true); setState(() { _groups = groups; _error = false; }); } catch (e) { print(e); setState(() { _error = true; }); } } /// 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(); } }