2010-11-08 11:08:02 -05:00
|
|
|
using System;
|
|
|
|
using Android.App;
|
2010-11-08 13:41:58 -05:00
|
|
|
using Android.Content;
|
2010-11-08 11:08:02 -05:00
|
|
|
using Android.OS;
|
|
|
|
using Android.Widget;
|
2010-11-08 13:41:58 -05:00
|
|
|
using MonoStockPortfolio.Core.PortfolioRepositories;
|
2010-11-08 11:08:02 -05:00
|
|
|
using MonoStockPortfolio.Entities;
|
2010-11-09 10:25:58 -05:00
|
|
|
using MonoStockPortfolio.Validation;
|
2010-11-08 11:08:02 -05:00
|
|
|
|
|
|
|
namespace MonoStockPortfolio
|
|
|
|
{
|
|
|
|
[Activity(Label = "Add Position", MainLauncher = false)]
|
|
|
|
public class AddPositionActivity : Activity
|
|
|
|
{
|
2010-11-09 10:25:58 -05:00
|
|
|
public AddPositionActivity(IntPtr handle) : base(handle)
|
|
|
|
{
|
|
|
|
_repo = new AndroidSqlitePortfolioRepository(this);
|
|
|
|
}
|
|
|
|
|
2010-11-08 11:08:02 -05:00
|
|
|
public static string ClassName { get { return "monoStockPortfolio.AddPositionActivity"; } }
|
|
|
|
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
|
2010-11-08 13:41:58 -05:00
|
|
|
private IPortfolioRepository _repo;
|
2010-11-08 11:08:02 -05:00
|
|
|
|
|
|
|
protected override void OnCreate(Bundle bundle)
|
|
|
|
{
|
|
|
|
base.OnCreate(bundle);
|
|
|
|
|
|
|
|
SetContentView(Resource.layout.addposition);
|
|
|
|
|
|
|
|
var saveButton = FindViewById<Button>(Resource.id.addPositionSaveButton);
|
|
|
|
saveButton.Click += saveButton_Click;
|
|
|
|
}
|
|
|
|
|
|
|
|
void saveButton_Click(object sender, System.EventArgs e)
|
|
|
|
{
|
|
|
|
var position = new Position();
|
|
|
|
if(Validate(position))
|
|
|
|
{
|
2010-11-08 13:41:58 -05:00
|
|
|
_repo.SavePosition(position);
|
|
|
|
|
|
|
|
var intent = new Intent();
|
|
|
|
SetResult(Result.Ok, intent);
|
|
|
|
Finish();
|
2010-11-08 11:08:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2010-11-09 10:25:58 -05:00
|
|
|
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");
|
2010-11-08 11:08:02 -05:00
|
|
|
|
2010-11-09 10:25:58 -05:00
|
|
|
var result = validator.Validate();
|
2010-11-08 11:08:02 -05:00
|
|
|
|
2010-11-09 10:25:58 -05:00
|
|
|
if (result == string.Empty)
|
2010-11-08 11:08:02 -05: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;
|
|
|
|
}
|
|
|
|
|
2010-11-09 10:25:58 -05:00
|
|
|
Toast.MakeText(this, result, ToastLength.Long).Show();
|
2010-11-08 11:08:02 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|