Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using MonoTouch.Foundation;
- using MonoTouch.UIKit;
- using System.Collections.Generic;
- using System.Linq;
- namespace Planning
- {
- public partial class ContactOverviewViewController : UIViewController
- {
- #region private members
- private DataAccessController dataController;
- private List<Contact> contacts, filteredContacts;
- private UITableView tableContacts;
- private UISearchBar searchBar;
- #endregion
- #region constructor and properties
- public ContactOverviewViewController () : base ("ContactOverviewViewController", null)
- {
- dataController = new DataAccessController (AppDelegate.UserID);
- }
- static bool UserInterfaceIdiomIsPhone
- {
- get
- {
- return UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone;
- }
- }
- #endregion
- public override void LoadView ()
- {
- base.LoadView ();
- contacts = AppDelegate.ContactList;
- filteredContacts = new List<Contact> ();
- }
- public override void ViewDidLoad ()
- {
- base.ViewDidLoad ();
- searchBar = new UISearchBar ();
- searchBar.Placeholder = "Zoek contact.";
- searchBar.SizeToFit ();
- searchBar.ShowsCancelButton = true;
- searchBar.AutocorrectionType = UITextAutocorrectionType.No;
- searchBar.AutocapitalizationType = UITextAutocapitalizationType.Words;
- searchBar.TextChanged += (object sender, UISearchBarTextChangedEventArgs e) =>
- {
- SearchContact (searchBar.Text);
- };
- searchBar.CancelButtonClicked += (sender, e) =>
- {
- searchBar.Text = "";
- SearchContact (searchBar.Text);
- searchBar.ResignFirstResponder();
- };
- searchBar.SearchButtonClicked += (sender, e) =>
- {
- if (filteredContacts.Count() == 0)
- {
- SearchClicked();
- }
- searchBar.ResignFirstResponder();
- };
- tableContacts = new UITableView (new RectangleF (0, 0, AppDelegate.ScreenWidth, AppDelegate.ScreenHeight));
- tableContacts.Bounces = true;
- tableContacts.ScrollEnabled = true;
- tableContacts.SeparatorStyle = UITableViewCellSeparatorStyle.SingleLine;
- tableContacts.SeparatorColor = UIColor.DarkGray;
- this.View.AddSubview (tableContacts);
- tableContacts.TableHeaderView = searchBar;
- CreateNavigationBar ();
- }
- public override void WillRotate (UIInterfaceOrientation toInterfaceOrientation, double duration)
- {
- base.WillRotate (toInterfaceOrientation, duration);
- if (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight)
- {
- AppDelegate.IsLandscape = true;
- }
- else
- {
- AppDelegate.IsLandscape = false;
- }
- }
- public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
- {
- base.DidRotate (fromInterfaceOrientation);
- AppDelegate.ScreenWidth = this.View.Frame.Width;
- AppDelegate.ScreenHeight = this.View.Frame.Height;
- }
- public override void ViewWillAppear(bool animated)
- {
- base.ViewWillAppear (animated);
- RefreshTableView ();
- }
- private void CreateNavigationBar ()
- {
- UIBarButtonItem menuButton;
- //Style settings
- this.NavigationController.SetNavigationBarHidden (false, false);
- this.NavigationController.NavigationBar.Translucent = false;
- this.NavigationController.NavigationBar.BarTintColor = AppDelegate.Theme;
- this.NavigationController.NavigationBar.TintColor = UIColor.White;
- this.NavigationController.NavigationBar.TitleTextAttributes = AppDelegate.NavigationStringAttributes;
- this.NavigationItem.Title = "Contactenoverzicht";
- menuButton = new UIBarButtonItem (
- UIImage.FromFile ("Images/menu_iphone.png"),
- UIBarButtonItemStyle.Plain, MenuClicked);
- menuButton.TintColor = UIColor.White;
- this.NavigationItem.LeftBarButtonItem = menuButton;
- }
- public void RefreshTableView()
- {
- ContactOverviewDataSource source;
- if (searchBar.Text == null || searchBar.Text == "")
- {
- source = new ContactOverviewDataSource (contacts);
- }
- else
- {
- source = new ContactOverviewDataSource (filteredContacts);
- }
- source.OnRowSelected += (object sender, ContactOverviewDataSource.RowSelectedEventArgs e) =>
- {
- char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray ();
- string[] keys = new string[letters.Length];
- for (int i = 0; i < letters.Length; i++)
- {
- keys [i] = letters [i].ToString ();
- }
- List<Contact> temp = new List<Contact> ();
- foreach (var c in contact)
- {
- if (c.LastName.ToLower()[0].ToString() == keys [e.indexPath.Section].ToLower())
- {
- temp.Add (c);
- }
- }
- AppDelegate.SelectedContact = temp[e.indexPath.Row];
- this.NavigationController.PushViewController (new ContactDetailViewController (), true);
- e.tableView.DeselectRow (e.indexPath, true);
- };
- tableContacts.Source = source;
- tableContacts.ReloadData ();
- }
- void MenuClicked (object sender, EventArgs e)
- {
- AppDelegate.Navigation.ToggleMenu ();
- }
- void SearchClicked()
- {
- AppDelegate.SelectedContact = dataController.GetContactByID (searchBar.Text);
- if (AppDelegate.SelectedContact != null)
- {
- this.NavigationController.PushViewController (new ContactDetailViewController (), true);
- }
- else
- {
- AppDelegate.ShowPopupMessage("Het opgegeven contactnummer is niet juist. Probeer het opnieuw.");
- }
- }
- public void SearchContact (string filter)
- {
- filteredContacts.Clear ();
- foreach (var contact in contacts)
- {
- if (contact.LastName.ToLower().Contains (filter.ToLower ()) || contact.ContactID.Contains (filter))
- {
- if (!filteredContacts.Contains (contact))
- {
- filteredContacts.Add (contact);
- }
- }
- }
- RefreshTableView ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement