Merge branch 'cleanup_refactoring1' into develop

This commit is contained in:
mgroves 2010-11-09 10:44:43 -05:00
commit 5365be8440
6 changed files with 145 additions and 64 deletions

View file

@ -11,7 +11,11 @@ namespace MonoStockPortfolio
[Activity(Label = "Add Portfolio", MainLauncher = false)]
public class AddPortfolioActivity : Activity
{
public AddPortfolioActivity(IntPtr handle) : base(handle) { }
public AddPortfolioActivity(IntPtr handle) : base(handle)
{
_repo = new AndroidSqlitePortfolioRepository(this);
}
public static string ClassName { get { return "monoStockPortfolio.AddPortfolioActivity"; } }
private IPortfolioRepository _repo;
@ -21,17 +25,20 @@ namespace MonoStockPortfolio
SetContentView(Resource.layout.addportfolio);
WireUpEvents();
}
private void WireUpEvents()
{
var saveButton = FindViewById<Button>(Resource.id.btnSave);
saveButton.Click += saveButton_Click;
_repo = new AndroidSqlitePortfolioRepository(this);
}
private void saveButton_Click(object sender, EventArgs e)
{
var portfolioName = FindViewById<EditText>(Resource.id.portfolioName);
_repo.SavePortfolio(new Portfolio() {Name = portfolioName.Text.ToString()});
_repo.SavePortfolio(new Portfolio {Name = portfolioName.Text.ToString()});
Toast.MakeText(this, "You saved: " + portfolioName.Text, ToastLength.Short).Show();

View file

@ -4,35 +4,43 @@ using Android.Content;
using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.Services;
using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Validation;
namespace MonoStockPortfolio
{
[Activity(Label = "Add Position", MainLauncher = false)]
public class AddPositionActivity : Activity
{
public AddPositionActivity(IntPtr handle) : base(handle) { }
public AddPositionActivity(IntPtr handle) : base(handle)
{
_repo = new AndroidSqlitePortfolioRepository(this);
}
public static string ClassName { get { return "monoStockPortfolio.AddPositionActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
private IPortfolioRepository _repo;
private EditText TickerTextBox { get { return FindViewById<EditText>(Resource.id.addPositionTicker); } }
private EditText PriceTextBox { get { return FindViewById<EditText>(Resource.id.addPositionPrice); } }
private EditText SharesTextBox { get { return FindViewById<EditText>(Resource.id.addPositionShares); } }
private Button SaveButton { get { return FindViewById<Button>(Resource.id.addPositionSaveButton); } }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.layout.addposition);
var saveButton = FindViewById<Button>(Resource.id.addPositionSaveButton);
saveButton.Click += saveButton_Click;
SaveButton.Click += saveButton_Click;
}
void saveButton_Click(object sender, System.EventArgs e)
void saveButton_Click(object sender, EventArgs e)
{
var position = new Position();
if(Validate(position))
{
_repo = new AndroidSqlitePortfolioRepository(this);
_repo.SavePosition(position);
var intent = new Intent();
@ -43,49 +51,31 @@ namespace MonoStockPortfolio
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);
var result = ValidationRules.Apply();
string errorMessage = string.Empty;
if (string.IsNullOrEmpty(tickerTextBox.Text.ToString()))
if (result == string.Empty)
{
errorMessage += "Please enter a ticker\n";
}
decimal dummy;
if (string.IsNullOrEmpty(sharesTextBox.Text.ToString()))
{
errorMessage += "Please enter the number of shares you bought\n";
}
else if (!decimal.TryParse(sharesTextBox.Text.ToString(), out dummy) ||
decimal.Parse(sharesTextBox.Text.ToString()) <= 0)
{
errorMessage += "Please enter a valid number of shares";
}
if (string.IsNullOrEmpty(priceTextBox.Text.ToString()))
{
errorMessage += "Please enter the price you bought these shares at";
}
else if (!decimal.TryParse(priceTextBox.Text.ToString(), out dummy) ||
decimal.Parse(priceTextBox.Text.ToString()) <= 0)
{
errorMessage += "Please enter a valid price";
}
if (errorMessage == string.Empty)
{
position.Shares = decimal.Parse(sharesTextBox.Text.ToString());
position.PricePerShare = decimal.Parse(priceTextBox.Text.ToString());
position.Ticker = tickerTextBox.Text.ToString();
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, errorMessage, ToastLength.Long).Show();
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;
}
}
}
}

