Created
November 2, 2011 16:38
-
-
Save nicwise/1334139 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using MonoTouch.Dialog; | |
using MonoTouch.Foundation; | |
using MonoTouch.UIKit; | |
namespace MobileAgent | |
{ | |
public class AddCarDialogViewController : BaseDetailDialogViewController | |
{ | |
public AddCarDialogViewController () : base(null, true) | |
{ | |
Title = "Add a vehicle"; | |
//this.Style = MonoTouch.UIKit.UITableViewStyle.Plain; | |
vehicle = new Vehicle (); | |
vehicle.RebillRate = 0.45; | |
if (!DataSource.Instance.Company.IsUKCompany) | |
{ | |
vehicle.ReclaimRate = 0.45; | |
} | |
InitDialog(); | |
} | |
public AddCarDialogViewController(Vehicle currentVehicle) : base(null, true) | |
{ | |
vehicle = currentVehicle; | |
Title = currentVehicle.TypeDescription; | |
IsEdit = true; | |
InitDialog(); | |
} | |
private void InitDialog() | |
{ | |
NavigationItem.RightBarButtonItem = new UIBarButtonItem ("Save", UIBarButtonItemStyle.Bordered, delegate { | |
if (PopulateVehicle ()) | |
{ | |
if (IsEdit) | |
{ | |
DataSource.Instance.UpdateItem<Vehicle>(vehicle); | |
Util.FlashStatus("Vehicle saved."); | |
} else | |
{ | |
DataSource.Instance.CreateItem<Vehicle>(vehicle); | |
Util.FlashStatus ("Vehicle added."); | |
} | |
NavigationController.PopViewControllerAnimated (true); | |
} else { | |
alert = new UIAlertView ("Invalid input", ValidationErrorText, null, "Ok"); | |
alert.Show (); | |
} | |
}); | |
BuildRootElement (); | |
} | |
private bool IsEdit = false; | |
private UIAlertView alert = null; | |
private Vehicle vehicle; | |
private string ValidationErrorText = ""; | |
public bool PopulateVehicle () | |
{ | |
vehicle.Description = descriptionElement.Value; | |
if (vehicle.Description == "") | |
{ | |
ValidationErrorText = "You must provide a description for this vehicle"; | |
return false; | |
} | |
vehicle.VehicleTypeInt = vehicleTypeRadio.Selected; | |
if (vehicle.VehicleType == VehicleType.Car) { | |
vehicle.EngineSizeInt = engineSizeRadio.Selected; | |
vehicle.EngineTypeInt = engineTypeRadio.Selected; | |
} | |
double val = 0; | |
if (!Util.TryParseDoubleWithoutLocalisation(rebillRateElement.Value, out val)) | |
{ | |
ValidationErrorText = string.Format("Rebill rate must be a valid decimal number (eg 0{0}44)", Util.DecimalSeperator); | |
return false; | |
} | |
vehicle.RebillRate = val; | |
vehicle.ReclaimRate = 0; | |
if (reclaimRateElement != null) { | |
if (!Util.TryParseDoubleWithoutLocalisation(reclaimRateElement.Value, out val)) | |
{ | |
ValidationErrorText = string.Format("Reclaim rate must be a valid decimal number (eg 0.44)", Util.DecimalSeperator); | |
return false; | |
} | |
vehicle.ReclaimRate = val; | |
} | |
return true; | |
} | |
public override void RefreshFromDatastore () | |
{ | |
} | |
public override void ViewWillAppear (bool animated) | |
{ | |
base.ViewWillAppear (animated); | |
int currentType = vehicleTypeRadio.Selected; | |
if (currentType == 0) { | |
//car | |
if (engineRoot.Parent == null) { | |
Root[0].Insert (2, engineRoot, sizeRoot); | |
} | |
} else { | |
//bike etc | |
if (engineRoot.Parent != null) { | |
Root[0].RemoveRange (2, 2, UITableViewRowAnimation.Fade); | |
engineRoot.Parent = null; | |
sizeRoot.Parent = null; | |
} | |
} | |
} | |
public BTEntryElement descriptionElement, reclaimRateElement, rebillRateElement; | |
public RadioGroup vehicleTypeRadio; | |
public RootElement engineRoot, sizeRoot; | |
public RadioGroup engineTypeRadio, engineSizeRadio; | |
public void BuildRootElement () | |
{ | |
descriptionElement = new BTEntryElement ("Description", "Describe your vehicle", vehicle.Description); | |
vehicleTypeRadio = new RadioGroup ("type", vehicle.VehicleTypeInt); | |
engineTypeRadio = new RadioGroup (vehicle.EngineTypeInt); | |
engineSizeRadio = new RadioGroup (vehicle.EngineSizeInt); | |
engineRoot = new BackgroundRootElement ("Engine", engineTypeRadio) { | |
new Section { | |
new RadioElement ("Petrol"), | |
new RadioElement ("Diesel"), | |
new RadioElement ("LPG") | |
} | |
}; | |
sizeRoot = new BackgroundRootElement ("Size", engineSizeRadio) { | |
new Section { | |
new RadioElement ("under 1400cc"), | |
new RadioElement ("1400-2000cc"), | |
new RadioElement ("over 2000cc") | |
} | |
} | |
; | |
var newRoot = new RootElement ("Add a vehicle") { | |
new Section { | |
descriptionElement, | |
new BackgroundRootElement ("Type", vehicleTypeRadio) { | |
new Section { | |
new RadioElement ("Car"), | |
new RadioElement ("Motorcycle"), | |
new RadioElement ("Bicycle") | |
} | |
} | |
} | |
}; | |
if (DataSource.Instance.Company.IsUKCompany) { | |
rebillRateElement = new BTEntryElement ("Rebill Rate", "", string.Format("{0:0.00}", vehicle.RebillRate)) { KeyboardType = Util.DecimalKeyboardType }; | |
newRoot.Add (new Section ("", string.Format("Enter 0{0}4 for 40p per mile", Util.DecimalSeperator)) { rebillRateElement }); | |
} else { | |
string unit = "km"; | |
if (DataSource.Instance.Company.CompanyDefaultCurrency == "USD" || | |
DataSource.Instance.Company.CompanyDefaultCurrency == "CAD" || | |
DataSource.Instance.Company.CompanyDefaultCurrency == "GBP" ) { | |
unit = "mile"; | |
} | |
reclaimRateElement = new BTEntryElement ("Reclaim Rate", "", string.Format("{0:0.00}", vehicle.ReclaimRate)) { KeyboardType = Util.DecimalKeyboardType }; | |
rebillRateElement = new BTEntryElement ("Rebill Rate", "", string.Format("{0:0.00}", vehicle.RebillRate)) { KeyboardType = Util.DecimalKeyboardType }; | |
newRoot.Add (new Section ("", "Enter 0{1}4 for 40c per {2}. Units are based on your {0} account settings. No conversion is done.".Fmt(ServiceConfig.ServiceName, Util.DecimalSeperator, unit)) { reclaimRateElement, rebillRateElement }); | |
} | |
Root = newRoot; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment