mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2025-01-12 03:00:20 +00:00
refactored the validation to be more usuable (user) and more REusuable (dev)
This commit is contained in:
parent
1a00d0bf60
commit
72029335c5
6 changed files with 126 additions and 53 deletions
|
@ -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();
|
||||
|
||||
|
|
|
@ -4,15 +4,19 @@ 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;
|
||||
|
@ -32,7 +36,6 @@ namespace MonoStockPortfolio
|
|||
var position = new Position();
|
||||
if(Validate(position))
|
||||
{
|
||||
_repo = new AndroidSqlitePortfolioRepository(this);
|
||||
_repo.SavePosition(position);
|
||||
|
||||
var intent = new Intent();
|
||||
|
@ -47,35 +50,14 @@ namespace MonoStockPortfolio
|
|||
var priceTextBox = FindViewById<EditText>(Resource.id.addPositionPrice);
|
||||
var sharesTextBox = FindViewById<EditText>(Resource.id.addPositionShares);
|
||||
|
||||
string errorMessage = string.Empty;
|
||||
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");
|
||||
|
||||
if (string.IsNullOrEmpty(tickerTextBox.Text.ToString()))
|
||||
{
|
||||
errorMessage += "Please enter a ticker\n";
|
||||
}
|
||||
var result = validator.Validate();
|
||||
|
||||
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)
|
||||
if (result == string.Empty)
|
||||
{
|
||||
position.Shares = decimal.Parse(sharesTextBox.Text.ToString());
|
||||
position.PricePerShare = decimal.Parse(priceTextBox.Text.ToString());
|
||||
|
@ -84,7 +66,7 @@ namespace MonoStockPortfolio
|
|||
return true;
|
||||
}
|
||||
|
||||
Toast.MakeText(this, errorMessage, ToastLength.Long).Show();
|
||||
Toast.MakeText(this, result, ToastLength.Long).Show();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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" />
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
80
MonoStockPortfolio/Validation/FormValidator.cs
Normal file
80
MonoStockPortfolio/Validation/FormValidator.cs
Normal 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 Validate()
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue