public override async void OnAppearing()
        {
            base.OnAppearing();

            this.IsBusyIndicatorVisible = true;

            var dialog = Database.Instance().GetDialog(dialogId);
            groupChatManager = App.QbProvider.GetXmppClient().GetGroupChatManager(dialog.XmppRoomJid, dialogId);
            groupChatManager.MessageReceived += OnMessageReceived;
            DialogName = dialog.Name;
            ImageSource = Device.OnPlatform(iOS: ImageSource.FromFile("groupholder.png"),
                Android: ImageSource.FromFile("groupholder.png"),
                WinPhone: ImageSource.FromFile("groupholder.png"));

            if (!string.IsNullOrEmpty(dialog.Photo))
            {
                ImageSource = ImageSource.FromUri(new Uri(dialog.Photo));
            }

            //Task.Factory.StartNew(async () =>
            //{
                await this.LoadMessages();

                Device.BeginInvokeOnMainThread(() =>
                    this.IsBusyIndicatorVisible = false
                );
            //});

        }
示例#2
0
        public override async void OnAppearing()
        {
            base.OnAppearing();

            this.IsBusyIndicatorVisible = true;

            var dialog = Database.Instance().GetDialog(dialogId);

            groupChatManager = App.QbProvider.GetXmppClient().GetGroupChatManager(dialog.XmppRoomJid, dialogId);
            groupChatManager.MessageReceived += OnMessageReceived;
            DialogName  = dialog.Name;
            ImageSource = Device.OnPlatform(iOS: ImageSource.FromFile("groupholder.png"),
                                            Android: ImageSource.FromFile("groupholder.png"),
                                            WinPhone: ImageSource.FromFile("groupholder.png"));

            if (!string.IsNullOrEmpty(dialog.Photo))
            {
                ImageSource = ImageSource.FromUri(new Uri(dialog.Photo));
            }

            //Task.Factory.StartNew(async () =>
            //{
            await this.LoadMessages();

            Device.BeginInvokeOnMainThread(() =>
                                           this.IsBusyIndicatorVisible = false
                                           );
            //});
        }
        public GroupChatViewModel(string dialogId) : base (dialogId)
        {
            this.OpenChatInfoCommand = new RelayCommand(this.OpenChatInfoCommandExecute);
            base.SendMessageCommand = new RelayCommand(this.SendMessageCommandExecute);

            if (string.IsNullOrEmpty(dialogId))
                return;

            var dialog = Database.Instance().GetDialog(dialogId);
            if (dialog == null)
                return;

            DialogName = dialog.Name;

            groupChatManager = App.QbProvider.GetXmppClient().GetGroupChatManager(dialog.XmppRoomJid, dialogId);
            groupChatManager.MessageReceived += OnMessageReceived;
        }
示例#4
0
        public GroupChatViewModel(string dialogId) : base(dialogId)
        {
            this.OpenChatInfoCommand = new RelayCommand(this.OpenChatInfoCommandExecute);
            base.SendMessageCommand  = new RelayCommand(this.SendMessageCommandExecute);

            if (string.IsNullOrEmpty(dialogId))
            {
                return;
            }

            var dialog = Database.Instance().GetDialog(dialogId);

            if (dialog == null)
            {
                return;
            }

            DialogName = dialog.Name;

            groupChatManager = App.QbProvider.GetXmppClient().GetGroupChatManager(dialog.XmppRoomJid, dialogId);
            groupChatManager.MessageReceived += OnMessageReceived;
        }
		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;
		}
示例#6
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;
        }