refactored AddPosition activity a bit

This commit is contained in:
mgroves 2010-11-09 10:43:42 -05:00
parent 72029335c5
commit 136421d11a
2 changed files with 25 additions and 17 deletions

View file

@ -19,19 +19,24 @@ namespace MonoStockPortfolio
public static string ClassName { get { return "monoStockPortfolio.AddPositionActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
private IPortfolioRepository _repo;
private EditText TickerTextBox { get { return FindViewById<EditText>(Resource.id.addPositionTicker); } }
private EditText PriceTextBox { get { return FindViewById<EditText>(Resource.id.addPositionPrice); } }
private EditText SharesTextBox { get { return FindViewById<EditText>(Resource.id.addPositionShares); } }
private Button SaveButton { get { return FindViewById<Button>(Resource.id.addPositionSaveButton); } }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.layout.addposition);
var saveButton = FindViewById<Button>(Resource.id.addPositionSaveButton);
saveButton.Click += saveButton_Click;
SaveButton.Click += saveButton_Click;
}
void saveButton_Click(object sender, System.EventArgs e)
void saveButton_Click(object sender, EventArgs e)
{
var position = new Position();
if(Validate(position))
@ -46,22 +51,13 @@ namespace MonoStockPortfolio
private bool Validate(Position position)
{
var tickerTextBox = FindViewById<EditText>(Resource.id.addPositionTicker);
var priceTextBox = FindViewById<EditText>(Resource.id.addPositionPrice);
var sharesTextBox = FindViewById<EditText>(Resource.id.addPositionShares);
var validator = new FormValidator();
validator.AddRequired(tickerTextBox, "Please enter a ticker");
validator.AddValidPositiveDecimal(sharesTextBox, "Please enter a valid, positive number of shares");
validator.AddValidPositiveDecimal(priceTextBox, "Please enter a valid, positive price per share");
var result = validator.Validate();
var result = ValidationRules.Apply();
if (result == string.Empty)
{
position.Shares = decimal.Parse(sharesTextBox.Text.ToString());
position.PricePerShare = decimal.Parse(priceTextBox.Text.ToString());
position.Ticker = tickerTextBox.Text.ToString();
position.Shares = decimal.Parse(SharesTextBox.Text.ToString());
position.PricePerShare = decimal.Parse(PriceTextBox.Text.ToString());
position.Ticker = TickerTextBox.Text.ToString();
position.ContainingPortfolioID = Intent.GetLongExtra(Extra_PortfolioID, -1);
return true;
}
@ -69,5 +65,17 @@ namespace MonoStockPortfolio
Toast.MakeText(this, result, ToastLength.Long).Show();
return false;
}
private FormValidator ValidationRules
{
get
{
var validator = new FormValidator();
validator.AddRequired(TickerTextBox, "Please enter a ticker");
validator.AddValidPositiveDecimal(SharesTextBox, "Please enter a valid, positive number of shares");
validator.AddValidPositiveDecimal(PriceTextBox, "Please enter a valid, positive price per share");
return validator;
}
}
}
}

View file

@ -30,7 +30,7 @@ namespace MonoStockPortfolio.Validation
AddValidation(control, () => ValidPositiveDecimal(control, errorMessage));
}
public string Validate()
public string Apply()
{
var errorMessage = string.Empty;
foreach (var keyValuePair in _dict)