1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-29 06:18:51 +00:00
comunicmobile/lib/ui/screens/update_conversation_screen.dart

181 lines
5.6 KiB
Dart
Raw Normal View History

2019-04-27 14:23:08 +00:00
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/lists/users_list.dart';
2019-05-01 07:24:50 +00:00
import 'package:comunic/models/conversation.dart';
2019-04-27 14:23:08 +00:00
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 {
2019-05-01 07:24:50 +00:00
final Conversation initialSettings;
final UsersList initialUsers;
const UpdateConversationScreen({
Key key,
this.initialSettings,
this.initialUsers,
}) : super(key: key);
2019-04-27 14:23:08 +00:00
@override
State<StatefulWidget> createState() => _UpdateConversationScreen();
}
class _UpdateConversationScreen extends State<UpdateConversationScreen> {
TextEditingController _nameController = TextEditingController();
UsersList _members = UsersList();
bool _followConversation = true;
2019-05-01 07:24:50 +00:00
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;
}
}
2019-04-27 14:23:08 +00:00
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
// Conversation name
TextField(
controller: _nameController,
decoration: InputDecoration(
2019-05-01 07:24:50 +00:00
labelText: tr("Conversation name (optionnal)"),
alignLabelWithHint: true,
enabled: isOwner,
),
2019-04-27 14:23:08 +00:00
),
// Add a member to the conversation
PickUserWidget(
resetOnChoose: true,
keepFocusOnChoose: true,
label: tr("Add member"),
2019-05-01 07:24:50 +00:00
enabled: isOwner,
2019-04-27 14:23:08 +00:00
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],
2019-05-01 07:24:50 +00:00
trailing: isOwner
? PopupMenuButton<_MembersMenuChoices>(
onSelected: (choice) =>
_membersMenuItemSelected(i, choice),
itemBuilder: (c) =>
<PopupMenuEntry<_MembersMenuChoices>>[
PopupMenuItem(
child: Text(tr("Remove")),
value: _MembersMenuChoices.REMOVE,
)
],
)
: null,
2019-04-27 14:23:08 +00:00
);
},
),
),
),
// 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,
2019-05-01 07:24:50 +00:00
child: Text(isUpdating
? tr("Update the conversation")
: tr("Create the conversation")),
2019-04-27 14:23:08 +00:00
)
],
),
);
}
/// 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 {
2019-05-01 07:24:50 +00:00
final settings = Conversation(
id: isUpdating ? widget.initialSettings.id : 0,
2019-05-01 09:30:55 +00:00
ownerID: isUpdating ? widget.initialSettings.ownerID : 0,
2019-05-01 07:24:50 +00:00
name: _nameController.text,
following: _followConversation,
members: _members.usersID,
// Give random value to these fields as they are ignored here
lastActive: 0,
sawLastMessage: true);
2019-04-27 14:23:08 +00:00
// Create the conversation
2019-05-01 07:24:50 +00:00
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;
}
2019-04-27 14:23:08 +00:00
// Check for errors
2019-05-01 07:24:50 +00:00
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),
));
2019-04-27 14:23:08 +00:00
// Open the conversation
Navigator.of(context).pushReplacement(MaterialPageRoute(
2019-05-01 07:24:50 +00:00
builder: (c) => ConversationRoute(
conversationID: conversationID,
)));
2019-04-27 14:23:08 +00:00
}
}