/// <summary> /// Adds a new contact or updates an existing contact. Called by ContactPage. /// </summary> /// <param name="contact">New Contact</param> /// <returns>Result of operation</returns> public MLResult SaveContact(MLContactsContact contact) { Log("Saving..."); ulong requestHandle = 0; MLResult result; if (!string.IsNullOrEmpty(contact.ID)) { Debug.LogFormat("Updating existing contact with id = {0}", contact.ID); result = MLContacts.UpdateContact(contact, out requestHandle); } else { Debug.LogFormat("Saving new contact with name = {0}", contact.Name); result = MLContacts.AddContact(contact, out requestHandle); } if (!result.IsOk) { Debug.LogErrorFormat("Error: ContactsExample failed to save contact. Reason: {0}", result); Log(string.Format("<color=red>Failed to save contact. {0}</color>", result)); } return(result); }
/// <summary> /// Requests privileges and starts MLContacts. /// </summary> private void StartAPI() { #if PLATFORM_LUMIN MLResult result = MLPrivilegesStarterKit.Start(); if (!result.IsOk) { Debug.LogErrorFormat("Error: MLContactsBehavior failed starting MLPrivileges, disabling script. Reason: {0}", result); OnStartupComplete?.Invoke(false); enabled = false; return; } result = MLPrivilegesStarterKit.RequestPrivileges(MLPrivileges.Id.AddressBookRead, MLPrivileges.Id.AddressBookWrite); if (result.Result != MLResult.Code.PrivilegeGranted) { Debug.LogErrorFormat("Error: MLContactsBehavior failed requesting privileges, disabling script. Reason: {0}", result); OnStartupComplete?.Invoke(false); enabled = false; return; } MLPrivilegesStarterKit.Stop(); result = MLContacts.Start(); if (!result.IsOk) { Debug.LogErrorFormat("Error: MLContactsBehavior failed starting MLContacts, disabling script. Reason: {0}", result); OnStartupComplete?.Invoke(false); enabled = false; return; } OnStartupComplete?.Invoke(true); #endif }
/// <summary> /// Clean Up. /// </summary> void OnDestroy() { #if PLATFORM_LUMIN if (MLContacts.IsStarted) { MLContacts.Stop(); } #endif }
/// <summary> /// Clean Up. /// </summary> void OnDestroy() { _privilegeRequester.OnPrivilegesDone -= HandlePrivilegesDone; MLInput.OnControllerButtonUp -= HandleControllerButtonUp; if (MLContacts.IsStarted) { MLContacts.OnContactAdded -= HandleOnContactAdded; MLContacts.OnContactUpdated -= HandleOnContactUpdated; MLContacts.OnContactDeleted -= HandleOnContactDeleted; MLContacts.OnOperationFailed -= HandleOnOperationFailed; MLContacts.Stop(); } }
/// <summary> /// Request a new page /// </summary> /// <param name="pageLength">The length of the page.</param> /// <param name="offset">Offset into the page.</param> /// <returns> /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successfully submitted /// MLResult.Result will be <c>MLResult.Code.InvalidParam</c> if either of the parameters are invalid. /// MLResult.Result will be <c>MLResult.Code.PrivilegeDenied</c> if necessary privilege is missing. /// </returns> protected override MLResult InternalNewPage(uint pageLength, string offset) { MLResult.Code resultCode = MLContacts.GetSearch(this.searchQuery, this.searchFields, pageLength, offset, out ulong handle); this.RequestHandle = handle; if (resultCode != MLResult.Code.Ok) { this.Status = PageStatus.Failed; return(MLResult.Create(MLResult.Code.Ok)); } this.Status = PageStatus.Pending; MLDevice.RegisterUpdate(this.Update); this.UnregisterUpdate = true; return(MLResult.Create(MLResult.Code.Ok)); }
/// <summary> /// Handler when the user wants to save the contact. /// </summary> private void HandleSaveEdit() { UpdateContactFromFields(); #if PLATFORM_LUMIN MLResult contactResult = MLContacts.ValidateContact(_contact); if (!contactResult.IsOk) { _visualizerStatusText.text = string.Format("<color=red>{0}</color>", contactResult.ToString()); return; } _contactsVisualizer.Contacts.SaveContact(_contact); #endif // leave the page, the MLContactsBehavior will update the // page depending on the result of operation }
/// <summary> /// Fetches all contacts matching the query, if any. /// </summary> /// <param name="query">The query string to use for searching.</param> public void LoadContactsFromAPI(string query = "") { #if PLATFORM_LUMIN if (!MLContacts.IsStarted) { return; } loadedContacts.Clear(); MLResult result; MLContacts.ListPage fillPage = null; if (string.IsNullOrEmpty(query)) { fillPage = MLContacts.CreateListPage(MLContacts.DefaultFetchLimit, out result, RefreshListPageReady, HandlePageFailed); if (!result.IsOk) { Debug.LogErrorFormat("Error: MLContactsBehavior 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, MLContacts.SearchField.All, MLContacts.DefaultFetchLimit, out result, RefreshListPageReady, HandlePageFailed); if (!result.IsOk) { Debug.LogErrorFormat("Error: MLContactsBehavior failed to create the contacts search page, disabling script. Reason: {0}", result); enabled = false; return; } } // Begin fetching contacts. result = fillPage.NextPage(); if (!result.IsOk) { Debug.LogErrorFormat("Error: MLContactsBehavior failed to request the next page of contacts, disabling script. Reason: {0}", result); enabled = false; return; } #endif }
/// <summary> /// Deletes a contact. Called by ContactListPage. /// </summary> /// <param name="id">Id of the contact to delete.</param> public void DeleteContact(string id) { #if PLATFORM_LUMIN ulong requestHandle = 0; if (!string.IsNullOrEmpty(id) && loadedContacts.ContainsKey(id)) { MLResult result = MLContacts.DeleteContact(loadedContacts[id], out requestHandle); if (!result.IsOk) { Debug.LogErrorFormat("Error: MLContactsBehavior failed to delete contact. Reason: {0}", result); } } else { Debug.LogErrorFormat("Error: MLContactsBehavior failed to delete contact. Reason: Invalid ID {0}", id); } #endif }
/// <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> /// Start the API and register callbacks. /// </summary> private void StartupAPI() { MLResult result = MLContacts.Start(); if (!result.IsOk) { Debug.LogErrorFormat("Error: ContactsExample failed starting MLContacts, disabling script. Reason: {0}", result); enabled = false; return; } Log("<color=green>API Started</color>"); MLContacts.OnContactAdded += HandleOnContactAdded; MLContacts.OnContactUpdated += HandleOnContactUpdated; MLContacts.OnContactDeleted += HandleOnContactDeleted; MLContacts.OnOperationFailed += HandleOnOperationFailed; LoadListPage(); }
/// <summary> /// Deletes a contact. Called by ContactListPage. /// </summary> /// <param name="id">ID of the Contact</param> public void DeleteContact(string id) { Log("Deleting contact"); ulong requestHandle = 0; if (!string.IsNullOrEmpty(id) && _loadedContacts.ContainsKey(id)) { MLResult result = MLContacts.DeleteContact(_loadedContacts[id], out requestHandle); if (!result.IsOk) { Debug.LogErrorFormat("Error: ContactsExample failed to delete contact. Reason: {0}", result); Log(string.Format("<color=red>Failed to delete contact. {0}</color>", result)); } } else { Debug.LogErrorFormat("Error: ContactsExample failed to delete contact. Reason: Invalid ID {0}", id); Log("<color=red>Failed to delete contact.</color>"); } }
/// <summary> /// Must check for privileges again after pause. /// </summary> void OnApplicationPause(bool pause) { if (pause) { #if PLATFORM_LUMIN if (MLContacts.IsStarted) { MLContacts.Stop(); } #endif } else { #if PLATFORM_LUMIN if (MLDevice.IsReady()) { StartAPI(); } #endif } }
/// <summary> /// Adds a new contact or updates an existing contact. /// Called by ContactPageVisualizer. /// </summary> /// <param name="contact">The new contact to save.</param> public MLResult SaveContact(MLContacts.Contact contact) { ulong requestHandle = 0; MLResult result; if (!string.IsNullOrEmpty(contact.ID)) { Debug.LogFormat("Updating existing contact with id = {0}", contact.ID); result = MLContacts.UpdateContact(contact, out requestHandle); } else { Debug.LogFormat("Saving new contact with name = {0}", contact.Name); result = MLContacts.AddContact(contact, out requestHandle); } if (!result.IsOk) { Debug.LogErrorFormat("Error: MLContactsBehavior failed to save contact. Reason: {0}", result); } return(result); }
/// <summary> /// Validates a contact. /// </summary> /// <param name="contact">Contact to be validated</param> /// <returns>Result of Validation</returns> public MLResult ValidateContact(MLContactsContact contact) { return(MLContacts.ValidateContact(contact)); }