/// <summary> /// Augment the existing update with a new state /// </summary> /// <param name="newUpdate">The update to absorb</param> /// <param name="silent">Do not notify externally of changes</param> /// <param name="local_id">The ID associated with the local user</param> public void absorb(Updates newUpdate, Boolean silent, string local_id) { // Given an update, augment the existing one // Case 1 - isMatch() and hasMessages() - BACKLOG // Case 2 - hasMessages() - New Message Existing Constact // Case 3 - isMatch() - NEW CONSTACT foreach (Match m in newUpdate.matches) { if (m.isMatch()) { if (m.hasMessages()) { // Case 1 - Backlog Messages foreach (Message msg in m.messages) { msg.getImageOfGiphy(); } } // Add constact to state (with messages) matches.Add(m); if (!silent) { NewMatchToast.Do(m); } } // Case 2 - New messages for exisiting user // We must find the "existing" user. else if (m.hasMessages()) { for (int idx = 0; idx < matches.Count; idx++) { Match existing = matches[idx]; if (existing._id == m._id) { foreach (Message message in m.messages) { // Asynchronous message.getImageOfGiphy(); // Add only if the last message isn't the same // We already add messages we send instantly //if (existing.messages.Last()._id != message._id) //{ // existing.messages.Add(message); //} existing.messages.Add(message); } // If it is an incoming message, do a toast if (m.messages.Last().from != local_id) { NewMessageToast.Do(existing, m.messages.Last()); } // Propagate the changes up in the list if its not already at the top //if (idx != 0) // matches.Move(idx, 0); } } } } }
/// <summary> /// Augment the existing update with a new state /// </summary> /// <param name="newUpdate">The update to absorb</param> /// <param name="silent">Do not notify externally of changes</param> /// <param name="local_id">The ID associated with the local user</param> public void absorb(Updates newUpdate, Boolean silent, string local_id) { // Given an update, augment the existing one // Case 1 - isMatch() and hasMessages() - BACKLOG // Case 2 - hasMessages() - New Message Existing Constact // Case 3 - isMatch() - NEW CONSTACT foreach (Match m in newUpdate.matches) { if (m.isMatch()) { if (m.hasMessages()) { // Case 1 - Backlog Messages foreach (Message msg in m.messages) msg.getImageOfGiphy(); } // Add constact to state (with messages) matches.Add(m); if (!silent) { NewMatchToast.Do(m); } } // Case 2 - New messages for exisiting user // We must find the "existing" user. else if (m.hasMessages()) { for (int idx = 0; idx < matches.Count; idx++) { Match existing = matches[idx]; if (existing._id == m._id) { foreach (Message message in m.messages) { // Asynchronous message.getImageOfGiphy(); // Add only if the last message isn't the same // We already add messages we send instantly //if (existing.messages.Last()._id != message._id) //{ // existing.messages.Add(message); //} existing.messages.Add(message); } // If it is an incoming message, do a toast if (m.messages.Last().from != local_id) NewMessageToast.Do(existing, m.messages.Last()); // Propagate the changes up in the list if its not already at the top //if (idx != 0) // matches.Move(idx, 0); } } } } }
// Is this the right way to do this? public async void startUpdatesLoop() { if (Updates != null) return; // Set the initial state, without propagating the updates Updates = new Updates(); Updates.absorb(await getLatestUpdates(), true, Me._id); if (!looping) { looping = true; while (true) { // Every 3 seconds await Task.Delay(3000); if (looping) { Updates newUpdate = await getLatestUpdates(); // Merge matches from both Updates // New messages are intersperced in here Updates.absorb(newUpdate, false, Me._id); } } } }