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

133 lines
4.2 KiB
Dart

import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation_settings.dart';
import 'package:comunic/ui/routes/conversation_route.dart';
import 'package:comunic/ui/tiles/simple_user_tile.dart';
import 'package:comunic/ui/widgets/pick_user_widget.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
/// Create / Update conversation screen
///
/// @author Pierre HUBERT
enum _MembersMenuChoices { REMOVE }
class UpdateConversationScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => _UpdateConversationScreen();
}
class _UpdateConversationScreen extends State<UpdateConversationScreen> {
TextEditingController _nameController = TextEditingController();
UsersList _members = UsersList();
bool _followConversation = true;
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
// Conversation name
TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: tr("Conversation name (optionnal)"),
alignLabelWithHint: true),
),
// Add a member to the conversation
PickUserWidget(
resetOnChoose: true,
keepFocusOnChoose: true,
label: tr("Add member"),
onSelectUser: (user) => setState(() {
if (!_members.contains(user)) _members.add(user);
}),
),
//Conversation members
Container(
child: _members.length == 0
? null
: Flexible(
child: ListView.builder(
itemCount: _members.length,
itemBuilder: (b, i) {
return SimpleUserTile(
user: _members[i],
trailing: PopupMenuButton<_MembersMenuChoices>(
onSelected: (choice) =>
_membersMenuItemSelected(i, choice),
itemBuilder: (c) =>
<PopupMenuEntry<_MembersMenuChoices>>[
PopupMenuItem(
child: Text(tr("Remove")),
value: _MembersMenuChoices.REMOVE,
)
],
),
);
},
),
),
),
// Follow conversation ?
Row(
children: <Widget>[
Switch(
value: _followConversation,
onChanged: (b) => setState(() {
_followConversation = b;
}),
),
Text(tr("Follow conversation"))
],
),
// Submit button
RaisedButton(
onPressed: _members.length < 1 ? null : _submitForm,
child: Text(tr("Create the conversation")),
)
],
),
);
}
/// An option of the members menu has been selected
void _membersMenuItemSelected(int index, _MembersMenuChoices choice) {
if (choice == null) return;
if (choice == _MembersMenuChoices.REMOVE)
return setState(() {
_members.removeAt(index);
});
}
/// Submit the conversation
Future<void> _submitForm() async {
final settings = ConversationSettings(
id: 0,
name: _nameController.text,
following: _followConversation,
members: _members.usersID,
);
// Create the conversation
final conversationID = await ConversationsHelper().createConversation(settings);
// Check for errors
if(conversationID < 1)
return Scaffold.of(context).showSnackBar(
SnackBar(content: Text(tr("Could not create the conversation!")), duration: Duration(seconds: 1),));
// Open the conversation
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (c) => ConversationRoute(conversationID: conversationID,)
));
}
}