View file

@ -14,7 +14,10 @@ namespace MonoStockPortfolio
public class MainActivity : Activity
{
public static string ClassName { get { return "monoStockPortfolio.MainActivity"; } }
public MainActivity(IntPtr handle) : base(handle) { }
public MainActivity(IntPtr handle) : base(handle)
{
_svc = new PortfolioService(this);
}
private IPortfolioService _svc;
private IList<Portfolio> _portfolios;
@ -35,7 +38,6 @@ namespace MonoStockPortfolio
private void RefreshList()
{
_svc = new PortfolioService(this);
_portfolios = _svc.GetAllPortfolios();
var listAdapter = new ArrayAdapter<string>(this, Android.R.Layout.SimpleListItem1, _portfolios.Select(p => p.Name).ToList());

View file

@ -47,6 +47,7 @@
<Compile Include="AddPositionActivity.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Validation\FormValidator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />

View file

@ -32,13 +32,11 @@ namespace MonoStockPortfolio
SetContentView(Resource.layout.portfolio);
var addPositionButton = FindViewById<Button>(Resource.id.btnAddPosition);
addPositionButton.Click += addPositionButton_Click;
var portfolio = _svc.GetPortolioById(ThisPortofolioId);
this.Title = "Portfolio: " + portfolio.Name;
Refresh();
WireUpEvents();
SetTitle();
}
private void Refresh()
@ -59,6 +57,18 @@ namespace MonoStockPortfolio
}
}
private void WireUpEvents()
{
var addPositionButton = FindViewById<Button>(Resource.id.btnAddPosition);
addPositionButton.Click += addPositionButton_Click;
}
private void SetTitle()
{
var portfolio = _svc.GetPortolioById(ThisPortofolioId);
this.Title = "Portfolio: " + portfolio.Name;
}
private List<StockDataItem> GetStockItemsFromConfig()
{
// TODO: load this from a config
@ -130,16 +140,7 @@ namespace MonoStockPortfolio
void tr_LongClick(object sender, Android.Views.View.LongClickEventArgs e)
{
longClickOptions = new IList<char>[] {"Edit".ToCharArray(), "Delete".ToCharArray()};
var dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.SetTitle("Options");
dialogBuilder.SetItems(longClickOptions, tr_LongClick_Options);
dialogBuilder.Create().Show();
}
private void tr_LongClick_Options(object sender, DialogClickEventArgs e)
{
Toast.MakeText(this, "Option: " + longClickOptions[e.Which], ToastLength.Long).Show();
Toast.MakeText(this, "TODO!", ToastLength.Long);
}
}
}

View file

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using Android.Widget;
namespace MonoStockPortfolio.Validation
{
public class FormValidator
{
private IList<KeyValuePair<EditText, Func<string>>> _dict;
public FormValidator()
{
_dict = new List<KeyValuePair<EditText, Func<string>>>();
}
public void AddValidation(EditText control, Func<string> validationFunction)
{
_dict.Add(new KeyValuePair<EditText, Func<string>>(control, validationFunction));
}
public void AddRequired(EditText control, string errorMessage)
{
AddValidation(control, () => Required(control, errorMessage));
}
public void AddValidDecimal(EditText control, string errorMessage)
{
AddValidation(control, () => ValidDecimal(control, errorMessage));
}
public void AddValidPositiveDecimal(EditText control, string errorMessage)
{
AddValidation(control, () => ValidPositiveDecimal(control, errorMessage));
}
public string Apply()
{
var errorMessage = string.Empty;
foreach (var keyValuePair in _dict)
{
var result = keyValuePair.Value();
errorMessage += keyValuePair.Value();
if(result != string.Empty)
{
errorMessage += "\n";
}
}
return errorMessage;
}
#region Validation Functions
private static string Required(EditText control, string errorMessage)
{
if (string.IsNullOrEmpty(control.Text.ToString()))
{
return errorMessage;
}
return string.Empty;
}
private static string ValidDecimal(EditText control, string errorMessage)
{
var test = control.Text.ToString();
decimal dummy;
if(!decimal.TryParse(test, out dummy))
{
return errorMessage;
}
return string.Empty;
}
private static string ValidPositiveDecimal(EditText control, string errorMessage)
{
if(ValidDecimal(control, errorMessage) == string.Empty)
{
var val = decimal.Parse(control.Text.ToString());
if (val >= 0) return string.Empty;
}
return errorMessage;
}
#endregion
}
}