2010-11-08 16:08:02 +00:00
|
|
|
using System;
|
|
|
|
using Android.App;
|
2010-11-08 18:41:58 +00:00
|
|
|
using Android.Content;
|
2010-11-08 16:08:02 +00:00
|
|
|
using Android.OS;
|
|
|
|
using Android.Widget;
|
2010-11-08 18:41:58 +00:00
|
|
|
using MonoStockPortfolio.Core.PortfolioRepositories;
|
2010-11-08 16:08:02 +00:00
|
|
|
using MonoStockPortfolio.Entities;
|
2010-11-09 15:25:58 +00:00
|
|
|
using MonoStockPortfolio.Validation;
|
2010-11-08 16:08:02 +00:00
|
|
|
|
|
|
|
namespace MonoStockPortfolio
|
|
|
|
{
|
|
|
|
[Activity(Label = "Add Position", MainLauncher = false)]
|
2010-12-27 02:47:10 +00:00
|
|
|
public partial class AddPositionActivity : Activity
|
2010-11-08 16:08:02 +00:00
|
|
|
{
|
2010-12-27 02:47:10 +00:00
|
|
|
[IoC] private IPortfolioRepository _repo;
|
2010-11-09 15:43:42 +00:00
|
|
|
|
2010-11-08 16:08:02 +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-08 16:08:02 +00:00
|
|
|
}
|
|
|
|
|
2010-11-09 15:43:42 +00:00
|
|
|
void saveButton_Click(object sender, EventArgs e)
|
2010-11-08 16:08:02 +00:00
|
|
|
{
|
|
|
|
var position = new Position();
|
|
|
|
if(Validate(position))
|
|
|
|
{
|
2010-11-08 18:41:58 +00:00
|
|
|
_repo.SavePosition(position);
|
|
|
|
|
|
|
|
var intent = new Intent();
|
|
|
|
SetResult(Result.Ok, intent);
|
|
|
|
Finish();
|
2010-11-08 16:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool Validate(Position position)
|
|
|
|
{
|
2010-11-09 15:43:42 +00:00
|
|
|
var result = ValidationRules.Apply();
|
2010-11-08 16:08:02 +00:00
|
|
|
|
2010-11-09 15:25:58 +00:00
|
|
|
if (result == string.Empty)
|
2010-11-08 16:08:02 +00:00
|
|
|
{
|
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();
|
2010-11-08 16:08:02 +00:00
|
|
|
position.ContainingPortfolioID = Intent.GetLongExtra(Extra_PortfolioID, -1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-09 15:25:58 +00:00
|
|
|
Toast.MakeText(this, result, ToastLength.Long).Show();
|
2010-11-08 16:08:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2010-11-08 16:08:02 +00:00
|
|
|
}
|
|
|
|
}
|