private async Task <ContactList> GetContactListAsync(UserDataAccount userDataAccount, ContactStore store) { try { var user = _cacheService.GetUser(_cacheService.Options.MyId); var displayName = user?.GetFullName() ?? "Unigram"; ContactList contactList = null; if (_cacheService.Options.TryGetValue("x_contact_list", out string id)) { contactList = await store.GetContactListAsync(id); } if (contactList == null) { contactList = await store.CreateContactListAsync(displayName, userDataAccount.Id); await _protoService.SendAsync(new Telegram.Td.Api.SetOption("x_contact_list", new Telegram.Td.Api.OptionValueString(contactList.Id))); } contactList.DisplayName = displayName; contactList.OtherAppWriteAccess = ContactListOtherAppWriteAccess.None; await contactList.SaveAsync(); return(contactList); } catch { return(null); } }
public async Task <bool> DeleteContact(Contact contact) { try { ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly); var contactLists = await allAccessStore.FindContactListsAsync(); string contactListId = string.Empty; if (contactLists.Count != 0) { contactListId = contactLists.FirstOrDefault().Id; return(false); } var contactList = await allAccessStore.GetContactListAsync(contactListId); await contactList.DeleteContactAsync(contact); return(true); } catch { return(false); } }
public async Task InserNewContact(string firstName, string lastName, string phone, string email) { var contactInfo = new Contact { FirstName = firstName, LastName = lastName }; contactInfo.Phones.Add(new ContactPhone() { Number = phone }); contactInfo.Emails.Add(new ContactEmail() { Address = email }); ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly); var contactLists = await allAccessStore.FindContactListsAsync(); string contactListId = string.Empty; if (contactLists.Count != 0) { contactListId = contactLists.FirstOrDefault().Id; return; } var contactList = await allAccessStore.GetContactListAsync(contactListId); await contactList.SaveContactAsync(contactInfo); }
private async void AddContact(string firstName, string lastName) { /// https://docs.microsoft.com/en-us/windows/uwp/contacts-and-calendar/integrating-with-contacts /// https://stackoverflow.com/questions/34647386/windows-phone-10-is-possible-to-edit-add-new-contact-programmatically-in-wind ContactStore contactstore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadWrite); IReadOnlyList <ContactList> contactLists = await contactstore.FindContactListsAsync(); ContactList lists2 = await contactstore.GetContactListAsync("24,d,d"); if (lists2 != null) { var contact1 = new Contact(); contact1.FirstName = firstName; contact1.LastName = lastName; await lists2.SaveContactAsync(contact1); return; } ContactList contactList = null; //if there is no contact list we create one if (contactLists.Count == 0) { try { contactList = await contactstore.CreateContactListAsync("MyList"); } catch (Exception ex) { Console.WriteLine(ex); } } //otherwise if there is one then we reuse it else { foreach (var c in contactLists) { if (c.DisplayName == "MyList") { contactList = c; break; } } } var contact = new Contact(); contact.FirstName = "Bob"; contact.LastName = "Doe"; ContactEmail email = new ContactEmail(); email.Address = "*****@*****.**"; email.Kind = ContactEmailKind.Other; contact.Emails.Add(email); await contactList.SaveContactAsync(contact); }