/// <summary> /// Fetches all contacts matching the query, if any. /// </summary> /// <param name="query">Search Query</param> private void LoadContactsFromAPI(string query = "") { _loadedContacts.Clear(); _needToReloadContacts = false; MLResult result; MLContactsListPage fillPage = null; if (string.IsNullOrEmpty(query)) { fillPage = MLContacts.CreateListPage(MLContacts.DEFAULT_FETCH_LIMIT, out result, RefreshListPageReady, HandlePageFailed); if (!result.IsOk) { Log(string.Format("<color=red>Cannot load contacts. Reason: {0}</color>", result)); Debug.LogErrorFormat("Error: ContactsExample failed to create the contacts list page, disabling script. Reason: {0}", result); enabled = false; return; } } else { // change second parameter to limit searching by name, email address, or phone number only fillPage = MLContacts.CreateSearchPage(query, MLContactsSearchField.All, MLContacts.DEFAULT_FETCH_LIMIT, out result, RefreshListPageReady, HandlePageFailed); if (!result.IsOk) { Log(string.Format("<color=red>Cannot search contacts. Reason: {0}</color>", result)); Debug.LogErrorFormat("Error: ContactsExample failed to create the contacts search page, disabling script. Reason: {0}", result); enabled = false; return; } } // begin fetching contacts result = fillPage.NextPage(); if (!result.IsOk) { Log(string.Format("<color=red>Cannot load contacts. Reason: {0}</color>", result)); Debug.LogErrorFormat("Error: ContactsExample failed to request the next page of contacts, disabling script. Reason: {0}", result); enabled = false; return; } }
/// <summary> /// Handler when page is retrieved successfully. Store the list of contacts in memory and /// fetch the next page. If this is the last page, display them. /// </summary> /// <param name="page">Page with list of contacts</param> private void RefreshListPageReady(MLContactsListPage page) { foreach (MLContactsContact contact in page.ContactsList) { _loadedContacts[contact.ID] = contact; } if (page.Status == MLContactsListPage.PageStatus.LastPage) { _listPage.PopulateList(new List <MLContactsContact>(_loadedContacts.Values)); } else { MLResult result = page.NextPage(); // automatically fetches the next page; triggering RefreshListPageReady when done or HandlePageFailed on failure if (!result.IsOk) { Log("<color=red>RefreshListPageReady() Error: " + result.ToString() + "</color>"); Debug.LogErrorFormat("Error: ContactsExample failed to request the next page while refreshing the list, disabling script. Reason was: {0}", result); enabled = false; return; } } }
/// <summary> /// Handler when page failed to retrieve. /// </summary> /// <param name="page">Page that failed</param> /// <param name="result">Result of the operation</param> private void HandlePageFailed(MLContactsListPage page, MLResult result) { Log(string.Format("<color=red>HandlePageFailed() Error: {0}</color>", result)); Debug.LogErrorFormat("Error: ContactsExample failed to retreive a page. Reason was: {0}", result); }