// Called when a phone number is swiped for deletion. Illustrates how to delete a multivalue property
        protected void DeletePhoneNumber(int phoneNumberID)
        {
            using (ABAddressBook addressBook = new ABAddressBook()) {
                ABPerson contact = addressBook.GetPerson(contactID);

                // get the phones and copy them to a mutable set of multivalues (so we can edit)
                ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue();

                // loop backwards and delete the phone number
                for (int i = phones.Count - 1; i >= 0; i--)
                {
                    if (phones [i].Identifier == phoneNumberID)
                    {
                        phones.RemoveAt(i);
                    }
                }

                // attach the phones back to the contact
                contact.SetPhones(phones);

                // save the changes
                addressBook.Save();

                // show an alert, letting the user know the number deletion was successful
                new UIAlertView("Alert", "Phone Number Deleted", null, "OK", null).Show();

                // repopulate the page
                PopulatePage(contact);
            }
        }
        protected void BtnAddPhoneNumberTouchUpInside(object sender, EventArgs e)
        {
            // get a reference to the contact
            using (ABAddressBook addressBook = new ABAddressBook())
            {
                ABPerson contact = addressBook.GetPerson(contactID);

                // get the phones and copy them to a mutable set of multivalues (so we can edit)
                ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue();

                // add the phone number to the phones via the multivalue.Add method
                phones.Add(new NSString(txtPhoneLabel.Text), new NSString(txtPhoneNumber.Text));

                // attach the phones back to the contact
                contact.SetPhones(phones);

                // save the address book changes
                addressBook.Save();

                // show an alert, letting the user know the number addition was successful
                new UIAlertView("Alert", "Phone Number Added", null, "OK", null).Show();

                // update the page
                PopulatePage(contact);

                // we have to call reload to refresh the table because the action didn't originate
                // from the table.
                tblPhoneNumbers.ReloadData();
            }
        }
Exemplo n.º 3
0
        public IContact Load(String id)
        {
            if (String.IsNullOrWhiteSpace(id))
            {
                throw new ArgumentNullException("id");
            }

            CheckStatus();

            Int32 rowId;

            if (!Int32.TryParse(id, out rowId))
            {
                throw new ArgumentException("Not a valid contact ID", "id");
            }

            ABPerson person = addressBook.GetPerson(rowId);

            if (person == null)
            {
                return(null);
            }

            return(ContactHelper.GetContact(person));
        }
Exemplo n.º 4
0
        public List <Person> GetPeople()
        {
            NSError       error;
            List <Person> result = new List <Person> ();

            Xamarin.Contacts.AddressBook book = new Xamarin.Contacts.AddressBook();

            ABAddressBook nativeBook = ABAddressBook.Create(out error);

            nativeBook.ExternalChange += BookChanged;

            foreach (Xamarin.Contacts.Contact c in book)
            {
                if (c.Phones.Count() > 0)
                {
                    Person p = new Person(Convert.ToInt32(c.Id), c.FirstName, c.MiddleName, c.LastName);

                    p.ModificationDate = nativeBook.GetPerson(p.Id).ModificationDate;

                    p.NickName = c.Nickname;
                    if (c.Organizations.Count() > 0)
                    {
                        p.Organization = c.Organizations.ElementAt(0).Name;
                    }

                    p.Notes = String.Concat(c.Notes.Select(n => n.Contents));

                    StringBuilder sb = new StringBuilder();

                    foreach (Phone phone in c.Phones)
                    {
                        PhoneNumber pnb = new PhoneNumber()
                        {
                            Type = (PhoneNumberType)((int)phone.Type), Number = phone.Number
                        };
                        p.Phones.Add(pnb);
                        sb.AppendFormat("{0},{1};", (int)pnb.Type, pnb.Number);
                    }
                    p.DetailData = p.Details;
                    p.PhoneData  = sb.ToString();

                    result.Add(p);
                }
            }



            return(result);
        }
