1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2025-06-19 08:15:16 +00:00

Can update a conversation

This commit is contained in:
2019-05-01 09:24:50 +02:00
parent 92054d6e29
commit e7c34e6e71
7 changed files with 212 additions and 57 deletions

View File

@ -1,6 +1,6 @@
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation_settings.dart';
import 'package:comunic/models/conversation.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';
@ -14,6 +14,15 @@ import 'package:flutter/material.dart';
enum _MembersMenuChoices { REMOVE }
class UpdateConversationScreen extends StatefulWidget {
final Conversation initialSettings;
final UsersList initialUsers;
const UpdateConversationScreen({
Key key,
this.initialSettings,
this.initialUsers,
}) : super(key: key);
@override
State<StatefulWidget> createState() => _UpdateConversationScreen();
}
@ -23,6 +32,22 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
UsersList _members = UsersList();
bool _followConversation = true;
get isUpdating => widget.initialSettings != null;
get isOwner => !isUpdating || widget.initialSettings.isOwner;
@override
void initState() {
super.initState();
// Check if we are updating an existing conversation
if (widget.initialSettings != null) {
_nameController.text = widget.initialSettings.name;
_members = widget.initialUsers;
_followConversation = widget.initialSettings.following;
}
}
@override
Widget build(BuildContext context) {
return Container(
@ -33,8 +58,10 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: tr("Conversation name (optionnal)"),
alignLabelWithHint: true),
labelText: tr("Conversation name (optionnal)"),
alignLabelWithHint: true,
enabled: isOwner,
),
),
// Add a member to the conversation
@ -42,6 +69,7 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
resetOnChoose: true,
keepFocusOnChoose: true,
label: tr("Add member"),
enabled: isOwner,
onSelectUser: (user) => setState(() {
if (!_members.contains(user)) _members.add(user);
}),
@ -57,17 +85,19 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
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,
)
],
),
trailing: isOwner
? PopupMenuButton<_MembersMenuChoices>(
onSelected: (choice) =>
_membersMenuItemSelected(i, choice),
itemBuilder: (c) =>
<PopupMenuEntry<_MembersMenuChoices>>[
PopupMenuItem(
child: Text(tr("Remove")),
value: _MembersMenuChoices.REMOVE,
)
],
)
: null,
);
},
),
@ -90,7 +120,9 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
// Submit button
RaisedButton(
onPressed: _members.length < 1 ? null : _submitForm,
child: Text(tr("Create the conversation")),
child: Text(isUpdating
? tr("Update the conversation")
: tr("Create the conversation")),
)
],
),
@ -109,24 +141,40 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
/// Submit the conversation
Future<void> _submitForm() async {
final settings = ConversationSettings(
id: 0,
name: _nameController.text,
following: _followConversation,
members: _members.usersID,
);
final settings = Conversation(
id: isUpdating ? widget.initialSettings.id : 0,
name: _nameController.text,
following: _followConversation,
members: _members.usersID,
// Give random value to these fields as they are ignored here
lastActive: 0,
ownerID: 0,
sawLastMessage: true);
// Create the conversation
final conversationID = await ConversationsHelper().createConversation(settings);
var conversationID = settings.id;
bool error = false;
if (isUpdating)
error = !(await ConversationsHelper().updateConversation(settings));
else {
conversationID = await ConversationsHelper().createConversation(settings);
if (conversationID < 1) error = true;
}
// Check for errors
if(conversationID < 1)
return Scaffold.of(context).showSnackBar(
SnackBar(content: Text(tr("Could not create the conversation!")), duration: Duration(seconds: 1),));
if (error)
return Scaffold.of(context).showSnackBar(SnackBar(
content: Text(isUpdating
? tr("Could not update the conversation!")
: tr("Could not create the conversation!")),
duration: Duration(seconds: 1),
));
// Open the conversation
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (c) => ConversationRoute(conversationID: conversationID,)
));
builder: (c) => ConversationRoute(
conversationID: conversationID,
)));
}
}