mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-12-26 12:58:51 +00:00
Can create conversations
This commit is contained in:
parent
f54cc22fc6
commit
ba60fa9e37
@ -17,6 +17,7 @@ import 'package:comunic/models/new_conversation_message.dart';
|
||||
import 'package:comunic/models/new_conversation_settings.dart';
|
||||
import 'package:comunic/models/unread_conversation.dart';
|
||||
import 'package:comunic/utils/account_utils.dart';
|
||||
import 'package:comunic/utils/color_utils.dart';
|
||||
import 'package:comunic/utils/dart_color.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
@ -32,17 +33,18 @@ class ConversationsHelper {
|
||||
|
||||
/// Create a new conversation
|
||||
///
|
||||
/// Return the ID of the newly created conversation or -1 in case of failure
|
||||
Future<int> createConversation(NewConversation settings) async {
|
||||
final response =
|
||||
await APIRequest(uri: "conversations/create", needLogin: true, args: {
|
||||
/// Return the ID of the newly created conversation
|
||||
///
|
||||
/// Throws in case of failure
|
||||
static Future<int> createConversation(NewConversation settings) async {
|
||||
final response = await APIRequest.withLogin("conversations/create", args: {
|
||||
"name": settings.name ?? "",
|
||||
"follow": settings.follow ? "true" : "false",
|
||||
"users": settings.members.join(","),
|
||||
"color": settings.color ?? ","
|
||||
}).addBool("canEveryoneAddMembers", settings.canEveryoneAddMembers).exec();
|
||||
|
||||
if (response.code != 200) return -1;
|
||||
"color": colorToHex(settings.color) ?? ","
|
||||
})
|
||||
.addBool("canEveryoneAddMembers", settings.canEveryoneAddMembers)
|
||||
.execWithThrow();
|
||||
|
||||
return response.getObject()["conversationID"];
|
||||
}
|
||||
|
@ -56,6 +56,10 @@ class Conversation extends SerializableElement<Conversation> {
|
||||
/// Get the list of members in the conversation
|
||||
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)
|
||||
Set<int> get otherMembersID => membersID..remove(userID());
|
||||
|
||||
|
@ -9,7 +9,7 @@ class NewConversation {
|
||||
final List<int> members;
|
||||
final bool follow;
|
||||
final bool canEveryoneAddMembers;
|
||||
final String color;
|
||||
final Color color;
|
||||
|
||||
const NewConversation({
|
||||
@required this.name,
|
||||
|
@ -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/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';
|
||||
|
||||
/// Update a conversation route
|
||||
@ -24,69 +17,7 @@ class UpdateConversationRoute extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _UpdateConversationRoute extends State<UpdateConversationRoute> {
|
||||
Conversation _conversation;
|
||||
UsersList _membersInfo;
|
||||
bool _error = false;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_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(),
|
||||
);
|
||||
}
|
||||
Widget build(BuildContext context) =>
|
||||
UpdateConversationScreen(convID: widget.conversationID);
|
||||
}
|
||||
|
@ -1,6 +1,4 @@
|
||||
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';
|
||||
|
||||
/// Create a new conversation route
|
||||
@ -9,13 +7,5 @@ import 'package:flutter/material.dart';
|
||||
|
||||
class CreateConversationScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: ComunicBackButton(),
|
||||
title: Text(tr("Create a conversation")),
|
||||
),
|
||||
body: UpdateConversationScreen(),
|
||||
);
|
||||
}
|
||||
Widget build(BuildContext context) => UpdateConversationScreen();
|
||||
}
|
||||
|
@ -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/models/conversation.dart';
|
||||
import 'package:comunic/models/new_conversation.dart';
|
||||
import 'package:comunic/models/user.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/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/utils/color_utils.dart';
|
||||
import 'package:comunic/utils/dart_color.dart';
|
||||
import 'package:comunic/utils/intl_utils.dart';
|
||||
import 'package:comunic/utils/log_utils.dart';
|
||||
import 'package:comunic/utils/ui_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Create / Update conversation screen
|
||||
@ -17,13 +24,11 @@ import 'package:flutter/material.dart';
|
||||
enum _MembersMenuChoices { REMOVE }
|
||||
|
||||
class UpdateConversationScreen extends StatefulWidget {
|
||||
final Conversation initialSettings;
|
||||
final UsersList initialUsers;
|
||||
final convID;
|
||||
|
||||
const UpdateConversationScreen({
|
||||
Key key,
|
||||
this.initialSettings,
|
||||
this.initialUsers,
|
||||
this.convID,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
@ -31,9 +36,12 @@ class UpdateConversationScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _UpdateConversationScreen extends State<UpdateConversationScreen> {
|
||||
Conversation _conversation;
|
||||
|
||||
TextEditingController _nameController = TextEditingController();
|
||||
TextEditingController _colorController = TextEditingController();
|
||||
UsersList _members = UsersList();
|
||||
Set<int> _admins = Set();
|
||||
bool _followConversation = 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
|
||||
void initState() {
|
||||
super.initState();
|
||||
Future<void> _init() async {
|
||||
if (!isUpdating) return;
|
||||
_conversation =
|
||||
await ConversationsHelper().getSingle(widget.convID, force: true);
|
||||
|
||||
// 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;
|
||||
}
|
||||
_nameController.text = _conversation.name ?? "";
|
||||
_colorController.text =
|
||||
_conversationColor == null ? "" : "#${_conversation.color}";
|
||||
_members = await UsersHelper().getList(_conversation.membersID);
|
||||
_admins = _conversation.adminsID;
|
||||
_followConversation = _conversation.following;
|
||||
_canEveryoneAddMembers = _conversation.canEveryoneAddMembers;
|
||||
}
|
||||
|
||||
@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(
|
||||
child: Container(
|
||||
padding: EdgeInsets.all(8.0),
|
||||
@ -159,7 +187,7 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
|
||||
value: _MembersMenuChoices.REMOVE,
|
||||
enabled: isAdmin ||
|
||||
(_canEveryoneAddMembers &&
|
||||
!_initialSettings.members
|
||||
!_conversation.membersID
|
||||
.contains(f.id)),
|
||||
)
|
||||
],
|
||||
@ -168,14 +196,6 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
|
||||
))
|
||||
.toList(),
|
||||
),
|
||||
|
||||
// Submit button
|
||||
RaisedButton(
|
||||
onPressed: _members.length < 1 ? null : _submitForm,
|
||||
child: Text(isUpdating
|
||||
? tr("Update the conversation")
|
||||
: tr("Create the conversation")),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
@ -200,8 +220,23 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
|
||||
|
||||
/// Submit the conversation
|
||||
Future<void> _submitForm() async {
|
||||
// TODO : reimplement
|
||||
/* final settings = Conversation(
|
||||
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
|
||||
/* final settings = Conversation(
|
||||
id: isUpdating ? widget.initialSettings.id : 0,
|
||||
ownerID: isUpdating ? widget.initialSettings.ownerID : 0,
|
||||
name: _nameController.text,
|
||||
@ -237,5 +272,9 @@ class _UpdateConversationScreen extends State<UpdateConversationScreen> {
|
||||
MainController.of(context).popPage();
|
||||
if (!isUpdating)
|
||||
MainController.of(context).openConversation(conversationID);*/
|
||||
} catch (e, s) {
|
||||
logError(e, s);
|
||||
snack(context, tr("Failed to update conversation settings!"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,8 @@ import 'dart:ui';
|
||||
String colorToHex(Color color) {
|
||||
if (color == null) return "";
|
||||
|
||||
return color.red.toRadixString(16).padLeft(2, '0') +
|
||||
color.green.toRadixString(16).padLeft(2, '0') +
|
||||
color.blue.toRadixString(16).padLeft(2, '0');
|
||||
return (color.red.toRadixString(16).padLeft(2, '0') +
|
||||
color.green.toRadixString(16).padLeft(2, '0') +
|
||||
color.blue.toRadixString(16).padLeft(2, '0'))
|
||||
.toUpperCase();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user