1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00
comunicmobile/lib/ui/screens/groups_list_screen.dart

173 lines
5.1 KiB
Dart
Raw Normal View History

2022-03-18 19:13:45 +00:00
import 'package:comunic/enums/report_target_type.dart';
2020-04-15 10:04:19 +00:00
import 'package:comunic/helpers/groups_helper.dart';
2022-03-18 19:13:45 +00:00
import 'package:comunic/helpers/server_config_helper.dart';
2020-04-15 10:04:19 +00:00
import 'package:comunic/lists/groups_list.dart';
2020-04-15 11:25:55 +00:00
import 'package:comunic/models/group.dart';
2022-03-18 19:13:45 +00:00
import 'package:comunic/models/report_target.dart';
import 'package:comunic/ui/dialogs/report_dialog.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;
2020-04-15 10:04:19 +00:00
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(
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.toList()
2021-04-22 13:15:40 +00:00
..sort((one, two) => two.id.compareTo(one.id)))
2020-05-02 16:15:55 +00:00
.map((g) => ListTile(
leading: GroupIcon(group: g),
title: Text(g.displayName),
subtitle: GroupMembershipWidget(
group: g,
onUpdated: () => _refreshIndicatorKey.currentState!.show(),
2020-05-02 16:15:55 +00:00
),
2022-03-18 19:13:45 +00:00
trailing: IntrinsicWidth(
child: Row(
children: [
// Remove membership
IconButton(
icon: Icon(Icons.delete),
onPressed: () => _deleteGroup(g)),
// Report button
srvConfig!.isReportingEnabled && !g.isAtLeastMember
2022-03-18 19:13:45 +00:00
? IconButton(
onPressed: () => _reportGroup(g),
icon: Icon(
Icons.flag,
),
)
: Container()
],
),
),
onTap: () => MainController.of(context)!.openGroup(g.id),
2020-05-02 16:15:55 +00:00
))
.toList(),
);
Widget _buildCreateButton() => Positioned(
right: 15,
bottom: 15,
child: FloatingActionButton(
2020-05-13 16:38:57 +00:00
heroTag: null,
2020-05-02 16:15:55 +00:00
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!")!);
2020-04-15 11:25:55 +00:00
// Refresh the list of groups
_refreshIndicatorKey.currentState!.show();
2020-04-15 11:25:55 +00:00
}
2020-05-02 16:15:55 +00:00
2022-03-18 19:13:45 +00:00
/// Report a group
void _reportGroup(Group g) => showReportDialog(
ctx: context, target: ReportTarget(ReportTargetType.Group, g.id));
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")!,
2020-05-02 16:15:55 +00:00
defaultValue: "",
hint: tr("Name of the group")!,
2020-05-02 16:15:55 +00:00
maxLength: 50,
);
if (name == null) return;
final groupID = await GroupsHelper.create(name);
MainController.of(context)!.openGroup(groupID);
2020-05-02 16:15:55 +00:00
} catch (e, s) {
print("Could not create a new group! $e\n$s");
showSimpleSnack(context, tr("Could not create a new group!")!);
2020-05-02 16:15:55 +00:00
}
}
2020-04-15 10:04:19 +00:00
}