/// <summary> /// Call this method ONLY and ONLY if you have got Long Poll History already. /// </summary> /// <param name="id"></param> /// <param name="isConference"></param> public void LoadMoreMessages(int id, bool isConference) { if (_currentChatViewModel == null) return; IsLoadingMessagesHistory = true; var currentMessages = _messagesCache.GetItems(id); // Only and only if any messages already presents, we may load more elder data (since we think, that this data is latest and greatest). // Otherwise we can get unsynchronized data with VK. int number = _currentChatViewModel.Messages.Count; if (number < currentMessages.Count) { try { // Where from to start take. int start = currentMessages.Count - number; // how much take. int finish = start - MESSAGES_COUNT; if (finish < 0) finish = 0; for (int i = start - 1; i >= finish; i--) { _currentChatViewModel.AddOldMessage(currentMessages[i]); } _currentChatViewModel.UpdateStatusInfo(); IsLoadingMessagesHistory = false; } catch (Exception ex) { Debug.WriteLine("_UpdateCurrentMessagesList failed: " + ex.Message); } } else //currentMessages.Any()) { if (isConference) { // load chat messages MessagesGet getMessages = new MessagesGet(-1, id, currentMessages.Count, MESSAGES_COUNT, _UpdateCurrentMessagesList); getMessages.Execute(); } else { // load messages of user MessagesGet getMessages = new MessagesGet(id, -1, currentMessages.Count, MESSAGES_COUNT, _UpdateCurrentMessagesList); getMessages.Execute(); } } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="isConference"></param> /// <returns></returns> public ChatViewModel GetChatViewModel(int id, bool isConference) { _currentChatId = id; _isCurrentConference = isConference; try { var currentMessages = _messagesCache.GetItems(id); string title = EntitiesHelpers.GetChatTitle(id, isConference); // Load only 30 first from cache. int finish = currentMessages.Count - MESSAGES_COUNT; if (finish < 0) finish = 0; int count = MESSAGES_COUNT; if (currentMessages.Count < count) count = currentMessages.Count; var filtered = new Message[count];// new List<Message>(MESSAGES_COUNT); for (int i = currentMessages.Count - 1, j = count - 1; i >= finish; i--, j--) filtered[j] = currentMessages[i]; _currentChatViewModel = new ChatViewModel(id, title, isConference, filtered, _imageCache); if (!currentMessages.Any()) // Get messages only first time for this chat. { MessagesGet getMessages = null; if (isConference) getMessages = new MessagesGet(-1, id, 0, MESSAGES_COUNT, _UpdateCurrentMessagesList); else getMessages = new MessagesGet(id, -1, 0, MESSAGES_COUNT, _UpdateCurrentMessagesList); getMessages.Execute(); } else _UpdateReadStatusOfMessages(currentMessages); // This method will be called in _UpdateCurrentMessagesList _currentChatViewModel.UpdateStatusInfo(); } catch (Exception ex) { Debug.WriteLine("GetChatViewModel failed: " + ex.Message); } return _currentChatViewModel; }