1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 21:09:21 +00:00

Can create conversations

This commit is contained in:
Pierre HUBERT 2021-03-13 10:32:11 +01:00
parent f54cc22fc6
commit ba60fa9e37
7 changed files with 91 additions and 124 deletions

View File

@ -17,6 +17,7 @@ import 'package:comunic/models/new_conversation_message.dart';
import 'package:comunic/models/new_conversation_settings.dart'; import 'package:comunic/models/new_conversation_settings.dart';
import 'package:comunic/models/unread_conversation.dart'; import 'package:comunic/models/unread_conversation.dart';
import 'package:comunic/utils/account_utils.dart'; import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/color_utils.dart';
import 'package:comunic/utils/dart_color.dart'; import 'package:comunic/utils/dart_color.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
@ -32,17 +33,18 @@ class ConversationsHelper {
/// Create a new conversation /// Create a new conversation
/// ///
/// Return the ID of the newly created conversation or -1 in case of failure /// Return the ID of the newly created conversation
Future<int> createConversation(NewConversation settings) async { ///
final response = /// Throws in case of failure
await APIRequest(uri: "conversations/create", needLogin: true, args: { static Future<int> createConversation(NewConversation settings) async {
final response = await APIRequest.withLogin("conversations/create", args: {
"name": settings.name ?? "", "name": settings.name ?? "",
"follow": settings.follow ? "true" : "false", "follow": settings.follow ? "true" : "false",
"users": settings.members.join(","), "users": settings.members.join(","),
"color": settings.color ?? "," "color": colorToHex(settings.color) ?? ","
}).addBool("canEveryoneAddMembers", settings.canEveryoneAddMembers).exec(); })
.addBool("canEveryoneAddMembers", settings.canEveryoneAddMembers)
if (response.code != 200) return -1; .execWithThrow();
return response.getObject()["conversationID"]; return response.getObject()["conversationID"];
} }

View File

@ -56,6 +56,10 @@ class Conversation extends SerializableElement<Conversation> {
/// Get the list of members in the conversation /// Get the list of members in the conversation
Set<int> get membersID => members.map((e) => e.userID).toSet(); Set<int> get membersID => members.map((e) => e.userID).toSet();
/// Get the list of admins in the conversation
Set<int> get adminsID =>
members.where((e) => e.isAdmin).map((e) => e.userID).toSet();
/// Get the list of the OTHER members of the conversation (all except current user) /// Get the list of the OTHER members of the conversation (all except current user)
Set<int> get otherMembersID => membersID..remove(userID()); Set<int> get otherMembersID => membersID..remove(userID());

View File

@ -9,7 +9,7 @@ class NewConversation {
final List<int> members; final List<int> members;
final bool follow; final bool follow;
final bool canEveryoneAddMembers; final bool canEveryoneAddMembers;
final String color; final Color color;
const NewConversation({ const NewConversation({
@required this.name, @required this.name,

View File

@ -1,11 +1,4 @@
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation.dart';
import 'package:comunic/ui/screens/update_conversation_screen.dart'; import 'package:comunic/ui/screens/update_conversation_screen.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// Update a conversation route /// Update a conversation route
@ -24,69 +17,7 @@ class UpdateConversationRoute extends StatefulWidget {
} }
class _UpdateConversationRoute extends State<UpdateConversationRoute> { class _UpdateConversationRoute extends State<UpdateConversationRoute> {
Conversation _conversation;
UsersList _membersInfo;
bool _error = false;
@override @override
void didChangeDependencies() { Widget build(BuildContext context) =>
super.didChangeDependencies(); UpdateConversationScreen(convID: widget.conversationID);
_loadConversation();
}
void setError(bool e) => setState(() {
_error = e;
});
/// Load information about the being updated conversation
Future<void> _loadConversation() async {
setError(false);
try {
final conversation = await ConversationsHelper()
.getSingle(widget.conversationID, force: true);
//Load information about the members of the conversation
_membersInfo = await UsersHelper().getList(conversation.membersID);
setState(() => _conversation = conversation);
} catch (e, s) {
print("Failed to load conversation information! $e=>$s");
setError(true);
}
}
/// Build the body of this widget
Widget _buildBody() {
if (_error)
return buildErrorCard(
tr("Could not load information about the conversation"),
actions: [
FlatButton(
onPressed: _loadConversation,
child: Text(
tr("Retry").toUpperCase(),
style: TextStyle(color: Colors.white),
),
)
]);
if (_conversation == null) return buildLoadingPage();
return UpdateConversationScreen(
initialUsers: _membersInfo,
initialSettings: _conversation,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: ComunicBackButton(),
title: Text(tr("Update a conversation")),
),
body: _buildBody(),
);
}
} }

View File

@ -1,6 +1,4 @@
import 'package:comunic/ui/screens/update_conversation_screen.dart'; import 'package:comunic/ui/screens/update_conversation_screen.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// Create a new conversation route /// Create a new conversation route
@ -9,13 +7,5 @@ import 'package:flutter/material.dart';
class CreateConversationScreen extends StatelessWidget { class CreateConversationScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => UpdateConversationScreen();
return Scaffold(
appBar: AppBar(
leading: ComunicBackButton(),
title: Text(tr("Create a conversation")),
),
body: UpdateConversationScreen(),
);
}
} }

View File

@ -1,13 +1,20 @@
import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/users_list.dart'; import 'package:comunic/lists/users_list.dart';
import 'package:comunic/models/conversation.dart'; import 'package:comunic/models/conversation.dart';
import 'package:comunic/models/new_conversation.dart';
import 'package:comunic/models/user.dart'; import 'package:comunic/models/user.dart';
import 'package:comunic/ui/dialogs/color_picker_dialog.dart'; import 'package:comunic/ui/dialogs/color_picker_dialog.dart';
import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/tiles/simple_user_tile.dart'; import 'package:comunic/ui/tiles/simple_user_tile.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
import 'package:comunic/ui/widgets/pick_user_widget.dart'; import 'package:comunic/ui/widgets/pick_user_widget.dart';
import 'package:comunic/utils/color_utils.dart'; import 'package:comunic/utils/color_utils.dart';
import 'package:comunic/utils/dart_color.dart'; import 'package:comunic/utils/dart_color.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/log_utils.dart'; import 'package:comunic/utils/log_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// Create / Update conversation screen /// Create / Update conversation screen
@ -17,13 +24,11 @@ import 'package:flutter/material.dart';
enum _MembersMenuChoices { REMOVE } enum _MembersMenuChoices { REMOVE }
class UpdateConversationScreen extends StatefulWidget { class UpdateConversationScreen extends StatefulWidget {
final Conversation initialSettings; final convID;
final UsersList initialUsers;
const UpdateConversationScreen({ const UpdateConversationScreen({
Key key, Key key,
this.initialSettings, this.convID,
this.initialUsers,
}) : super(key: key); }) : super(key: key);
@override @override
@ -31,9 +36,12 @@ class UpdateConversationScreen extends StatefulWidget {
} }
class _UpdateConversationScreen extends State<UpdateConversationScreen> { class _UpdateConversationScreen extends State<UpdateConversationScreen> {
Conversation _conversation;
TextEditingController _nameController = TextEditingController(); TextEditingController _nameController = TextEditingController();
TextEditingController _colorController = TextEditingController(); TextEditingController _colorController = TextEditingController();
UsersList _members = UsersList(); UsersList _members = UsersList();
Set<int> _admins = Set();
bool _followConversation = true; bool _followConversation = true;
bool _canEveryoneAddMembers = true; bool _canEveryoneAddMembers = true;
@ -50,29 +58,49 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
} }
} }
get isUpdating => widget.initialSettings != null; get isUpdating => widget.convID != null;
get isAdmin => !isUpdating || widget.initialSettings.isAdmin; get isAdmin => !isUpdating || _conversation.isAdmin;
Conversation get _initialSettings => widget.initialSettings; bool get _canAddMembers => isAdmin || _conversation.canEveryoneAddMembers;
bool get _canAddMembers => isAdmin || _initialSettings.canEveryoneAddMembers; get _isValid => _members.length > 0;
@override Future<void> _init() async {
void initState() { if (!isUpdating) return;
super.initState(); _conversation =
await ConversationsHelper().getSingle(widget.convID, force: true);
// Check if we are updating an existing conversation _nameController.text = _conversation.name ?? "";
if (widget.initialSettings != null) { _colorController.text =
_nameController.text = widget.initialSettings.name; _conversationColor == null ? "" : "#${_conversation.color}";
_members = widget.initialUsers; _members = await UsersHelper().getList(_conversation.membersID);
_followConversation = widget.initialSettings.following; _admins = _conversation.adminsID;
_canEveryoneAddMembers = widget.initialSettings.canEveryoneAddMembers; _followConversation = _conversation.following;
} _canEveryoneAddMembers = _conversation.canEveryoneAddMembers;
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
leading: ComunicBackButton(),
title: Text(isUpdating
? tr("Update conversation")
: tr("Create a conversation")),
actions: [
IconButton(
icon: Icon(Icons.check),
onPressed: _isValid ? _submitForm : null)
],
),
body: AsyncScreenWidget(
onReload: _init,
onBuild: _buildBody,
errorMessage: tr("Failed to load conversation settings!"),
),
);
Widget _buildBody() {
return SingleChildScrollView( return SingleChildScrollView(
child: Container( child: Container(
padding: EdgeInsets.all(8.0), padding: EdgeInsets.all(8.0),
@ -159,7 +187,7 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
value: _MembersMenuChoices.REMOVE, value: _MembersMenuChoices.REMOVE,
enabled: isAdmin || enabled: isAdmin ||
(_canEveryoneAddMembers && (_canEveryoneAddMembers &&
!_initialSettings.members !_conversation.membersID
.contains(f.id)), .contains(f.id)),
) )
], ],
@ -168,14 +196,6 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
)) ))
.toList(), .toList(),
), ),
// Submit button
RaisedButton(
onPressed: _members.length < 1 ? null : _submitForm,
child: Text(isUpdating
? tr("Update the conversation")
: tr("Create the conversation")),
)
], ],
), ),
), ),
@ -200,6 +220,21 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
/// Submit the conversation /// Submit the conversation
Future<void> _submitForm() async { Future<void> _submitForm() async {
try {
// Create the conversation
if (!isUpdating) {
final conversationID = await ConversationsHelper.createConversation(
NewConversation(
name: _nameController.text,
members: _members.map((element) => element.id).toList(),
follow: _followConversation,
canEveryoneAddMembers: _canEveryoneAddMembers,
color: _color));
MainController.of(context).popPage();
MainController.of(context).openConversation(conversationID);
}
// TODO : reimplement // TODO : reimplement
/* final settings = Conversation( /* final settings = Conversation(
id: isUpdating ? widget.initialSettings.id : 0, id: isUpdating ? widget.initialSettings.id : 0,
@ -237,5 +272,9 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
MainController.of(context).popPage(); MainController.of(context).popPage();
if (!isUpdating) if (!isUpdating)
MainController.of(context).openConversation(conversationID);*/ MainController.of(context).openConversation(conversationID);*/
} catch (e, s) {
logError(e, s);
snack(context, tr("Failed to update conversation settings!"));
}
} }
} }

View File

@ -7,7 +7,8 @@ import 'dart:ui';
String colorToHex(Color color) { String colorToHex(Color color) {
if (color == null) return ""; if (color == null) return "";
return color.red.toRadixString(16).padLeft(2, '0') + return (color.red.toRadixString(16).padLeft(2, '0') +
color.green.toRadixString(16).padLeft(2, '0') + color.green.toRadixString(16).padLeft(2, '0') +
color.blue.toRadixString(16).padLeft(2, '0'); color.blue.toRadixString(16).padLeft(2, '0'))
.toUpperCase();
} }