private async Task ExportAsyncInternal(Telegram.Td.Api.Users result) { Logs.Logger.Info(Logs.LogTarget.Contacts, "Exporting contacts"); Debug.WriteLine("» Exporting contacts"); var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite); if (store == null) { return; } var userDataAccount = await GetUserDataAccountAsync(); if (userDataAccount == null) { return; } var contactList = await GetContactListAsync(userDataAccount, store); var annotationList = await GetAnnotationListAsync(userDataAccount); if (contactList != null && annotationList != null) { await ExportAsync(contactList, annotationList, result); } Logs.Logger.Info(Logs.LogTarget.Contacts, "Exporting contacts completed"); Debug.WriteLine("» Exporting contacts completed"); }
public async Task SyncAsync(Telegram.Td.Api.Users result) { try { if (result == null) { return; } if (_syncToken == null) { _syncToken = new CancellationTokenSource(); } using (await _syncLock.WaitAsync(_syncToken.Token)) { await ExportAsyncInternal(result); await ImportAsyncInternal(); } } catch { Logs.Logger.Warning(Logs.LogTarget.Contacts, "Sync contacts canceled"); Debug.WriteLine("» Sync contacts canceled"); } }
public async Task ExportAsync(Telegram.Td.Api.Users result) { using (await _syncLock.WaitAsync()) { Debug.WriteLine("» Exporting contacts"); var store = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AppContactsReadWrite); if (store == null) { return; } var contactList = await GetContactListAsync(store); var annotationList = await GetAnnotationListAsync(); if (contactList != null && annotationList != null) { await ExportAsync(contactList, annotationList, result); } Debug.WriteLine("» Exporting contacts completed"); } }
public async Task ExportAsync(Telegram.Td.Api.Users result) { try { if (_syncToken == null) { _syncToken = new CancellationTokenSource(); } using (await _syncLock.WaitAsync(_syncToken.Token)) { await ExportAsyncInternal(result); } } catch { Logs.Log.Write("Sync contacts canceled"); Debug.WriteLine("» Sync contacts canceled"); } }
public async Task SyncAsync(Telegram.Td.Api.Users result) { try { if (_syncToken == null) { _syncToken = new CancellationTokenSource(); } using (await _syncLock.WaitAsync(_syncToken.Token)) { //_contacts = result.UserIds.ToDictionary(x => x, y => _protoService.GetUser(y)); await ExportAsyncInternal(result); await ImportAsyncInternal(); } } catch { Logs.Logger.Warning(Logs.Target.Contacts, "Sync contacts canceled"); Debug.WriteLine("» Sync contacts canceled"); } }
private async Task ExportAsync(ContactList contactList, ContactAnnotationList annotationList, Telegram.Td.Api.Users result) { if (result == null) { return; } var remove = new List <long>(); var prev = _contacts; var next = result.UserIds.ToHashSet(); if (prev != null) { foreach (var id in prev) { if (!next.Contains(id)) { remove.Add(id); } } } _contacts = next; foreach (var item in remove) { var contact = await contactList.GetContactFromRemoteIdAsync("u" + item); if (contact != null) { await contactList.DeleteContactAsync(contact); } } foreach (var item in result.UserIds) { var user = _protoService.GetUser(item); var contact = await contactList.GetContactFromRemoteIdAsync("u" + user.Id); if (contact == null) { contact = new Contact(); } if (user.ProfilePhoto != null && Telegram.Td.Api.Extensions.IsFileExisting(user.ProfilePhoto.Small.Local)) { contact.SourceDisplayPicture = await StorageFile.GetFileFromPathAsync(user.ProfilePhoto.Small.Local.Path); } contact.FirstName = user.FirstName; contact.LastName = user.LastName; //contact.Nickname = item.Username ?? string.Empty; contact.RemoteId = "u" + user.Id; //contact.Id = item.Id.ToString(); if (user.PhoneNumber.Length > 0) { var phone = contact.Phones.FirstOrDefault(); if (phone == null) { phone = new ContactPhone(); phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); contact.Phones.Add(phone); } else { phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); } } await contactList.SaveContactAsync(contact); ContactAnnotation annotation; var annotations = await annotationList.FindAnnotationsByRemoteIdAsync(user.Id.ToString()); if (annotations.Count == 0) { annotation = new ContactAnnotation(); } else { annotation = annotations[0]; } annotation.ContactId = contact.Id; annotation.RemoteId = contact.RemoteId; annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.AudioCall | ContactAnnotationOperations.VideoCall | ContactAnnotationOperations.Share; if (annotation.ProviderProperties.Count == 0) { annotation.ProviderProperties.Add("ContactPanelAppID", Package.Current.Id.FamilyName + "!App"); annotation.ProviderProperties.Add("ContactShareAppID", Package.Current.Id.FamilyName + "!App"); } await annotationList.TrySaveAnnotationAsync(annotation); } }
private async Task ExportAsync(ContactList contactList, ContactAnnotationList annotationList, Telegram.Td.Api.Users result) { if (result == null) { return; } foreach (var item in result.UserIds) { var user = _protoService.GetUser(item); var contact = await contactList.GetContactFromRemoteIdAsync("u" + user.Id); if (contact == null) { contact = new Contact(); } if (user.ProfilePhoto != null && user.ProfilePhoto.Small.Local.IsDownloadingCompleted) { contact.SourceDisplayPicture = await StorageFile.GetFileFromPathAsync(user.ProfilePhoto.Small.Local.Path); } contact.FirstName = user.FirstName ?? string.Empty; contact.LastName = user.LastName ?? string.Empty; //contact.Nickname = item.Username ?? string.Empty; contact.RemoteId = "u" + user.Id; //contact.Id = item.Id.ToString(); var phone = contact.Phones.FirstOrDefault(); if (phone == null) { phone = new ContactPhone(); phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); contact.Phones.Add(phone); } else { phone.Kind = ContactPhoneKind.Mobile; phone.Number = string.Format("+{0}", user.PhoneNumber); } await contactList.SaveContactAsync(contact); ContactAnnotation annotation; var annotations = await annotationList.FindAnnotationsByRemoteIdAsync(user.Id.ToString()); if (annotations.Count == 0) { annotation = new ContactAnnotation(); } else { annotation = annotations[0]; } annotation.ContactId = contact.Id; annotation.RemoteId = contact.RemoteId; annotation.SupportedOperations = ContactAnnotationOperations.ContactProfile | ContactAnnotationOperations.Message | ContactAnnotationOperations.AudioCall; if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5)) { annotation.SupportedOperations |= ContactAnnotationOperations.Share; } if (annotation.ProviderProperties.Count == 0) { annotation.ProviderProperties.Add("ContactPanelAppID", Package.Current.Id.FamilyName + "!App"); annotation.ProviderProperties.Add("ContactShareAppID", Package.Current.Id.FamilyName + "!App"); } var added = await annotationList.TrySaveAnnotationAsync(annotation); } }