Search code examples
xamarin.formscontactsphonebook

Xamarin Forms: How to fetch the latest contacts of phone after adding a number to phonebook?


I have referred to this blog for listing the phone contacts. Also, I have implemented adding contacts to the phonebook using DependencyService as per this thread.

My problem is after adding a contact to the device phone book I need to fetch the entire latest contacts. Also, I need to show the new contact on the contact listview.

For reference, I have created a sample project and uploaded it here. In this sample first of all I am listing the phone contacts with the Add New option on the top. If we tap Add New, a new page will open with the Add to Contact option and entry for phone number. Click the Add to Contact option after entering a phone number, then the device phonebook page will show with the phone number entered.

At this stage, the user may or may not save that number to the device phonebook. So when the user resuming to the App I need to fetch the entire contacts and check the phone number is added or not to the device phone book. If contact added I will hide the Add to Contact option else I will show that option again. At the same time when the user going back to the contact list, I need to show the newly added contact over there.

For this, I have added a message on App.xaml.cs and Subscribe it on AddContactPage.

App.xaml.cs

protected override void OnResume()
{
    // Handle when your app resumes
    MessagingCenter.Send(this, "isContactAdded");
}

AddContactPage

MessagingCenter.Subscribe<App>(this, "isContactAdded", (sender) =>
{
    //How I can fetch the entire latest contacts here
    //After fetching conatcts only I can check the new phone number is added to the device phonebook
});

The contacts are passing as an argument from MainActivity to App.xaml.cs. So I don't know how to fetch it directly. Is there any way to fetch all contacts on this page?


Solution

  • //loading all the new contacts
    protected async override void OnResume()
    {
        if (Utility.isContactAdding)
        {
            UserDialogs.Instance.ShowLoading("");
            List<Contact> contacts = await contactsService.RetrieveContactsAsync() as List<Contact>;
            MessagingCenter.Send<App, List<Contact>>(App.Current as App, "isContactAdded", contacts);
        }
    }
    
    //Comparing the new contacts with phone number
    MessagingCenter.Subscribe<App, List<Contact>>(App.Current, "isContactAdded", (snd, arg) =>
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            Utility.ContactsList.Clear();
            phone = Regex.Replace(phone, @"[^0-9+]+", "");
            bool isContactExist = false;
            var AllNewContacts = arg as List<Contact>;
            foreach(var item in AllNewContacts)
            {
                if (item.PhoneNumbers.Length != 0)
                {
                    foreach(var number in item.PhoneNumbers)
                    {
                        if (number.Replace("-", "").Replace(" ","") == phone)
                        {
                            isContactExist = true;
                        }
                    }
                }
                Utility.ContactsList.Add(item);
            }
            if (isContactExist)
            {
                Phonebook_layout.IsVisible = false;
                MessagingCenter.Send<CallHistoryDetailPage>(this, "refreshcontacts");
            }
            else
            {
                Phonebook_layout.IsVisible = true;
            }
            Utility.isContactAdding = false;
            UserDialogs.Instance.HideLoading();
        });
    });
    
    //Subscribed message and refershing the contacts
    MessagingCenter.Subscribe<CallHistoryDetailPage>(this, "refreshcontacts", (sender) =>
    {
        BindingContext = new ContactsViewModel(Utility.myContacts);
    });