Advertisement
Magicbjorn

ContactOverViewViewController

Feb 17th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.79 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using MonoTouch.Foundation;
  4. using MonoTouch.UIKit;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7.  
  8. namespace Planning
  9. {
  10.     public partial class ContactOverviewViewController : UIViewController
  11.     {
  12.         #region private members
  13.         private DataAccessController dataController;
  14.  
  15.         private List<Contact> contacts, filteredContacts;
  16.         private UITableView tableContacts;
  17.  
  18.         private UISearchBar searchBar;
  19.         #endregion
  20.  
  21.         #region constructor and properties
  22.         public ContactOverviewViewController () : base ("ContactOverviewViewController", null)
  23.         {
  24.             dataController = new DataAccessController (AppDelegate.UserID);
  25.         }
  26.  
  27.         static bool UserInterfaceIdiomIsPhone
  28.         {
  29.             get
  30.             {
  31.                 return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone;
  32.             }
  33.         }
  34.         #endregion
  35.  
  36.         public override void LoadView ()
  37.         {
  38.             base.LoadView ();
  39.  
  40.             contacts = AppDelegate.ContactList;
  41.             filteredContacts = new List<Contact> ();
  42.         }
  43.  
  44.         public override void ViewDidLoad ()
  45.         {
  46.             base.ViewDidLoad ();
  47.  
  48.             searchBar = new UISearchBar ();
  49.             searchBar.Placeholder = "Zoek contact.";
  50.             searchBar.SizeToFit ();
  51.             searchBar.ShowsCancelButton = true;
  52.             searchBar.AutocorrectionType = UITextAutocorrectionType.No;
  53.             searchBar.AutocapitalizationType = UITextAutocapitalizationType.Words;
  54.             searchBar.TextChanged += (object sender, UISearchBarTextChangedEventArgs e) =>
  55.             {
  56.                 SearchContact (searchBar.Text);
  57.             };
  58.  
  59.             searchBar.CancelButtonClicked += (sender, e) =>
  60.             {
  61.                 searchBar.Text = "";
  62.                 SearchContact (searchBar.Text);
  63.                 searchBar.ResignFirstResponder();
  64.             };
  65.  
  66.             searchBar.SearchButtonClicked += (sender, e) =>
  67.             {
  68.                 if (filteredContacts.Count() == 0)
  69.                 {
  70.                     SearchClicked();
  71.                 }
  72.  
  73.                 searchBar.ResignFirstResponder();
  74.             };
  75.  
  76.             tableContacts = new UITableView (new RectangleF (0, 0, AppDelegate.ScreenWidth, AppDelegate.ScreenHeight));
  77.             tableContacts.Bounces = true;
  78.             tableContacts.ScrollEnabled = true;
  79.             tableContacts.SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine;
  80.             tableContacts.SeparatorColor = UIColor.DarkGray;
  81.  
  82.             this.View.AddSubview (tableContacts);
  83.  
  84.             tableContacts.TableHeaderView = searchBar;
  85.  
  86.             CreateNavigationBar ();
  87.         }
  88.  
  89.         public override void WillRotate (UIInterfaceOrientation toInterfaceOrientation, double duration)
  90.         {
  91.             base.WillRotate (toInterfaceOrientation, duration);
  92.  
  93.             if (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight)
  94.             {
  95.                 AppDelegate.IsLandscape = true;
  96.             }
  97.  
  98.             else
  99.             {
  100.                 AppDelegate.IsLandscape = false;
  101.             }
  102.         }
  103.  
  104.         public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
  105.         {
  106.             base.DidRotate (fromInterfaceOrientation);
  107.  
  108.             AppDelegate.ScreenWidth = this.View.Frame.Width;
  109.             AppDelegate.ScreenHeight = this.View.Frame.Height;
  110.         }
  111.  
  112.         public override void ViewWillAppear(bool animated)
  113.         {
  114.             base.ViewWillAppear (animated);
  115.  
  116.             RefreshTableView ();
  117.         }
  118.  
  119.         private void CreateNavigationBar ()
  120.         {
  121.             UIBarButtonItem menuButton;
  122.  
  123.             //Style settings
  124.             this.NavigationController.SetNavigationBarHidden (false, false);
  125.             this.NavigationController.NavigationBar.Translucent = false;
  126.             this.NavigationController.NavigationBar.BarTintColor = AppDelegate.Theme;
  127.             this.NavigationController.NavigationBar.TintColor = UIColor.White;
  128.             this.NavigationController.NavigationBar.TitleTextAttributes = AppDelegate.NavigationStringAttributes;
  129.  
  130.             this.NavigationItem.Title = "Contactenoverzicht";
  131.  
  132.             menuButton = new UIBarButtonItem (
  133.                 UIImage.FromFile ("Images/menu_iphone.png"),
  134.                 UIBarButtonItemStyle.Plain, MenuClicked);
  135.  
  136.             menuButton.TintColor = UIColor.White;
  137.  
  138.             this.NavigationItem.LeftBarButtonItem = menuButton;
  139.         }
  140.  
  141.         public void RefreshTableView()
  142.         {
  143.             ContactOverviewDataSource source;
  144.  
  145.             if (searchBar.Text == null || searchBar.Text == "")
  146.             {
  147.                 source = new ContactOverviewDataSource (contacts);
  148.             }
  149.  
  150.             else
  151.             {
  152.                 source = new ContactOverviewDataSource (filteredContacts);
  153.             }
  154.  
  155.             source.OnRowSelected += (object sender, ContactOverviewDataSource.RowSelectedEventArgs e) =>
  156.             {
  157.                 char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray ();
  158.                 string[] keys = new string[letters.Length];
  159.  
  160.                 for (int i = 0; i < letters.Length; i++)
  161.                 {
  162.                     keys [i] = letters [i].ToString ();
  163.                 }
  164.  
  165.                 List<Contact> temp = new List<Contact> ();
  166.  
  167.                 foreach (var c in contact)
  168.                 {
  169.                     if (c.LastName.ToLower()[0].ToString() == keys [e.indexPath.Section].ToLower())
  170.                     {
  171.                         temp.Add (c);
  172.                     }
  173.                 }
  174.  
  175.                 AppDelegate.SelectedContact = temp[e.indexPath.Row];
  176.                 this.NavigationController.PushViewController (new ContactDetailViewController (), true);
  177.                 e.tableView.DeselectRow (e.indexPath, true);
  178.             };
  179.  
  180.             tableContacts.Source = source;
  181.             tableContacts.ReloadData ();
  182.         }
  183.  
  184.         void MenuClicked (object sender, EventArgs e)
  185.         {
  186.             AppDelegate.Navigation.ToggleMenu ();
  187.         }
  188.  
  189.         void SearchClicked()
  190.         {
  191.             AppDelegate.SelectedContact = dataController.GetContactByID (searchBar.Text);
  192.  
  193.             if (AppDelegate.SelectedContact != null)
  194.             {
  195.                 this.NavigationController.PushViewController (new ContactDetailViewController (), true);
  196.             }
  197.  
  198.             else
  199.             {
  200.                 AppDelegate.ShowPopupMessage("Het opgegeven contactnummer is niet juist. Probeer het opnieuw.");
  201.             }
  202.         }
  203.  
  204.         public void SearchContact (string filter)
  205.         {
  206.             filteredContacts.Clear ();
  207.  
  208.             foreach (var contact in contacts)  
  209.             {
  210.                 if (contact.LastName.ToLower().Contains (filter.ToLower ()) || contact.ContactID.Contains (filter))
  211.                 {
  212.                     if (!filteredContacts.Contains (contact))
  213.                     {
  214.                         filteredContacts.Add (contact);
  215.                     }
  216.                 }
  217.             }
  218.  
  219.             RefreshTableView ();
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement