public async void GetMegaContacts() { // User must be online to perform this operation if (!await IsUserOnlineAsync()) { return; } // First cancel any other loading task that is busy CancelLoad(); // Create the option to cancel CreateLoadCancelOption(); await OnUiThreadAsync(() => this.ItemCollection.Clear()); MUserList contactsList = this.MegaSdk.getContacts(); await Task.Factory.StartNew(() => { try { for (int i = 0; i < contactsList.size(); i++) { // If the task has been cancelled, stop processing if (LoadingCancelToken.IsCancellationRequested) { LoadingCancelToken.ThrowIfCancellationRequested(); } // To avoid null values if (contactsList.get(i) == null) { continue; } if (contactsList.get(i).getVisibility() != MUserVisibility.VISIBILITY_VISIBLE) { continue; } var megaContact = new ContactViewModel(contactsList.get(i), this); OnUiThread(() => this.ItemCollection.Items.Add(megaContact)); megaContact.GetContactFirstname(); megaContact.GetContactLastname(); megaContact.GetContactAvatarColor(); megaContact.GetContactAvatar(); } } catch (OperationCanceledException) { // Do nothing. Just exit this background process because a cancellation exception has been thrown } }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current); this.SortBy(this.CurrentOrder, this.ItemCollection.CurrentOrderDirection); }
protected void OnContactUpdated(object sender, MUser user) { var existingContact = (ContactViewModel)this.ItemCollection.Items.FirstOrDefault( contact => contact.Handle.Equals(user.getHandle())); // If the contact exists in the contact list if (existingContact != null) { //If the contact is no longer a contact(REMOVE CONTACT SCENARIO) if (!existingContact.Visibility.Equals(user.getVisibility()) && !(user.getVisibility().Equals(MUserVisibility.VISIBILITY_VISIBLE))) { OnUiThread(() => this.ItemCollection.Items.Remove(existingContact)); } // If the contact has been changed (UPDATE CONTACT SCENARIO) and is not an own change else if (!Convert.ToBoolean(user.isOwnChange())) { if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_AVATAR) && !string.IsNullOrWhiteSpace(existingContact.AvatarPath)) { existingContact.GetContactAvatar(); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_EMAIL)) { OnUiThread(() => existingContact.Email = user.getEmail()); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_FIRSTNAME)) { existingContact.GetContactFirstname(); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_LASTNAME)) { existingContact.GetContactLastname(); } } } // If is a new contact (ADD CONTACT SCENARIO - REQUEST ACCEPTED) else if (user.getVisibility().Equals(MUserVisibility.VISIBILITY_VISIBLE)) { var megaContact = new ContactViewModel(user, this); OnUiThread(() => this.ItemCollection.Items.Add(megaContact)); megaContact.GetContactFirstname(); megaContact.GetContactLastname(); megaContact.GetContactAvatarColor(); megaContact.GetContactAvatar(); } }