Exemplo n.º 1
0
        public async void CreateChatCommandExecute(object obj)
        {
            if (this.isCreating)
            {
                return;
            }

            this.isCreating = true;

            if (Users == null)
            {
                return;
            }

            try
            {
                this.IsBusyIndicatorVisible = true;
                var selectedUsers = Users.Where(u => u.IsSelected).Select(u => u.User).ToList();
                if (selectedUsers.Any())
                {
                    string     dialogName = null;
                    DialogType dialogType = DialogType.Group;
                    if (selectedUsers.Count == 1)
                    {
                        dialogType = DialogType.Private;
                        dialogName = selectedUsers.First().FullName;
                    }
                    else
                    {
                        var promptResult = await
                                           UserDialogs.Instance.PromptAsync("Enter chat name:", null, "Create", "Cancel",
                                                                            "Enter chat name", InputType.Name);

                        if (promptResult.Ok)
                        {
                            dialogName = promptResult.Text;

                            if (string.IsNullOrWhiteSpace(dialogName))
                            {
                                dialogName = App.UserName + "_" +
                                             string.Join(", ", selectedUsers.Select(u => u.FullName));
                            }
                        }
                        else
                        {
                            this.IsBusyIndicatorVisible = false;
                            this.isCreating             = false;
                            return;
                        }
                    }

                    var userIds       = selectedUsers.Select(u => u.Id).ToList();
                    var userIdsString = string.Join(",", userIds);

                    Dialog dialog = null;
                    if (dialogType == DialogType.Group)
                    {
                        dialog = await App.QbProvider.CreateDialogAsync(dialogName.Trim(), userIdsString, dialogType);

                        if (dialog != null)
                        {
                            SaveDialogToDb(dialog);

                            groupManager = App.QbProvider.GetXmppClient()
                                           .GetGroupChatManager(dialog.XmppRoomJid, dialog.Id);
                            groupManager.MessageReceived += OnMessageReceived;
                            groupManager.JoinGroup(App.QbProvider.UserId.ToString());
                            await Task.Delay(1000);

                            groupManager.NotifyAboutGroupCreation(userIds, dialog);
                            return;
                        }
                    }
                    else if (dialogType == DialogType.Private)
                    {
                        dialog = await App.QbProvider.GetDialogAsync(new int[] { App.QbProvider.UserId, userIds.First() });

                        if (dialog == null)
                        {
                            dialog = await App.QbProvider.CreateDialogAsync(dialogName, userIdsString, dialogType);

                            var privateManager =
                                App.QbProvider.GetXmppClient()
                                .GetPrivateChatManager(selectedUsers.First().Id, dialog.Id);
                            var message = "Hello, I created chat with you!";
                            privateManager.SendMessage(message);
                            dialog.LastMessage = message;
                        }

                        SaveDialogToDb(dialog);
                        var privateChantPage = new PrivateChatPage(dialog.Id);
                        App.Navigation.InsertPageBefore(privateChantPage,
                                                        (App.Current.MainPage as NavigationPage).CurrentPage);
                        App.Navigation.PopAsync();
                    }
                }
                else
                {
                    App.Current.MainPage.DisplayAlert("Error", "Please, select any of users", "Ok");
                }

                this.IsBusyIndicatorVisible = false;
            }
            catch (Exception ex)
            {
            }

            this.isCreating = false;
        }
Exemplo n.º 2
0
 protected override void OnElementChanged(VisualElementChangedEventArgs e)
 {
     base.OnElementChanged(e);
     page = (PrivateChatPage)e.NewElement;
 }