public async void onUsersUpdate(MegaSDK api, MUserList users) { // Exit methods when users list is incorrect if (users == null || users.size() < 1) { return; } // Retrieve the listsize for performance reasons and store local int listSize = users.size(); for (int i = 0; i < listSize; i++) { MUser user = users.get(i); if (user == null) { continue; } // If the change is on the current user if (user.getHandle().Equals(api.getMyUser().getHandle()) && !Convert.ToBoolean(user.isOwnChange())) { if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_AVATAR) && !string.IsNullOrWhiteSpace(AccountService.UserData.AvatarPath)) { AccountService.GetUserAvatar(); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_EMAIL)) { await AccountService.GetUserEmail(); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_FIRSTNAME)) { AccountService.GetUserFirstname(); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_LASTNAME)) { AccountService.GetUserLastname(); } } else // If the change is on a contact { OnContactUpdated(user); } } }
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); }
public void onUsersUpdate(MegaSDK api, MUserList users) { if (users == null || users.size() < 1) { return; } for (int i = 0; i < users.size(); i++) { MUser user = users.get(i); if (user == null) { continue; } // If the change is on the current user if (user.getHandle().Equals(api.getMyUser().getHandle()) && !Convert.ToBoolean(user.isOwnChange())) { if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_AVATAR) && !String.IsNullOrWhiteSpace(AccountService.AccountDetails.AvatarPath)) { api.getUserAvatar(user, AccountService.AccountDetails.AvatarPath, new GetUserAvatarRequestListener()); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_EMAIL)) { Deployment.Current.Dispatcher.BeginInvoke(() => AccountService.AccountDetails.UserEmail = user.getEmail()); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_FIRSTNAME)) { api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_FIRSTNAME, new GetUserDataRequestListener()); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_LASTNAME)) { api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_LASTNAME, new GetUserDataRequestListener()); } } else // If the change is on a contact { // If there are any ContactsViewModel active foreach (var contactViewModel in Contacts) { Contact existingContact = contactViewModel.MegaContactsList.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))) { Deployment.Current.Dispatcher.BeginInvoke(() => contactViewModel.MegaContactsList.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)) { api.getUserAvatar(user, existingContact.AvatarPath, new GetContactAvatarRequestListener(existingContact)); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_EMAIL)) { Deployment.Current.Dispatcher.BeginInvoke(() => existingContact.Email = user.getEmail()); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_FIRSTNAME)) { api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_FIRSTNAME, new GetContactDataRequestListener(existingContact)); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_LASTNAME)) { api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_LASTNAME, new GetContactDataRequestListener(existingContact)); } } } // If is a new contact (ADD CONTACT SCENARIO - REQUEST ACCEPTED) else if (user.getVisibility().Equals(MUserVisibility.VISIBILITY_VISIBLE)) { var _megaContact = new Contact() { Handle = user.getHandle(), Email = user.getEmail(), Timestamp = user.getTimestamp(), Visibility = user.getVisibility(), AvatarColor = UiService.GetColorFromHex(SdkService.MegaSdk.getUserAvatarColor(user)) }; Deployment.Current.Dispatcher.BeginInvoke(() => contactViewModel.MegaContactsList.Add(_megaContact)); api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_FIRSTNAME, new GetContactDataRequestListener(_megaContact)); api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_LASTNAME, new GetContactDataRequestListener(_megaContact)); api.getUserAvatar(user, _megaContact.AvatarPath, new GetContactAvatarRequestListener(_megaContact)); } } // If there are any ContactDetailsViewModel active foreach (var contactDetailsViewModel in ContactsDetails) { // If the selected contact has been changed (UPDATE CONTACT SCENARIO) if (contactDetailsViewModel.SelectedContact.Handle.Equals(user.getHandle())) { if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_AVATAR) && !String.IsNullOrWhiteSpace(contactDetailsViewModel.SelectedContact.AvatarPath)) { api.getUserAvatar(user, contactDetailsViewModel.SelectedContact.AvatarPath, new GetContactAvatarRequestListener(contactDetailsViewModel.SelectedContact)); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_EMAIL)) { Deployment.Current.Dispatcher.BeginInvoke(() => contactDetailsViewModel.SelectedContact.Email = user.getEmail()); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_FIRSTNAME)) { api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_FIRSTNAME, new GetContactDataRequestListener(contactDetailsViewModel.SelectedContact)); } if (user.hasChanged((int)MUserChangeType.CHANGE_TYPE_LASTNAME)) { api.getUserAttribute(user, (int)MUserAttrType.USER_ATTR_LASTNAME, new GetContactDataRequestListener(contactDetailsViewModel.SelectedContact)); } } } } } }
public void GetMegaContacts() { // User must be online to perform this operation if (!IsUserOnline()) { return; } // First cancel any other loading task that is busy CancelLoad(); // Create the option to cancel CreateLoadCancelOption(); SetEmptyContentTemplate(true); OnUiThread(() => MegaContactsList.Clear()); MUserList contactsList = this.MegaSdk.getContacts(); Task.Factory.StartNew(() => { try { Deployment.Current.Dispatcher.BeginInvoke(() => { 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)) { var _megaContact = new Contact() { Handle = contactsList.get(i).getHandle(), Email = contactsList.get(i).getEmail(), Timestamp = contactsList.get(i).getTimestamp(), Visibility = contactsList.get(i).getVisibility(), AvatarColor = UiService.GetColorFromHex(SdkService.MegaSdk.getUserAvatarColor(contactsList.get(i))) }; MegaContactsList.Add(_megaContact); MegaSdk.getUserAttribute(contactsList.get(i), (int)MUserAttrType.USER_ATTR_FIRSTNAME, new GetContactDataRequestListener(_megaContact)); MegaSdk.getUserAttribute(contactsList.get(i), (int)MUserAttrType.USER_ATTR_LASTNAME, new GetContactDataRequestListener(_megaContact)); MegaSdk.getUserAvatar(contactsList.get(i), _megaContact.AvatarPath, new GetContactAvatarRequestListener(_megaContact)); } } SetEmptyContentTemplate(false); }); } catch (OperationCanceledException) { // Do nothing. Just exit this background process because a cancellation exception has been thrown } }, LoadingCancelToken, TaskCreationOptions.PreferFairness, TaskScheduler.Current); }