1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Can create a group

This commit is contained in:
Pierre HUBERT 2020-05-02 18:15:55 +02:00
parent a48e7f57a6
commit d4a39a3527
3 changed files with 79 additions and 35 deletions

View File

@ -126,6 +126,17 @@ class GroupsHelper {
.map((f) => cast<int>(f))
.toSet();
/// Create a new group
///
/// Throws in case of failure
static Future<int> create(String name) async {
final result = await APIRequest.withLogin("groups/create")
.addString("name", name)
.execWithThrow();
return result.getObject()["id"];
}
/// Perform a simple membership request
Future<bool> _simpleMembershipRequest(int groupID, String uri,
{Map<String, String> args}) async =>

View File

@ -54,15 +54,20 @@ class _GroupsListScreenState extends SafeState<GroupsListScreen> {
onRefresh: () => this._refreshList(),
child: _groups == null
? Container()
: ListView(
: Stack(children: [_buildGroupsList(), _buildCreateButton()]),
))
],
);
}
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(),
onUpdated: () => _refreshIndicatorKey.currentState.show(),
),
trailing: IconButton(
icon: Icon(Icons.delete),
@ -70,11 +75,16 @@ class _GroupsListScreenState extends SafeState<GroupsListScreen> {
onTap: () => MainController.of(context).openGroup(g.id),
))
.toList(),
),
))
],
);
}
Widget _buildCreateButton() => Positioned(
right: 15,
bottom: 15,
child: FloatingActionButton(
child: Icon(Icons.add),
onPressed: _createGroup,
),
);
/// Refresh the list of groups
Future<void> _refreshList() async {
@ -109,4 +119,27 @@ class _GroupsListScreenState extends SafeState<GroupsListScreen> {
// Refresh the list of groups
_refreshIndicatorKey.currentState.show();
}
/// 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!"));
}
}
}

View File

@ -1,5 +1,6 @@
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/ui/routes/full_screen_image.dart';
import 'package:comunic/ui/widgets/dialogs/auto_sized_dialog_content_widget.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
import 'package:html/parser.dart';
@ -86,12 +87,14 @@ Future<String> askUserString({
@required String message,
@required String defaultValue,
@required String hint,
int maxLength = 200,
}) async {
assert(context != null);
assert(title != null);
assert(message != null);
assert(defaultValue != null);
assert(hint != null);
assert(maxLength != null);
TextEditingController controller = TextEditingController(text: defaultValue);
@ -99,16 +102,14 @@ Future<String> askUserString({
context: context,
builder: (c) => AlertDialog(
title: Text(title),
content: Container(
height: MediaQuery.of(context).size.height - 50,
child: SingleChildScrollView(
content: AutoSizeDialogContentWidget(
child: Column(
children: <Widget>[
Text(message),
TextField(
controller: controller,
maxLines: null,
maxLength: 200,
maxLength: maxLength,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: hint,
@ -118,7 +119,6 @@ Future<String> askUserString({
],
),
),
),
actions: <Widget>[
FlatButton(
child: Text(tr("Cancel").toUpperCase()),