Skip to content

Instantly share code, notes, and snippets.

@nicwise
Created January 30, 2012 16:54
Show Gist options
  • Save nicwise/1705400 to your computer and use it in GitHub Desktop.
Save nicwise/1705400 to your computer and use it in GitHub Desktop.
Right aligned entry box
public class BTDateElement : EntryElement
{
public bool minDateSet = false;
public bool maxDateSet = false;
private DateTime _minDate, _maxDate;
public DateTime MinDate
{
get { return _minDate; }
set { _minDate = value; minDateSet = true; }
}
public DateTime MaxDate
{
get { return _maxDate; }
set { _maxDate = value; maxDateSet = true; }
}
public string FormatDate (DateTime dt)
{
return fmt.ToString (dt);
}
public string FormatDate(NSDate dt)
{
return fmt.ToString(dt);
}
private void DumpState(string ident)
{
Util.Log("");
Util.Log("------------------------------------------");
Util.Log("At: {0}", ident);
Util.Log("InternalDate: {0}", _internalDate);
//Util.Log("DateValue: {0}", DateValue);
Util.Log("TZ: {0}", NSTimeZone.LocalTimeZone.Name);
Util.Log("Full date {0}", fullFormat.ToString(_internalDate));
}
public UIDatePicker CreatePicker ()
{
var picker = new UIDatePicker(RectangleF.Empty) {
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight,
Mode = UIDatePickerMode.Date,
Date = _internalDate
};
if (minDateSet)
picker.MinimumDate = MinDate;
if (maxDateSet)
picker.MaximumDate = MaxDate;
picker.TimeZone = NSTimeZone.LocalTimeZone;
picker.Date = _internalDate;
return picker;
}
void SetDisplayValue ()
{
Value = FormatDate(_internalDate);
if (entry != null)
{
entry.Text = Value;
}
}
private NSDate _internalDate;
public NSDate InternalDate
{
get
{
return _internalDate;
}
set
{
_internalDate = value;
if (datePicker != null)
{
datePicker.TimeZone = NSTimeZone.LocalTimeZone;
datePicker.Date = _internalDate;
datePicker.TimeZone = NSTimeZone.LocalTimeZone;
}
SetDisplayValue();
}
}
public DateTime DateValue
{
get
{
string internalAsString = fullFormat.ToString(InternalDate);
DateTime dt;
if (DateTime.TryParseExact(internalAsString, "yyyy-MM-dd|HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out dt))
{
DumpState("DateValue:Set");
Util.Log("Date value converted to: {0}", dt);
return dt;
} else {
DumpState("DateValue:Set-Failed");
Util.Log("DateValue FAILED to convert! {0}", internalAsString);
return InternalDate;
}
}
set
{
InternalDate = value;
}
}
public UIDatePicker datePicker;
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 BTDateElement (string caption, DateTime date) : base (caption, "", "")
{
DateValue = date;
//InternalDate = date;
//DumpState("Ctr");
//fmt.DateStyle = NSDateFormatterStyle.Medium;
}
public static string key = "BTDateElement";
public override UITableViewCell CreateCell()
{
return new UITableViewCell (UITableViewCellStyle.Default, key);
}
public override UITableViewCell DequeueCell(UITableView tv)
{
return tv.DequeueReusableCell (key);
}
public override UITableViewCell GetCell (UITableView tv)
{
Value = FormatDate (_internalDate);
var cell = base.GetCell (tv);
datePicker = CreatePicker();
var view = new UIView(datePicker.Bounds);
view.AddSubview(datePicker);
entry.InputView = view;
entry.Ended += (sender, e) =>
{
_internalDate = datePicker.Date;
SetDisplayValue();
//DumpState("Ended");
};
//RIGHT HERE----------------------------------------------
entry.TextAlignment = UITextAlignment.Right;
entry.TextColor = UIColor.FromRGBA(56,84,135,255);
datePicker.ValueChanged += (sender, e) =>
{
_internalDate = datePicker.Date;
SetDisplayValue();
//DumpState("Vaue changed");
DoChanged();
};
return cell;
}
protected override void Dispose (bool disposing)
{
base.Dispose (disposing);
if (disposing){
fmt.Dispose ();
fmt = null;
if (datePicker != null){
datePicker.Dispose ();
datePicker = null;
}
}
}
public void MoveNext() {}
public void MovePrev() {}
public UIView Entry
{
get
{
return entry;
}
}
public UIView Control
{
get
{
return datePicker;
}
}
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
if (datePicker != null)
{
datePicker.Date = _internalDate;
}
//DumpState("Selected");
entry.BecomeFirstResponder();
}
public void ToToday()
{
if (datePicker != null)
{
InternalDate = NSDate.Now;
DoChanged();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment