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

204 lines
6.7 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';
2020-04-26 13:25:56 +00:00
import 'package:comunic/models/user.dart';
2020-05-05 11:21:37 +00:00
import 'package:comunic/ui/routes/main_route/main_route.dart';
2019-04-27 14:23:08 +00:00
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;
2020-04-26 12:02:57 +00:00
bool _canEveryoneAddMembers = true;
2019-04-27 14:23:08 +00:00
2019-05-01 07:24:50 +00:00
get isUpdating => widget.initialSettings != null;
get isOwner => !isUpdating || widget.initialSettings.isOwner;
2020-04-26 13:25:56 +00:00
Conversation get _initialSettings => widget.initialSettings;
bool get _canAddMembers => isOwner || _initialSettings.canEveryoneAddMembers;
2019-05-01 07:24:50 +00:00
@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;
_canEveryoneAddMembers = widget.initialSettings.canEveryoneAddMembers;
2019-05-01 07:24:50 +00:00
}
}
2019-04-27 14:23:08 +00:00
@override
Widget build(BuildContext context) {
2020-04-26 13:25:56 +00:00
return SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
// Conversation name
TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: tr("Conversation name (optionnal)"),
alignLabelWithHint: true,
enabled: isOwner,
),
2020-04-26 13:25:56 +00:00
),
// Follow conversation ?
Row(
children: <Widget>[
Switch.adaptive(
value: _followConversation,
onChanged: (b) => setState(() {
_followConversation = b;
}),
),
Text(tr("Follow conversation"))
],
),
// Allow every members of the conversation to add users ?
Row(
children: <Widget>[
Switch.adaptive(
value: _canEveryoneAddMembers,
onChanged: isOwner
? (b) => setState(() {
_canEveryoneAddMembers = b;
})
: null,
),
Text(tr("Allow all members of the conversation to add users"))
],
),
// Add a member to the conversation
PickUserWidget(
resetOnChoose: true,
keepFocusOnChoose: true,
label: tr("Add member"),
enabled: _canAddMembers,
onSelectUser: (user) => setState(() {
if (!_members.contains(user)) _members.insert(0, user);
2020-04-26 13:25:56 +00:00
}),
),
//Conversation members
Column(
children: _members
.map((f) => SimpleUserTile(
user: f,
trailing: _canAddMembers
? PopupMenuButton<_MembersMenuChoices>(
captureInheritedThemes: false,
onSelected: (choice) =>
_membersMenuItemSelected(f, choice),
itemBuilder: (c) =>
<PopupMenuEntry<_MembersMenuChoices>>[
PopupMenuItem(
child: Text(tr("Remove")),
value: _MembersMenuChoices.REMOVE,
enabled: isOwner ||
(_canEveryoneAddMembers &&
!_initialSettings.members
.contains(f.id)),
)
],
)
: null,
))
.toList(),
),
// Submit button
RaisedButton(
onPressed: _members.length < 1 ? null : _submitForm,
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
2020-04-26 13:25:56 +00:00
void _membersMenuItemSelected(User user, _MembersMenuChoices choice) {
2019-04-27 14:23:08 +00:00
if (choice == null) return;
if (choice == _MembersMenuChoices.REMOVE)
2020-04-26 13:25:56 +00:00
setState(() {
_members.removeWhere((u) => u.id == user.id);
2019-04-27 14:23:08 +00:00
});
}
/// 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,
2020-04-26 12:02:57 +00:00
canEveryoneAddMembers: _canEveryoneAddMembers,
2019-05-01 07:24:50 +00:00
// 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);
2019-05-01 07:24:50 +00:00
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
2020-04-17 13:26:37 +00:00
MainController.of(context).popPage();
if (!isUpdating)
2020-04-17 13:26:37 +00:00
MainController.of(context).openConversation(conversationID);
2019-04-27 14:23:08 +00:00
}
}