mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2024-12-25 11:21:12 +00:00
68 lines
No EOL
2.2 KiB
C#
68 lines
No EOL
2.2 KiB
C#
using System;
|
|
using Android.App;
|
|
using Android.Content;
|
|
using Android.OS;
|
|
using Android.Widget;
|
|
using MonoStockPortfolio.Core.PortfolioRepositories;
|
|
using MonoStockPortfolio.Entities;
|
|
using MonoStockPortfolio.Framework;
|
|
|
|
namespace MonoStockPortfolio.Activites
|
|
{
|
|
[Activity(Label = "Add Position", MainLauncher = false)]
|
|
public partial class AddPositionActivity : Activity
|
|
{
|
|
[IoC] private IPortfolioRepository _repo;
|
|
|
|
protected override void OnCreate(Bundle bundle)
|
|
{
|
|
base.OnCreate(bundle);
|
|
|
|
SetContentView(Resource.layout.addposition);
|
|
|
|
SaveButton.Click += saveButton_Click;
|
|
}
|
|
|
|
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)
|
|
{
|
|
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.ContainingPortfolioID = Intent.GetLongExtra(Extra_PortfolioID, -1);
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
} |