MonodroidStockPortfolio/MonoStockPortfolio/Activites/EditPositionActivity.cs

140 lines
4.9 KiB
C#
Raw Normal View History

2011-01-02 21:51:19 +00:00
using System;
using Android.App;
using Android.Content;
2011-01-02 21:51:19 +00:00
using Android.OS;
using Android.Widget;
2011-01-02 21:51:19 +00:00
using MonoStockPortfolio.Core.PortfolioRepositories;
2011-02-26 16:27:52 +00:00
using MonoStockPortfolio.Core.StockData;
2011-01-02 21:51:19 +00:00
using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework;
namespace MonoStockPortfolio.Activites
{
[Activity(Label = "Add Position", MainLauncher = false, Name = "monostockportfolio.activites.EditPositionActivity")]
public class EditPositionActivity : Activity
2011-01-02 21:51:19 +00:00
{
[IoC] private IPortfolioRepository _repo;
2011-02-26 16:27:52 +00:00
[IoC] private IStockDataProvider _svc;
2011-01-02 21:51:19 +00:00
[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;
private const string POSITIONIDEXTRA = "monoStockPortfolio.EditPositionActivity.PositionID";
private const string PORTFOLIOIDEXTRA = "monoStockPortfolio.EditPositionActivity.PortfolioID";
public static Intent AddIntent(Context context, long portfolioId)
{
var intent = new Intent();
intent.SetClassName(context, ManifestNames.GetName<EditPositionActivity>());
intent.PutExtra(PORTFOLIOIDEXTRA, portfolioId);
return intent;
}
public static Intent EditIntent(Context context, long positionId, long portfolioId)
{
var intent = new Intent();
intent.SetClassName(context, ManifestNames.GetName<EditPositionActivity>());
intent.PutExtra(POSITIONIDEXTRA, positionId);
intent.PutExtra(PORTFOLIOIDEXTRA, portfolioId);
return intent;
}
public long ThisPortfolioId { get { return Intent.GetLongExtra(PORTFOLIOIDEXTRA, -1); } }
public long ThisPositionId { get { return Intent.GetLongExtra(POSITIONIDEXTRA, -1); } }
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");
2011-02-26 16:27:52 +00:00
validator.AddValidation(TickerTextBox, () => ValidateTicker(TickerTextBox.Text));
2011-01-02 21:51:19 +00:00
return validator;
}
}
2011-02-26 16:27:52 +00:00
private string ValidateTicker(string ticker)
{
if(_svc.IsValidTicker(ticker))
{
return string.Empty;
}
return "Invalid Ticker Name";
}
2011-01-02 21:51:19 +00:00
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();
}
2011-02-26 16:30:41 +00:00
positionToSave.Shares = decimal.Parse(SharesTextBox.Text);
positionToSave.PricePerShare = decimal.Parse(PriceTextBox.Text);
positionToSave.Ticker = TickerTextBox.Text.ToUpper();
2011-01-02 22:47:20 +00:00
positionToSave.ContainingPortfolioID = ThisPortfolioId;
2011-01-02 21:51:19 +00:00
return positionToSave;
}
}
}