public override void OnEnter() { DateTime?birthday = null; if (!string.IsNullOrEmpty(birthdayInput.Value)) { DateTime parseBirthday; if (DateTime.TryParseExact(birthdayInput.Value, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out parseBirthday)) { birthday = parseBirthday; } else { errorMsg.Value = "Can't convert birthday to yyyy-MM-dd format."; Fsm.Event(eventTarget, isNotSuccessEvent); } } AddedPhoneNumbersObject addedPhoneNumbersTemp = (AddedPhoneNumbersObject)phoneNumbersObject.Value; AddedEmailsObject addedEmailsTemp = (AddedEmailsObject)emailsObject.Value; List <StringStringKeyValuePair> addedEmails = addedEmailsTemp.AddedEmails != null ? addedEmailsTemp.AddedEmails : new List <StringStringKeyValuePair>(); List <StringStringKeyValuePair> addedPhoneNumbers = addedPhoneNumbersTemp.AddedPhoneNumbers != null ? addedPhoneNumbersTemp.AddedPhoneNumbers : new List <StringStringKeyValuePair>(); Texture2D avatarImage = (Texture2D)avatarImageInput.Value; //var avatar = avatarImage != null ? (Texture2D)avatarImage : null; Contact contact = new Contact() { FirstName = firstName.Value, MiddleName = middleName.Value, LastName = lastName.Value, Company = company.Value, Birthday = birthday, Emails = addedEmails.Select(email => new KeyValuePair <string, string>(email.Key, email.Value)).ToArray(), PhoneNumbers = addedPhoneNumbers.Select(phoneNumber => new KeyValuePair <string, string>(phoneNumber.Key, phoneNumber.Value)).ToArray(), Photo = avatarImage }; string error = DeviceContacts.AddContact(contact); if (string.IsNullOrEmpty(error)) { Fsm.Event(eventTarget, isSuccessEvent); } else { errorMsg.Value = error; Fsm.Event(eventTarget, isNotSuccessEvent); } }
IEnumerator CRAddContact() { yield return(StartCoroutine(CRRequestPermission(AndroidPermission.AndroidPermissionWriteContacts))); string firstName = firstnNameInput.text; string middleName = middleNameInput.text; string lastName = lastNameInput.text; string company = companyInput.text; DateTime?birthday = null; if (!string.IsNullOrEmpty(birthdayInput.text)) { DateTime parseBirthday; if (DateTime.TryParseExact(birthdayInput.text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out parseBirthday)) { birthday = parseBirthday; } else { NativeUI.Alert("Invalid birthday", "Can't convert birthday to yyyy-MM-dd format."); yield break; } } var avatar = avatarImage.texture != null ? (Texture2D)avatarImage.texture : null; var error = DeviceContacts.AddContact(new Contact() { FirstName = firstName, MiddleName = middleName, LastName = lastName, Company = company, Birthday = birthday, Emails = addedEmails.Select(email => new KeyValuePair <string, string>(email.Key, email.Value)).ToArray(), PhoneNumbers = addedPhoneNumbers.Select(phoneNumber => new KeyValuePair <string, string>(phoneNumber.Key, phoneNumber.Value)).ToArray(), Photo = avatar }); NativeUI.Alert("Add Contact", error ?? "Added new contact successfully"); }