public async override void OnNavigatedTo(NavigationEventArgs e)
 {
     var dialogId = e.Parameter as string;
     if (!string.IsNullOrEmpty(dialogId))
     {
         var dialogsManager = ServiceLocator.Locator.Get<IDialogsManager>();
         dialog = dialogsManager.Dialogs.FirstOrDefault(d => d.Id == dialogId);
         if (dialog != null)
         {
             var currentUserId = SettingsManager.Instance.ReadFromSettings<int>(SettingsKeys.CurrentUserId);
             int otherUserId = dialog.OccupantIds.FirstOrDefault(id => id != currentUserId);
             var cachingQbClient = ServiceLocator.Locator.Get<ICachingQuickbloxClient>();
             var user = await cachingQbClient.GetUserById(otherUserId);
             if (user != null)
             {
                 UserName = user.FullName;
                 MobilePhone = user.Phone;
                 if (user.BlobId.HasValue)
                 {
                     var imageService = ServiceLocator.Locator.Get<IImageService>();
                     UserImage = await imageService.GetPrivateImage(user.BlobId.Value, 100);
                 }
             }
         }
     }
 }
        public async override void OnNavigatedTo(NavigationEventArgs e)
        {
            var dialog = e.Parameter as DialogViewModel;
            if (dialog == null) return;

            currentDialog = dialog;
            await Initialize(dialog);
        }
        public async override void OnNavigatedTo(NavigationEventArgs e)
        {
            var dialogId = e.Parameter as string;
            if (dialogId == null) return;

            var dialogsManager = ServiceLocator.Locator.Get<IDialogsManager>();
            currentDialog = dialogsManager.Dialogs.FirstOrDefault(d => d.Id == dialogId);
            if(currentDialog != null)
                await Initialize(currentDialog);
        }
        private async Task Initialize(ChatNavigationParameter chatParameter)
        {
            IsLoading = true;

            currentUserId = SettingsManager.Instance.ReadFromSettings<int>(SettingsKeys.CurrentUserId);

            if (chatParameter.Dialog != null)
            {
                dialog = chatParameter.Dialog;
                ChatName = chatParameter.Dialog.Name;
                ChatImage = chatParameter.Dialog.Image;

                int otherUserId = dialog.OccupantIds.FirstOrDefault(id => id != currentUserId);
                await QmunicateLoggerHolder.Log(QmunicateLogLevel.Debug, string.Format("Initializing Chat page. CurrentUserId: {0}. OtherUserId: {1}.", currentUserId, otherUserId));

                if (!string.IsNullOrEmpty(chatParameter.Dialog.Id))
                    await MessageCollectionViewModel.LoadMessages(chatParameter.Dialog.Id);

                if (otherUserId != 0)
                {
                    privateChatManager = QuickbloxClient.ChatXmppClient.GetPrivateChatManager(otherUserId, chatParameter.Dialog.Id);
                    privateChatManager.OnMessageReceived += ChatManagerOnOnMessageReceived;
                    privateChatManager.OnIsTyping += PrivateChatManagerOnOnIsTyping;
                    privateChatManager.OnPausedTyping += PrivateChatManagerOnOnPausedTyping;
                }


                CheckIsMessageSendingAllowed();
            }

            IsLoading = false;
        }
        private async Task Initialize(string dialogId)
        {
            IsLoading = true;

            var dialogManager = ServiceLocator.Locator.Get<IDialogsManager>();
            dialog = dialogManager.Dialogs.FirstOrDefault(d => d.Id == dialogId);

            if (dialog == null) return;

            currentUserId = SettingsManager.Instance.ReadFromSettings<int>(SettingsKeys.CurrentUserId);

            ChatName = dialog.Name;
            ChatImage = dialog.Image;
            NumberOfMembers = dialog.OccupantIds.Count;

            await QmunicateLoggerHolder.Log(QmunicateLogLevel.Debug, string.Format("Initializing GroupChat page. CurrentUserId: {0}. Group JID: {1}.", currentUserId, dialog.XmppRoomJid));

            await MessageCollectionViewModel.LoadMessages(dialogId);

            groupChatManager = QuickbloxClient.ChatXmppClient.GetGroupChatManager(dialog.XmppRoomJid, dialog.Id);
            groupChatManager.MessageReceived += ChatManagerOnOnMessageReceived;
            groupChatManager.JoinGroup(currentUserId.ToString());

            IsLoading = false;
        }
        private async Task Initialize(DialogViewModel dialog)
        {
            IsLoading = true;
            ChatName = dialog.Name;
            ChatImage = dialog.Image;

            var cachingQbClient = ServiceLocator.Locator.Get<ICachingQuickbloxClient>();
            var imagesService = ServiceLocator.Locator.Get<IImageService>();

            foreach (int occupantId in dialog.OccupantIds)
            {
                var user = await cachingQbClient.GetUserById(occupantId);
                if (user != null)
                    Participants.Add(UserViewModel.FromUser(user));
            }

            foreach (var userViewModel in Participants)
            {
                userViewModel.IsOnline = QuickbloxClient.ChatXmppClient.Presences.Any(p => p.UserId == userViewModel.UserId && (p.PresenceType == PresenceType.None || p.PresenceType == PresenceType.Subscribed));
                var user = await cachingQbClient.GetUserById(userViewModel.UserId);
                if (user?.LastRequestAt != null)
                {
                    userViewModel.LastActive = user.LastRequestAt.Value;
                }
            }

            foreach (UserViewModel userVm in Participants)
            {
                if (userVm.ImageUploadId.HasValue)
                {
                    userVm.Image = await imagesService.GetPrivateImage(userVm.ImageUploadId.Value, 100);
                }
            }

            IsLoading = false;
        }
        private async Task Initialize(ChatNavigationParameter chatParameter)
        {
            IsLoading = true;

            currentUserId = SettingsManager.Instance.ReadFromSettings<int>(SettingsKeys.CurrentUserId);

            if (chatParameter.Dialog != null)
            {
                dialog = chatParameter.Dialog;
                ChatName = chatParameter.Dialog.Name;
                ChatImage = chatParameter.Dialog.Image;

                otherUserId = dialog.OccupantIds.FirstOrDefault(id => id != currentUserId);

                await QmunicateLoggerHolder.Log(QmunicateLogLevel.Debug, string.Format("Initializing Chat page. CurrentUserId: {0}. OtherUserId: {1}.", currentUserId, otherUserId));

                if (!string.IsNullOrEmpty(chatParameter.Dialog.Id))
                    await MessageCollectionViewModel.LoadMessages(chatParameter.Dialog.Id);

                if (otherUserId != 0)
                {
                    privateChatManager = QuickbloxClient.ChatXmppClient.GetPrivateChatManager(otherUserId, chatParameter.Dialog.Id);
                    privateChatManager.MessageReceived += ChatManagerOnOnMessageReceived;
                    privateChatManager.OpponentStartedTyping += PrivateChatManagerOnOpponentStartedTyping;
                    privateChatManager.OpponentPausedTyping += PrivateChatManagerOpponentOpponentPausedTyping;
                }
                
                QuickbloxClient.ChatXmppClient.PresenceReceived += ChatXmppClientOnPresenceReceived;
                IsOnline = QuickbloxClient.ChatXmppClient.Presences.Any(p => p.UserId == otherUserId && (p.PresenceType == PresenceType.None || p.PresenceType == PresenceType.Subscribed));
                var otherUser = await ServiceLocator.Locator.Get<ICachingQuickbloxClient>().GetUserById(otherUserId);
                if (otherUser?.LastRequestAt != null)
                    LastActive = otherUser.LastRequestAt.Value;

                CheckIsMessageSendingAllowed();
            }

            IsLoading = false;
        }
 private async Task Initialize(DialogViewModel dialog)
 {
     ChatName = dialog.Name;
     ChatImage = dialog.Image;
 }
        private async Task InitializeAllContacts(DialogViewModel existingDialog)
        {
            allContacts = new List<SelectableListBoxItem<UserViewModel>>();

            foreach (Contact contact in QuickbloxClient.ChatXmppClient.Contacts)
            {
                if(existingDialog != null && existingDialog.OccupantIds.Contains(contact.UserId)) continue;

                var userVm = UserViewModel.FromContact(contact);
                allContacts.Add(new SelectableListBoxItem<UserViewModel>(userVm));
            }

            await LoadContactsNamesAndImages();
        }
        public override async void OnNavigatedTo(NavigationEventArgs e)
        {
            var dialog = e.Parameter as DialogViewModel;
            if (dialog != null)
            {
                IsEditMode = true;
                editedDialog = dialog;
            }

            IsLoading = true;
            await InitializeAllContacts(editedDialog);
            await Search(null);
            IsLoading = false;
        }