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)) .map((f) => cast<int>(f))
.toSet(); .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 /// Perform a simple membership request
Future<bool> _simpleMembershipRequest(int groupID, String uri, Future<bool> _simpleMembershipRequest(int groupID, String uri,
{Map<String, String> args}) async => {Map<String, String> args}) async =>

View File

@ -54,15 +54,20 @@ class _GroupsListScreenState extends SafeState<GroupsListScreen> {
onRefresh: () => this._refreshList(), onRefresh: () => this._refreshList(),
child: _groups == null child: _groups == null
? Container() ? Container()
: ListView( : Stack(children: [_buildGroupsList(), _buildCreateButton()]),
))
],
);
}
Widget _buildGroupsList() => ListView(
children: _groups.values children: _groups.values
.map((g) => ListTile( .map((g) => ListTile(
leading: GroupIcon(group: g), leading: GroupIcon(group: g),
title: Text(g.displayName), title: Text(g.displayName),
subtitle: GroupMembershipWidget( subtitle: GroupMembershipWidget(
group: g, group: g,
onUpdated: () => onUpdated: () => _refreshIndicatorKey.currentState.show(),
_refreshIndicatorKey.currentState.show(),
), ),
trailing: IconButton( trailing: IconButton(
icon: Icon(Icons.delete), icon: Icon(Icons.delete),
@ -70,11 +75,16 @@ class _GroupsListScreenState extends SafeState<GroupsListScreen> {
onTap: () => MainController.of(context).openGroup(g.id), onTap: () => MainController.of(context).openGroup(g.id),
)) ))
.toList(), .toList(),
),
))
],
); );
}
Widget _buildCreateButton() => Positioned(
right: 15,
bottom: 15,
child: FloatingActionButton(
child: Icon(Icons.add),
onPressed: _createGroup,
),
);
/// Refresh the list of groups /// Refresh the list of groups
Future<void> _refreshList() async { Future<void> _refreshList() async {
@ -109,4 +119,27 @@ class _GroupsListScreenState extends SafeState<GroupsListScreen> {
// Refresh the list of groups // Refresh the list of groups
_refreshIndicatorKey.currentState.show(); _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/helpers/preferences_helper.dart';
import 'package:comunic/ui/routes/full_screen_image.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:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:html/parser.dart'; import 'package:html/parser.dart';
@ -86,12 +87,14 @@ Future<String> askUserString({
@required String message, @required String message,
@required String defaultValue, @required String defaultValue,
@required String hint, @required String hint,
int maxLength = 200,
}) async { }) async {
assert(context != null); assert(context != null);
assert(title != null); assert(title != null);
assert(message != null); assert(message != null);
assert(defaultValue != null); assert(defaultValue != null);
assert(hint != null); assert(hint != null);
assert(maxLength != null);
TextEditingController controller = TextEditingController(text: defaultValue); TextEditingController controller = TextEditingController(text: defaultValue);
@ -99,16 +102,14 @@ Future<String> askUserString({
context: context, context: context,
builder: (c) => AlertDialog( builder: (c) => AlertDialog(
title: Text(title), title: Text(title),
content: Container( content: AutoSizeDialogContentWidget(
height: MediaQuery.of(context).size.height - 50,
child: SingleChildScrollView(
child: Column( child: Column(
children: <Widget>[ children: <Widget>[
Text(message), Text(message),
TextField( TextField(
controller: controller, controller: controller,
maxLines: null, maxLines: null,
maxLength: 200, maxLength: maxLength,
keyboardType: TextInputType.text, keyboardType: TextInputType.text,
decoration: InputDecoration( decoration: InputDecoration(
labelText: hint, labelText: hint,
@ -118,7 +119,6 @@ Future<String> askUserString({
], ],
), ),
), ),
),
actions: <Widget>[ actions: <Widget>[
FlatButton( FlatButton(
child: Text(tr("Cancel").toUpperCase()), child: Text(tr("Cancel").toUpperCase()),