mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 12:14:11 +00:00 
			
		
		
		
	Can create conversations
This commit is contained in:
		@@ -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!"));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user