Created
December 13, 2012 02:13
-
-
Save darbio/4273471 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using MonoTouch.Dialog; | |
| using net.blazeware.groupsms.core.Sessions; | |
| using net.blazeware.groupsms.Controls; | |
| using Model = net.blazeware.groupsms.core.Model; | |
| using System.Linq; | |
| using net.blazeware.groupsms.UI; | |
| using MonoTouch.UIKit; | |
| using net.blazeware.groupsms.core.Model; | |
| using System.Collections.Generic; | |
| using Xamarin.Contacts; | |
| using System.Drawing; | |
| using System.Threading.Tasks; | |
| namespace net.blazeware.groupsms.UI | |
| { | |
| public class ContactPickerDialogViewController : DialogViewController | |
| { | |
| List<Contact> _contacts = new List<Contact>(); | |
| Model.Group _myGroup; | |
| public Model.Group Group { | |
| get { | |
| return _myGroup ?? (_myGroup = new Model.Group () { Members = new List<Member> () }); | |
| } | |
| set { | |
| _myGroup = value; | |
| } | |
| } | |
| public ContactPickerDialogViewController () : base (null, true) | |
| { | |
| // Events | |
| AppSession.Instance.AddressBookAccessDenied += HandleAddressBookAccessDenied; | |
| // Get contacts in async task | |
| Task<List<Contact>> getContacts = new Task<List<Contact>> (() => { | |
| return AppSession.Instance.Book.Where (a => a.Phones.Any(p => p.Type == PhoneType.Mobile)).ToList (); | |
| }); | |
| getContacts.ContinueWith ((a) => { | |
| _contacts = a.Result; | |
| BeginInvokeOnMainThread (() => { | |
| UpdateRoot (); | |
| }); | |
| }, TaskScheduler.FromCurrentSynchronizationContext()); | |
| getContacts.Start (); | |
| } | |
| void HandleAddressBookAccessDenied (object sender, EventArgs e) | |
| { | |
| BeginInvokeOnMainThread (() => { | |
| var alert = new UIAlertView ("Permission denied", "User has denied this app access to their contacts. Please enable in your setings.", null, "Close"); | |
| alert.Clicked += HandleAlertClicked; | |
| alert.Show(); | |
| }); | |
| } | |
| void HandleAlertClicked (object sender, UIButtonEventArgs e) | |
| { | |
| this.DismissViewController (true, null); | |
| } | |
| public override void LoadView () | |
| { | |
| // Enable Search | |
| this.EnableSearch = true; | |
| this.SearchPlaceholder = "Search for a Contact"; | |
| // Set tableview style | |
| this.Style = UITableViewStyle.Plain; | |
| base.LoadView (); | |
| // Add Root | |
| this.Root = new RootElement("Contacts picker"); | |
| // Set up Navigation Buttons | |
| this.NavigationItem.SetRightBarButtonItem (new MonoTouch.UIKit.UIBarButtonItem(MonoTouch.UIKit.UIBarButtonSystemItem.Done), false); | |
| this.NavigationItem.RightBarButtonItem.Clicked += HandleDoneClicked; | |
| } | |
| public override void ViewWillDisappear (bool animated) | |
| { | |
| base.ViewWillDisappear (animated); | |
| AppSession.Instance.AddressBookAccessDenied -= HandleAddressBookAccessDenied; | |
| } | |
| void HandleDoneClicked (object sender, EventArgs e) | |
| { | |
| this.Group.Members.Add (new Member () { | |
| DisplayName = "James DARBYSHIRE", | |
| Number = "+61435182990" | |
| }); | |
| this.NavigationController.PopViewControllerAnimated (true); | |
| } | |
| class IndexedSource : Source | |
| { | |
| ContactPickerDialogViewController parent; | |
| public IndexedSource (ContactPickerDialogViewController parent) : base (parent) | |
| { | |
| this.parent = parent; | |
| } | |
| public override string[] SectionIndexTitles (UITableView tableView) | |
| { | |
| return parent.GetSectionTitles (); | |
| } | |
| } | |
| public override Source CreateSizingSource (bool unevenRows) | |
| { | |
| return new IndexedSource (this); | |
| } | |
| void UpdateRoot () | |
| { | |
| var sections = (from sh in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" select new Section ("") { | |
| from element in ContactElements().Where (a => a.Caption.StartsWith (sh.ToString ())) | |
| select element | |
| }); | |
| this.Root.Add (sections); | |
| this.ReloadData (); | |
| this.TableView.SetNeedsDisplay (); | |
| } | |
| string [] GetSectionTitles () | |
| { | |
| try { | |
| return (from section in this.Root select (section.Header.Substring (0, 1))).ToArray (); | |
| } | |
| catch (Exception ex) { | |
| return null; | |
| } | |
| } | |
| List<Element> ContactElements () | |
| { | |
| var elements = new List<Element> (); | |
| foreach (var contact in _contacts) { | |
| var element = new ObjectElement<Contact> (contact); | |
| elements.Add (element); | |
| } | |
| return elements; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment