MonodroidStockPortfolio/MonoStockPortfolio/AddPositionActivity.cs

68 lines
2.2 KiB
C#
Raw Normal View History

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Validation;
namespace MonoStockPortfolio
{
[Activity(Label = "Add Position", MainLauncher = false)]
public partial class AddPositionActivity : Activity
{
[IoC] private IPortfolioRepository _repo;
2010-11-09 15:43:42 +00:00
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.layout.addposition);
2010-11-09 15:43:42 +00:00
SaveButton.Click += saveButton_Click;
}
2010-11-09 15:43:42 +00:00
void saveButton_Click(object sender, EventArgs e)
{
var position = new Position();
if(Validate(position))
{
_repo.SavePosition(position);
var intent = new Intent();
SetResult(Result.Ok, intent);
Finish();
}
}
private bool Validate(Position position)
{
2010-11-09 15:43:42 +00:00
var result = ValidationRules.Apply();
if (result == string.Empty)
{
2010-11-09 15:43:42 +00:00
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;
}
Toast.MakeText(this, result, ToastLength.Long).Show();
return false;
}
2010-11-09 15:43:42 +00:00
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;
}
}
}
}