1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 16:26:27 +00:00
comunicmobile/lib/ui/screens/groups_list_screen.dart

113 lines
3.2 KiB
Dart
Raw Normal View History

2020-04-15 10:04:19 +00:00
import 'package:comunic/helpers/groups_helper.dart';
import 'package:comunic/lists/groups_list.dart';
2020-04-15 11:25:55 +00:00
import 'package:comunic/models/group.dart';
2020-04-15 16:06:20 +00:00
import 'package:comunic/ui/routes/home_route.dart';
2020-04-15 10:04:19 +00:00
import 'package:comunic/ui/widgets/group_icon_widget.dart';
2020-04-15 11:56:59 +00:00
import 'package:comunic/ui/widgets/group_membership_widget.dart';
2020-04-15 10:04:19 +00:00
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<GroupsListScreen> {
GroupsList _groups;
bool _error = false;
2020-04-15 11:25:55 +00:00
final _refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();
2020-04-15 10:04:19 +00:00
@override
void initState() {
super.initState();
this._refreshList();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
// Error
buildErrorCard(
tr("Could not load the list of groups!"),
hide: !_error,
actions: [
MaterialButton(
2020-04-15 16:06:20 +00:00
child: Text(tr("Try again").toUpperCase()),
2020-04-15 10:04:19 +00:00
onPressed: () => _refreshList(),
),
],
),
// List of groups
Expanded(
child: RefreshIndicator(
2020-04-15 11:25:55 +00:00
key: _refreshIndicatorKey,
2020-04-15 10:04:19 +00:00
onRefresh: () => this._refreshList(),
child: _groups == null
? Container()
: ListView(
children: _groups.values
.map((g) => ListTile(
leading: GroupIcon(group: g),
title: Text(g.displayName),
2020-04-15 12:34:48 +00:00
subtitle: GroupMembershipWidget(
group: g,
onUpdated: () =>
_refreshIndicatorKey.currentState.show(),
),
2020-04-15 11:25:55 +00:00
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () => _deleteGroup(g)),
2020-04-16 10:06:01 +00:00
onTap: () => HomeController.of(context).openGroup(g.id),
2020-04-15 10:04:19 +00:00
))
.toList(),
),
))
],
);
}
/// Refresh the list of groups
Future<void> _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;
});
}
}
2020-04-15 11:25:55 +00:00
/// 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();
}
2020-04-15 10:04:19 +00:00
}