async Task LoadToCollection(IEnumerable <Friend> friends) { await FriendsSemaphore.WaitAsync().ConfigureAwait(false); using (Friends.UpdatesBlock()) { Friends.Clear(); Friends.AddRange(friends); } FriendsSemaphore.Release(); }
public async Task ExecuteSearchForFriendCommand(string search) { search.Trim(); if (IsBusy) { return; } LoadingMessage = "Finding Friend..."; using (BusyContext()) { using (App.Logger.TrackTimeContext("FriendSearch")) { try { App.Logger.Track("FriendSearch"); if (!search.IsValidEmail()) { App.MessageDialog.SendToast("Please enter a valide email address."); return; } if (!await RefreshToken()) { return; } var userManager = new UserManager(Settings.AccessToken); var result = await userManager.GetUserViaEmail(search); IsBusy = false; //did not find anyone //add them and send invite if (result == null || result.Devices == null) { var newFriend = new Friend { FriendId = Settings.GenerateTempFriendId(), Name = search }; await DataManager.AddOrSaveFriendAsync(newFriend); await FriendsSemaphore.WaitAsync(); Friends.Add(newFriend); FriendsSemaphore.Release(); await userManager.SendUserInvite(search); RaiseNotification("Your friend hasn't signed up for Inner 6 Chat yet. We have sent them an invite!", "Friend Request Sent"); App.Logger.Track("FriendRequestSent"); return; } //did you enter yourself? if (!string.IsNullOrWhiteSpace(Settings.Email) && result.Email.ToLowerInvariant() == Settings.Email.ToLowerInvariant()) { RaiseNotification("This is you!"); return; } //did you already friend them? await FriendsSemaphore.WaitAsync(); var alreadyFriend = Friends.FirstOrDefault(f => f.FriendId == result.Id) != null; FriendsSemaphore.Release(); if (alreadyFriend) { RaiseNotification("Friend already added."); return; } //new friend found and we want to add them. var msg = string.Format("We found {0} do you want to add to friend list?", result.NickName); App.MessageDialog.SendConfirmation(msg, "Friend found!", async add => { if (!add) { return; } var newFriend = new Friend { FriendId = result.Id, Name = result.NickName, Photo = EndPoints.BaseUrl + result.Avatar.Location, AvatarType = result.Avatar.Type }; await DataManager.AddOrSaveFriendAsync(newFriend); await FriendsSemaphore.WaitAsync(); Friends.Add(newFriend); FriendsSemaphore.Release(); RaiseNotification("Friend added!"); App.Logger.Track("FriendAdded"); }); } catch (Exception ex) { App.Logger.Report(ex); RaiseError("Something has gone wrong, please try to search again."); } } } }