using System; using MonoTouch.Dialog; using MonoTouch.UIKit; using System.Drawing; using MonoTouch.Foundation; using System.Collections.Generic; using System.Globalization; namespace TravelBudget { public class StyledExpandableDateElement : Element, IElementSizing { public Action ValueChanged; DateTime _time = DateTime.Now; public DateTime Value { get { return _time; } set { if (_time != value) { _time = value; UpdateText (); if (ValueChanged != null) ValueChanged (Value); } } } UIDatePickerMode _mode; public UIDatePickerMode Mode { get { return _mode; } set { _mode = value; } } readonly RootElement _root; readonly Section _section; UIDatePicker _picker; Element _pickerElement; UITableViewCell _cell; bool minDateSet = false; bool maxDateSet = false; DateTime maxDate = DateTime.MaxValue; DateTime minDate = DateTime.MinValue; public DateTime MaxDate { get { return maxDate; } set { maxDate = value; maxDateSet = true; } } public DateTime MinDate { get { return minDate; } set { minDate = value; minDateSet = true; } } public StyledExpandableDateElement (string caption, DateTime dateTime, UIDatePickerMode mode, RootElement root, Section section) : base (caption) { Mode = mode; //Handle issues where time values are set and the date isn't. This leads to odd behavor. if (mode == UIDatePickerMode.Time) { if (dateTime.Year < 2000) { var currentDate = DateTime.Now; dateTime = new DateTime (currentDate.Year, currentDate.Month, currentDate.Day, dateTime.Hour, dateTime.Minute, dateTime.Second); } } Value = dateTime; _root = root; _section = section; } public override UITableViewCell GetCell (UITableView tv) { const string cellKey = "StyledExpandableDateElement"; var cell = tv.DequeueReusableCell (cellKey); if (cell == null) { cell = new UITableViewCell (UITableViewCellStyle.Value1, cellKey); cell.TextLabel.Font = UIFont.SystemFontOfSize (17f); cell.TextLabel.TextColor = UIColor.Black; cell.TextLabel.AdjustsFontSizeToFitWidth = true; cell.TextLabel.MinimumScaleFactor = .5f; cell.Accessory = UITableViewCellAccessory.None; cell.SelectionStyle = UITableViewCellSelectionStyle.None; cell.DetailTextLabel.Font = UIFont.SystemFontOfSize (16f); cell.DetailTextLabel.TextColor = UIColor.LightGray; cell.DetailTextLabel.AdjustsFontSizeToFitWidth = true; cell.DetailTextLabel.MinimumScaleFactor = .5f; } _cell = cell; cell.TextLabel.Text = Caption; UpdateText (); return cell; } public float GetHeight (UITableView tableView, NSIndexPath indexPath) { return 44; } protected internal NSDateFormatter fmt = new NSDateFormatter () { DateStyle = NSDateFormatterStyle.Medium, TimeZone = NSTimeZone.LocalTimeZone }; protected internal NSDateFormatter fullFormat = new NSDateFormatter () { DateFormat = "yyyy-MM-dd'|'HH:mm", TimeZone = NSTimeZone.LocalTimeZone }; public void Contract() { if (_picker != null) { _section.Remove (_pickerElement); //_root.Reload (_section, UITableViewRowAnimation.Fade); _pickerElement.Dispose (); _picker.Dispose (); _pickerElement = null; _picker = null; _cell.DetailTextLabel.TextColor = UIColor.LightGray; } } public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path) { base.Selected (dvc, tableView, path); _root.TableView.EndEditing (true); if (_picker == null) { var otherExpandableItems = new List (); foreach(var element in _section) { if (element is StyledExpandableDateElement && element != this) { otherExpandableItems.Add (element as StyledExpandableDateElement); } } foreach(var element in otherExpandableItems) { element.Contract (); } int left = 0; _picker = new UIDatePicker (new RectangleF (left, 0, 320, 200)) { Mode = Mode, Date = Value, TimeZone = NSTimeZone.LocalTimeZone }; _picker.TimeZone = NSTimeZone.LocalTimeZone; if (minDateSet) _picker.MinimumDate = minDate; if (maxDateSet) _picker.MaximumDate = maxDate; _picker.BackgroundColor = UIColor.White; _picker.ValueChanged += (object sender, EventArgs e) => { UIDatePicker picker = sender as UIDatePicker; string internalAsString = fullFormat.ToString (_picker.Date); DateTime dt; if (DateTime.TryParseExact (internalAsString, "yyyy-MM-dd|HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out dt)) { Value = dt; } else { Value = _picker.Date; } Console.WriteLine("Value: " + Value); }; _cell.DetailTextLabel.TextColor = UIColor.Blue; _pickerElement = new UIViewElement ("", _picker, true); var idx = _section.Elements.IndexOf (this); var elements = new List { _pickerElement }; _section.Insert (idx + 1, UITableViewRowAnimation.Top, elements); _root.TableView.ScrollToRow (IndexPath, UITableViewScrollPosition.Bottom, true); } else { Contract (); _root.TableView.ScrollToRow (IndexPath, UITableViewScrollPosition.Middle, true); } } void UpdateText () { string text; switch (Mode) { case UIDatePickerMode.Date: text = Value.ToShortDateString (); break; case UIDatePickerMode.DateAndTime: text = Value.ToShortDateString () + " " + Value.ToShortTimeString (); break; case UIDatePickerMode.Time: text = Value.ToShortTimeString (); break; default: text = ""; break; } if (_cell != null) { _cell.DetailTextLabel.Text = text; } } protected override void Dispose (bool disposing) { if (disposing) { if (_picker != null) { _picker.Dispose (); } if (_pickerElement != null) { _pickerElement.Dispose (); } _cell = null; } base.Dispose (disposing); } } }