1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-30 00:36:28 +00:00
comunicmobile/lib/ui/screens/groups_list_screen.dart

146 lines
4.0 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-05-05 11:21:37 +00:00
import 'package:comunic/ui/routes/main_route/main_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()
2020-05-02 16:15:55 +00:00
: Stack(children: [_buildGroupsList(), _buildCreateButton()]),
2020-04-15 10:04:19 +00:00
))
],
);
}
2020-05-02 16:15:55 +00:00
Widget _buildGroupsList() => ListView(
children: _groups.values
.map((g) => ListTile(
leading: GroupIcon(group: g),
title: Text(g.displayName),
subtitle: GroupMembershipWidget(
group: g,
onUpdated: () => _refreshIndicatorKey.currentState.show(),
),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () => _deleteGroup(g)),
onTap: () => MainController.of(context).openGroup(g.id),
))
.toList(),
);
Widget _buildCreateButton() => Positioned(
right: 15,
bottom: 15,
child: FloatingActionButton(
child: Icon(Icons.add),
onPressed: _createGroup,
),
);
2020-04-15 10:04:19 +00:00
/// 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-05-02 16:15:55 +00:00
/// Add a group
void _createGroup() async {
try {
final name = await askUserString(
context: context,
title: tr("Group name"),
message: tr("Name of the group to create"),
defaultValue: "",
hint: tr("Name of the group"),
maxLength: 50,
);
if (name == null) return;
final groupID = await GroupsHelper.create(name);
MainController.of(context).openGroup(groupID);
} catch (e, s) {
print("Could not create a new group! $e\n$s");
showSimpleSnack(context, tr("Could not create a new group!"));
}
}
2020-04-15 10:04:19 +00:00
}