public
Created

PickerElement

  • Download Gist
PickerElement.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
//
// PickerElement.cs: Defines UIPickerView element
// Based on DateTimeElement by Miguel de Icaza
//
// Author:
// Tomasz Cielecki (tomasz@ostebaronen.dk)
//
// Code licensed under the MIT X11 license
//
 
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
 
namespace Cirrious.MvvmCross.Dialog.Touch.Dialog.Elements
{
public interface IPickerComponentRow
{
string DisplayName { get; }
}
 
public interface IPickerComponent
{
IList<IPickerComponentRow> ComponentRows { get; }
IPickerComponentRow SelectedRow { get; set; }
float ComponentWidth { get; set; }
float RowHeight { get; set; }
}
 
public class PickerElementComponent : IPickerComponent
{
public IList<IPickerComponentRow> ComponentRows { get; set; }
public IPickerComponentRow SelectedRow { get; set; }
public float ComponentWidth { get; set; }
public float RowHeight { get; set; }
}
 
public class PickerElementComponentRow : IPickerComponentRow
{
public string DisplayName { get; set; }
}
 
public class PickerElement : ValueElement<IList<IPickerComponent>>
{
private static readonly NSString Key = new NSString("PickerElement");
 
private UIPickerView _picker;
 
public PickerElement(string caption, IList<IPickerComponent> data)
: base (caption, data)
{
}
protected override UITableViewCell GetCellImpl (UITableView tv)
{
var cell = tv.DequeueReusableCell(Key) ?? new UITableViewCell(UITableViewCellStyle.Value1, Key)
{Accessory = UITableViewCellAccessory.DisclosureIndicator};
 
UpdateDetailDisplay(cell);
return cell;
}
protected override void Dispose (bool disposing)
{
base.Dispose (disposing);
if (disposing)
{
if (_picker != null)
{
_picker.Model.Dispose();
_picker.Model = null;
_picker.Dispose();
_picker = null;
}
}
}
 
public virtual UIPickerView CreatePicker()
{
var picker = new UIPickerView(RectangleF.Empty)
{
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
Model = new PickerViewModel(Value),
ShowSelectionIndicator = true,
};
return picker;
}
static RectangleF PickerFrameWithSize (SizeF size)
{
var screenRect = UIScreen.MainScreen.ApplicationFrame;
float fY = 0, fX = 0;
switch (UIApplication.SharedApplication.StatusBarOrientation){
case UIInterfaceOrientation.LandscapeLeft:
case UIInterfaceOrientation.LandscapeRight:
fX = (screenRect.Height - size.Width) /2;
fY = (screenRect.Width - size.Height) / 2 -17;
break;
case UIInterfaceOrientation.Portrait:
case UIInterfaceOrientation.PortraitUpsideDown:
fX = (screenRect.Width - size.Width) / 2;
fY = (screenRect.Height - size.Height) / 2 - 25;
break;
}
return new RectangleF (fX, fY, size.Width, size.Height);
}
class PickerViewModel : UIPickerViewModel
{
private readonly IList<IPickerComponent> _components;
public IList<IPickerComponent> Components { get { return _components; } }
 
public PickerViewModel(IList<IPickerComponent> components)
{
_components = components;
}
 
public override int GetComponentCount(UIPickerView picker)
{
return null != _components ? _components.Count : 0;
}
 
public override int GetRowsInComponent(UIPickerView picker, int component)
{
return null != _components ? _components[component].ComponentRows.Count : 0;
}
 
public override string GetTitle(UIPickerView picker, int row, int component)
{
return null != _components ? _components[component].ComponentRows[row].DisplayName : null;
}
 
public override float GetComponentWidth(UIPickerView picker, int component)
{
if (null != _components)
{
if (_components[component].ComponentWidth > 0)
return _components[component].ComponentWidth;
 
if (null != picker)
return picker.Frame.Width/_components.Count;
}
return base.GetComponentWidth(picker, component);
}
 
public override float GetRowHeight(UIPickerView picker, int component)
{
if (null != _components)
if (_components[component].RowHeight > 0)
return _components[component].RowHeight;
return base.GetRowHeight(picker, component);
}
 
public override void Selected(UIPickerView picker, int row, int component)
{
if (null != _components)
_components[component].SelectedRow = _components[component].ComponentRows[row];
}
}
 
class PickerViewController : UIViewController
{
readonly PickerElement _container;
 
public PickerViewController(PickerElement container)
{
_container = container;
}
public override void ViewWillDisappear (bool animated)
{
base.ViewWillDisappear (animated);
_container.OnUserValueChanged(((PickerViewModel)_container._picker.Model).Components);
}
public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
{
base.DidRotate (fromInterfaceOrientation);
_container._picker.Frame = PickerFrameWithSize (_container._picker.SizeThatFits (SizeF.Empty));
}
public bool Autorotate { get; set; }
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return Autorotate;
}
}
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
var vc = new PickerViewController(this)
{
Autorotate = dvc.Autorotate
};
_picker = CreatePicker ();
_picker.Frame = PickerFrameWithSize (_picker.SizeThatFits (SizeF.Empty));
 
var components = ((PickerViewModel) _picker.Model).Components;
for (var i = 0; i < components.Count; i++)
{
_picker.Select(components[i].ComponentRows.IndexOf(components[i].SelectedRow), i, true);
}
 
vc.View.BackgroundColor = UIColor.Black;
vc.View.AddSubview (_picker);
dvc.ActivateController (vc);
}
 
protected override void UpdateDetailDisplay(UITableViewCell cell)
{
if (cell == null)
{
return;
}
 
if (cell.DetailTextLabel != null)
{
var sb = new StringBuilder();
foreach (var component in Value)
{
sb.AppendFormat("{0}", component.SelectedRow.DisplayName);
}
cell.DetailTextLabel.Text = sb.ToString();
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.