Exemplo n.º 5
0
        protected void BtnSaveChangesTouchUpInside(object sender, EventArgs e)
        {
            using (ABAddressBook addressBook = new ABAddressBook()) {
                ABPerson contact = addressBook.GetPerson(contactID);

                if (contact != null)
                {
                    // save contact name information
                    contact.FirstName = txtFirstName.Text;
                    contact.LastName  = txtLastName.Text;

                    // get the phones and copy them to a mutable set of multivalues (so we can edit)
                    ABMutableMultiValue <string> phones = contact.GetPhones().ToMutableMultiValue();

                    // remove all phones data
                    // HACK: Cast nint to int
                    for (int i = (int)phones.Count - 1; i >= 0; i--)
                    {
                        phones.RemoveAt(i);
                    }

                    // add the phone number to the phones from the table data source
                    for (int i = 0; i < PhoneNumberTableSource.labels.Count; i++)
                    {
                        phones.Add(new NSString(PhoneNumberTableSource.numbers [i]), new NSString(PhoneNumberTableSource.labels [i]));
                    }

                    // attach the phones back to the contact
                    contact.SetPhones(phones);

                    // save the address book changes
                    addressBook.Save();

                    // show an alert, letting the user know information saved successfully
                    new UIAlertView("Alert", "Contact Information Saved!", null, "OK", null).Show();

                    // update the page
                    PopulatePage(contact);

                    // we have to call reload to refresh the table because the action didn't originate
                    // from the table.
                    tblPhoneNumbers.ReloadData();
                }
                else
                {
                    new UIAlertView("Alert", "Please select a contact using the top right button", null, "OK", null).Show();
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="addressBook">
        /// A <see cref="ABAddressBook"/>
        /// </param>
        /// <param name="requestedGroupName">
        /// A <see cref="System.String"/>
        /// </param>
        /// <returns>
        /// A <see cref="ABPerson[]"/>
        /// </returns>
        private ABPerson[] GetContactsByGroup(ABAddressBook addressBook, string requestedGroupName)
        {
            ABPerson [] people = new ABPerson[0];

            ABGroup[] groups = addressBook.GetGroups();

            foreach(ABGroup abGroup in groups) {
                if(abGroup.Name == requestedGroupName) {
                    ABRecord[] records = abGroup.GetMembers(DEFAULT_CONTACTS_LIST_SORT); //get list sorted by FirstName (default)
                    people = new ABPerson[records.Length];
                    for(int i=0; i< records.Length; i++) {
                        ABRecord record = records[i];
                        ABPerson person = addressBook.GetPerson(record.Id);
                        if(person!=null) {
                            people[i] = person;
                        }
                    }
                    break;
                }
            }
            return people;
        }
Exemplo n.º 7
0
        private static void AddContactInfo(ABPerson selectedPerson, Item item)
        {
            // find the contact in the address book
            var book = new AddressBook();
            Contact contact = null;
            if (book.LoadSupported)
                contact = book.Load(selectedPerson.Id.ToString());
            else
                contact = book.FirstOrDefault(c => c.Id == selectedPerson.Id.ToString());

            if (contact == null)
                return;

            // make a copy of the item
            var itemCopy = new Item(item, true);

            // get more info from the address book
            var mobile = (from p in contact.Phones where
                p.Type == Xamarin.Contacts.PhoneType.Mobile
                select p.Number).FirstOrDefault();
            var home = (from p in contact.Phones where
                p.Type == Xamarin.Contacts.PhoneType.Home
                select p.Number).FirstOrDefault();
            var work = (from p in contact.Phones where
                p.Type == Xamarin.Contacts.PhoneType.Work
                select p.Number).FirstOrDefault();
            var email = (from em in contact.Emails
                select em.Address).FirstOrDefault();
            //var website = (from w in contact.Websites
            //    select w.Address).FirstOrDefault();

            string birthday = null;
            if (selectedPerson.Birthday != null)
                birthday = ((DateTime)selectedPerson.Birthday).ToString("d");

            if (birthday != null)
                item.GetFieldValue(FieldNames.Birthday, true).Value = birthday;
            if (mobile != null)
                item.GetFieldValue(FieldNames.Phone, true).Value = mobile;
            if (home != null)
                item.GetFieldValue(FieldNames.HomePhone, true).Value = home;
            if (work != null)
                item.GetFieldValue(FieldNames.WorkPhone, true).Value = work;
            if (email != null)
                item.GetFieldValue(FieldNames.Email, true).Value = email;
            /*
            if (website != null)
                item.GetFieldValue(FieldNames.Website, true).Value = website
                */

            // save changes to local storage
            Folder folder = App.ViewModel.LoadFolder(item.FolderID);
            StorageHelper.WriteFolder(folder);

            // enqueue the Web Request Record
            RequestQueue.EnqueueRequestRecord(RequestQueue.UserQueue,
                new RequestQueue.RequestRecord()
                {
                    ReqType = RequestQueue.RequestRecord.RequestType.Update,
                    Body = new List<Item>() { itemCopy, item },
                    BodyTypeName = "Item",
                    ID = item.ID
                });

            // add the zaplify contact header to a special Zaplify "related name" field
            // use the native address book because the cross-platform AddressBook class is read-only
            var ab = new ABAddressBook();
            var contactToModify = ab.GetPerson(selectedPerson.Id);

            /* RelatedNames doesn't work on a real device (iPad)
            var relatedNames = contactToModify.GetRelatedNames().ToMutableMultiValue();
            if (relatedNames.Any(name => name.Label == ZaplifyContactHeader))
            {
                // remove the existing one (can't figure out a way to get a mutable ABMultiValueEntry out of zapField)
                var zapField = relatedNames.Single(name => name.Label == ZaplifyContactHeader);
                relatedNames.RemoveAt(relatedNames.GetIndexForIdentifier(zapField.Identifier));
            }
            // add the Zaplify related name field with the itemID value
            relatedNames.Add(item.ID.ToString(), new MonoTouch.Foundation.NSString(ZaplifyContactHeader));
            contactToModify.SetRelatedNames(relatedNames);
            */

            var zapField = contactToModify.Note;
            if (zapField == null)
                contactToModify.Note = ZaplifyContactHeader + item.ID.ToString();
            else
            {
                if (zapField.Contains(ZaplifyContactHeader))
                {
                    var idstring = GetContactID(zapField);
                    if (idstring != null)
                        contactToModify.Note = zapField.Replace(idstring, item.ID.ToString());
                    else
                        contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
                }
                else
                    contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
            }

            // save changes to the address book
            ab.Save();
        }
        // Called when a phone number is swiped for deletion. Illustrates how to delete a multivalue property
        protected void DeletePhoneNumber(int phoneNumberID)
        {
            using (ABAddressBook addressBook = new ABAddressBook ()) {
                ABPerson contact = addressBook.GetPerson (contactID);

                // get the phones and copy them to a mutable set of multivalues (so we can edit)
                ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();

                // loop backwards and delete the phone number
                for (int i = phones.Count - 1; i >= 0; i--) {
                    if (phones [i].Identifier == phoneNumberID)
                        phones.RemoveAt (i);
                }

                // attach the phones back to the contact
                contact.SetPhones (phones);

                // save the changes
                addressBook.Save ();

                // show an alert, letting the user know the number deletion was successful
                new UIAlertView ("Alert", "Phone Number Deleted", null, "OK", null).Show ();

                // repopulate the page
                PopulatePage (contact);
            }
        }
        protected void BtnSaveChangesTouchUpInside(object sender, EventArgs e)
        {
            using (ABAddressBook addressBook = new ABAddressBook ()) {
                ABPerson contact = addressBook.GetPerson (contactID);

                if (contact != null) {
                    // save contact name information
                    contact.FirstName = txtFirstName.Text;
                    contact.LastName = txtLastName.Text;

                    // get the phones and copy them to a mutable set of multivalues (so we can edit)
                    ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();

                    // remove all phones data
                    for (int i = phones.Count - 1; i >= 0; i--) {
                        phones.RemoveAt (i);
                    }

                    // add the phone number to the phones from the table data source
                    for (int i = 0; i < PhoneNumberTableSource.labels.Count; i++) {
                        phones.Add (new NSString (PhoneNumberTableSource.numbers [i]), new NSString (PhoneNumberTableSource.labels [i]));
                    }

                    // attach the phones back to the contact
                    contact.SetPhones (phones);

                    // save the address book changes
                    addressBook.Save ();

                    // show an alert, letting the user know information saved successfully
                    new UIAlertView ("Alert", "Contact Information Saved!", null, "OK", null).Show ();

                    // update the page
                    PopulatePage (contact);

                    // we have to call reload to refresh the table because the action didn't originate
                    // from the table.
                    tblPhoneNumbers.ReloadData ();
                } else {
                    new UIAlertView ("Alert", "Please select a contact using the top right button", null, "OK", null).Show ();
                }
            }
        }
        protected void BtnAddPhoneNumberTouchUpInside(object sender, EventArgs e)
        {
            // get a reference to the contact
            using (ABAddressBook addressBook = new ABAddressBook ()) {
                ABPerson contact = addressBook.GetPerson (contactID);
                if (contact != null) {
                    // get the phones and copy them to a mutable set of multivalues (so we can edit)
                    ABMutableMultiValue<string> phones = contact.GetPhones ().ToMutableMultiValue ();

                    // add the phone number to the phones via the multivalue.Add method
                    if (txtPhoneNumber.Text != null || txtPhoneLabel.Text != null) {
                        phones.Add (new NSString (txtPhoneNumber.Text), new NSString (txtPhoneLabel.Text));

                        // attach the phones back to the contact
                        contact.SetPhones (phones);

                        // save the address book changes
                        addressBook.Save ();

                        // show an alert, letting the user know the number addition was successful
                        new UIAlertView ("Alert", "Phone Number Added", null, "OK", null).Show ();

                        // update the page
                        PopulatePage (contact);

                        // we have to call reload to refresh the table because the action didn't originate
                        // from the table.
                        tblPhoneNumbers.ReloadData ();
                    } else {
                        // show an alert, letting the user know he has to fill the phone label and phone number
                        new UIAlertView ("Alert", "You have to fill a label and a phone number", null, "OK", null).Show ();
                    }
                } else {
                    new UIAlertView ("Alert", "Please select a contact using the top right button", null, "OK", null).Show ();
                }
            }
        }
Exemplo n.º 11
0
        private static void AddContactInfo(ABPerson selectedPerson, Item item)
        {
            // find the contact in the address book
            var     book    = new AddressBook();
            Contact contact = null;

            if (book.LoadSupported)
            {
                contact = book.Load(selectedPerson.Id.ToString());
            }
            else
            {
                contact = book.FirstOrDefault(c => c.Id == selectedPerson.Id.ToString());
            }

            if (contact == null)
            {
                return;
            }

            // make a copy of the item
            var itemCopy = new Item(item, true);

            // get more info from the address book
            var mobile = (from p in contact.Phones where
                          p.Type == Xamarin.Contacts.PhoneType.Mobile
                          select p.Number).FirstOrDefault();
            var home = (from p in contact.Phones where
                        p.Type == Xamarin.Contacts.PhoneType.Home
                        select p.Number).FirstOrDefault();
            var work = (from p in contact.Phones where
                        p.Type == Xamarin.Contacts.PhoneType.Work
                        select p.Number).FirstOrDefault();
            var email = (from em in contact.Emails
                         select em.Address).FirstOrDefault();
            //var website = (from w in contact.Websites
            //    select w.Address).FirstOrDefault();

            string birthday = null;

            if (selectedPerson.Birthday != null)
            {
                birthday = ((DateTime)selectedPerson.Birthday).ToString("d");
            }

            if (birthday != null)
            {
                item.GetFieldValue(FieldNames.Birthday, true).Value = birthday;
            }
            if (mobile != null)
            {
                item.GetFieldValue(FieldNames.Phone, true).Value = mobile;
            }
            if (home != null)
            {
                item.GetFieldValue(FieldNames.HomePhone, true).Value = home;
            }
            if (work != null)
            {
                item.GetFieldValue(FieldNames.WorkPhone, true).Value = work;
            }
            if (email != null)
            {
                item.GetFieldValue(FieldNames.Email, true).Value = email;
            }

            /*
             * if (website != null)
             *  item.GetFieldValue(FieldNames.Website, true).Value = website
             */

            // save changes to local storage
            Folder folder = App.ViewModel.LoadFolder(item.FolderID);

            StorageHelper.WriteFolder(folder);

            // enqueue the Web Request Record
            RequestQueue.EnqueueRequestRecord(RequestQueue.UserQueue,
                                              new RequestQueue.RequestRecord()
            {
                ReqType = RequestQueue.RequestRecord.RequestType.Update,
                Body    = new List <Item>()
                {
                    itemCopy, item
                },
                BodyTypeName = "Item",
                ID           = item.ID
            });

            // add the zaplify contact header to a special Zaplify "related name" field
            // use the native address book because the cross-platform AddressBook class is read-only
            var ab = new ABAddressBook();
            var contactToModify = ab.GetPerson(selectedPerson.Id);

            /* RelatedNames doesn't work on a real device (iPad)
             * var relatedNames = contactToModify.GetRelatedNames().ToMutableMultiValue();
             * if (relatedNames.Any(name => name.Label == ZaplifyContactHeader))
             * {
             *  // remove the existing one (can't figure out a way to get a mutable ABMultiValueEntry out of zapField)
             *  var zapField = relatedNames.Single(name => name.Label == ZaplifyContactHeader);
             *  relatedNames.RemoveAt(relatedNames.GetIndexForIdentifier(zapField.Identifier));
             * }
             * // add the Zaplify related name field with the itemID value
             * relatedNames.Add(item.ID.ToString(), new MonoTouch.Foundation.NSString(ZaplifyContactHeader));
             * contactToModify.SetRelatedNames(relatedNames);
             */

            var zapField = contactToModify.Note;

            if (zapField == null)
            {
                contactToModify.Note = ZaplifyContactHeader + item.ID.ToString();
            }
            else
            {
                if (zapField.Contains(ZaplifyContactHeader))
                {
                    var idstring = GetContactID(zapField);
                    if (idstring != null)
                    {
                        contactToModify.Note = zapField.Replace(idstring, item.ID.ToString());
                    }
                    else
                    {
                        contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
                    }
                }
                else
                {
                    contactToModify.Note += String.Format("\n{0}{1}", ZaplifyContactHeader, item.ID.ToString());
                }
            }

            // save changes to the address book
            ab.Save();
        }