/// <summary> /// Because the app writes contact bindings the agent will be invoked on a per need basis /// (e.g.: the user opens a contact an swipes to the "Connect" pivot). On invocation, the agent is /// requested to write the ConnectTile information. /// </summary> /// <param name="operation">The operation that we need to perform</param> private async Task ProcessOperationAsync(DownloadRichConnectDataOperation operation) { Logger.Log("Agent", "DownloadRichConnectDataOperation Ids=(" + string.Join(",", operation.Ids) + ")"); try { await Helpers.ParallelForEach(operation.Ids, async (string remoteId) => { Logger.Log("Agent", "Start sync for id = " + remoteId); ContactBinding binding = await contactBindingManager.GetContactBindingByRemoteIdAsync(remoteId); ServerApi.TileData tileData = await ServerApi.GetTileDataFromWebServiceAsync(remoteId); binding.TileData = new ConnectTileData(); binding.TileData.Title = tileData.Title; foreach (IRandomAccessStream stream in tileData.Images) { ConnectTileImage image = new ConnectTileImage(); await image.SetImageAsync(stream); binding.TileData.Images.Add(image); } await contactBindingManager.SaveContactBindingAsync(binding); Logger.Log("Agent", "Finish sync for id = " + remoteId); }); } catch (Exception e) { Helpers.HandleException(e); } finally { Logger.Log("Agent", "DownloadRichConnectDataOperation Ids=(" + string.Join(",", operation.Ids) + ") - completed"); operation.SafeNotifyCompletion(); } }
private async Task CreateContactBindingsAsync() { ContactBindingManager bindingManager = await ContactBindings.GetAppContactBindingManagerAsync(); // Simulate call to web service List <ServerApi.ContactBinding> bindings = ServerApi.GetContactsFromWebServiceAsync(); foreach (ServerApi.ContactBinding binding in bindings) { ContactBinding myBinding = bindingManager.CreateContactBinding(binding.RemoteId); // This information is not displayed on the Contact Page in the People Hub app, but // is used to automatically link the contact binding with existent phone contacts. // Add as much information as possible for the ContactBinding to increase the // chances to find a matching Contact on the phone. myBinding.FirstName = binding.GivenName; myBinding.LastName = binding.FamilyName; myBinding.EmailAddress1 = binding.Email; myBinding.Name = binding.CodeName; // Don't crash if one binding fails, log the error and continue saving try { await bindingManager.SaveContactBindingAsync(myBinding); } catch (Exception e) { Logger.Log("MainPage", "Binding (" + binding.RemoteId + ") failed to save. " + e.Message); } } }
public async void CreateContactBindingsAsync() { ContactBindingManager bindingManager = await ContactBindings.GetAppContactBindingManagerAsync(); // Simulate call to web service TraktProfile[] profiles = await userDao.getUserFriends(); ContactStore store = await ContactStore.CreateOrOpenAsync(); foreach (TraktProfile profile in profiles) { ContactBinding myBinding = bindingManager.CreateContactBinding(profile.Username); if (!String.IsNullOrEmpty(profile.Name)) { if (profile.Name.Contains(" ")) { Regex regex = new Regex(@"\s"); String[] nameSplit = regex.Split(profile.Name); myBinding.FirstName = nameSplit[0]; myBinding.LastName = nameSplit[1]; } else { myBinding.LastName = profile.Name; } } try { if (!String.IsNullOrEmpty(profile.Name) && profile.Name.Contains(" ")) { Regex regex = new Regex(@"\s"); String[] nameSplit = regex.Split(profile.Name); AddContact(profile.Username, nameSplit[0], nameSplit[1], profile.Username, profile.Avatar, profile.Url); } else { AddContact(profile.Username, "", "", profile.Username, profile.Avatar, profile.Url); } await bindingManager.SaveContactBindingAsync(myBinding); } catch (Exception e) { Console.Write(e.InnerException); } } }
private async Task <bool> ProcessConnectData(DownloadRichConnectDataOperation downloadRichConnectDataOperation) { ContactBindingManager store = await ContactBindings.GetAppContactBindingManagerAsync(); ContactStore contactStore = await ContactStore.CreateOrOpenAsync(); foreach (string id in (IEnumerable <string>)downloadRichConnectDataOperation.Ids) { string remoteId = id; try { string title = (await contactStore.FindContactByRemoteIdAsync(remoteId)).DisplayName; ContactBinding contactBinding = await store.GetContactBindingByRemoteIdAsync(remoteId); ConnectTileData connectTileData = new ConnectTileData(); connectTileData.Title = title; BackendResult <PhotosListWithCount, ResultCode> profilePhotos = await PhotosService.Current.GetProfilePhotos(RemoteIdHelper.GetItemIdByRemoteId(remoteId), 0, 3); if (profilePhotos.ResultCode == ResultCode.Succeeded) { for (int index = 0; index < Math.Min(3, profilePhotos.ResultData.response.Count); ++index) { Photo photo = profilePhotos.ResultData.response[index]; ConnectTileImage connectTileImage = new ConnectTileImage(); connectTileImage.ImageUrl = photo.src_big; ((ICollection <ConnectTileImage>)connectTileData.Images).Add(connectTileImage); } } contactBinding.TileData = connectTileData; await store.SaveContactBindingAsync(contactBinding); title = null; contactBinding = null; connectTileData = null; } catch (Exception ex) { Logger.Instance.Error("ProcessConnectData failed", ex); } remoteId = null; } return(true); }
private async Task ProcessOperationAsync(DownloadRichConnectDataOperation operation) { try { await ParallelForEach(operation.Ids, async (string remoteId) => { if (remoteId.Contains(",")) { remoteId = remoteId.Split(',')[0]; } ContactBinding binding = await contactBindingManager.GetContactBindingByRemoteIdAsync(remoteId); TraktProfile tileData = await new UserController().GetUserProfile(remoteId); binding.TileData = new ConnectTileData(); binding.TileData.Title = tileData.Username; ConnectTileImage tileImage = new ConnectTileImage(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(tileData.Avatar)); HttpWebResponse webResponse = await request.GetResponseAsync() as HttpWebResponse; MemoryStream memoryStream = new MemoryStream(); webResponse.GetResponseStream().CopyTo(memoryStream); IRandomAccessStream stream = await ConvertToRandomAccessStream(memoryStream); await tileImage.SetImageAsync(stream); binding.TileData.Images.Add(tileImage); await contactBindingManager.SaveContactBindingAsync(binding); }); } catch (Exception e) { } finally { NotifyComplete(); } }