1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-30 14:58:52 +00:00
comunicmobile/lib/ui/screens/update_conversation_screen.dart

203 lines
6.6 KiB
Dart
Raw Normal View History

2019-04-27 14:23:08 +00:00
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';
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;
2021-03-10 16:54:41 +00:00
get isAdmin => !isUpdating || widget.initialSettings.isAdmin;
2019-05-01 07:24:50 +00:00
2020-04-26 13:25:56 +00:00
Conversation get _initialSettings => widget.initialSettings;
2021-03-10 16:54:41 +00:00
bool get _canAddMembers => isAdmin || _initialSettings.canEveryoneAddMembers;
2020-04-26 13:25:56 +00:00
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,
2021-03-10 16:54:41 +00:00
enabled: isAdmin,
),
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,
2021-03-10 16:54:41 +00:00
onChanged: isAdmin
2020-04-26 13:25:56 +00:00
? (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,
2021-03-10 16:54:41 +00:00
enabled: isAdmin ||
2020-04-26 13:25:56 +00:00
(_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 {
2021-03-10 16:54:41 +00:00
// TODO : reimplement
/* final settings = Conversation(
2019-05-01 07:24:50 +00:00
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)
2021-03-10 16:54:41 +00:00
MainController.of(context).openConversation(conversationID);*/
2019-04-27 14:23:08 +00:00
}
}