MonodroidStockPortfolio/MonoStockPortfolio/Activites/EditPositionActivity.cs

106 lines
3.4 KiB
C#
Raw Normal View History

2011-01-02 21:51:19 +00:00
using System;
using Android.App;
using Android.OS;
using Android.Widget;
2011-01-02 21:51:19 +00:00
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework;
namespace MonoStockPortfolio.Activites
{
[Activity(Label = "Add Position", MainLauncher = false)]
public partial class EditPositionActivity : Activity
{
[IoC] private IPortfolioRepository _repo;
[LazyView(Resource.Id.addPositionTicker)] protected EditText TickerTextBox;
[LazyView(Resource.Id.addPositionPrice)] protected EditText PriceTextBox;
[LazyView(Resource.Id.addPositionShares)] protected EditText SharesTextBox;
[LazyView(Resource.Id.addPositionSaveButton)] protected Button SaveButton;
2011-01-02 21:51:19 +00:00
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
2011-02-06 01:48:55 +00:00
SetContentView(Resource.Layout.addposition);
2011-01-02 21:51:19 +00:00
2011-01-02 22:47:20 +00:00
var positionId = ThisPositionId;
2011-01-02 21:51:19 +00:00
if (positionId != -1)
{
this.Title = "Edit Position";
PopulateForm(positionId);
}
WireUpEvents();
}
private void PopulateForm(long positionId)
{
var position = _repo.GetPositionById(positionId);
this.TickerTextBox.Text = position.Ticker;
this.PriceTextBox.Text = position.PricePerShare.ToString();
this.SharesTextBox.Text = position.Shares.ToString();
}
private void WireUpEvents()
{
SaveButton.Click += saveButton_Click;
}
void saveButton_Click(object sender, EventArgs e)
{
if(Validate())
{
var positionToSave = GetPositionToSave();
_repo.SavePosition(positionToSave);
2011-01-02 22:47:20 +00:00
this.EndActivity();
2011-01-02 21:51:19 +00:00
}
}
private bool Validate()
{
var result = ValidationRules.Apply();
if (result == string.Empty)
{
return true;
}
2011-01-02 22:47:20 +00:00
this.LongToast(result);
2011-01-02 21:51:19 +00:00
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;
}
}
private Position GetPositionToSave()
{
Position positionToSave;
2011-01-02 22:47:20 +00:00
var positionId = ThisPositionId;
2011-01-02 21:51:19 +00:00
if (positionId != -1)
{
positionToSave = new Position(positionId);
}
else
{
positionToSave = new Position();
}
positionToSave.Shares = decimal.Parse(SharesTextBox.Text.ToString());
positionToSave.PricePerShare = decimal.Parse(PriceTextBox.Text.ToString());
positionToSave.Ticker = TickerTextBox.Text.ToString();
2011-01-02 22:47:20 +00:00
positionToSave.ContainingPortfolioID = ThisPortfolioId;
2011-01-02 21:51:19 +00:00
return positionToSave;
}
}
}