created 'add position' form, validation, and a button to get to it from the main portfolio screen

This commit is contained in:
mgroves 2010-11-08 11:08:02 -05:00
parent e4ed0c1ce3
commit 7c71fa0f0d
7 changed files with 173 additions and 10 deletions

View file

@ -9,6 +9,6 @@ namespace MonoStockPortfolio.Entities
public string Ticker { get; set; }
public decimal Shares { get; set; }
public decimal PricePerShare { get; set; }
public int ContainingPortfolioID { get; set; }
public long ContainingPortfolioID { get; set; }
}
}

View file

@ -0,0 +1,84 @@
using System;
using Android.App;
using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Entities;
namespace MonoStockPortfolio
{
[Activity(Label = "Add Position", MainLauncher = false)]
public class AddPositionActivity : Activity
{
public AddPositionActivity(IntPtr handle) : base(handle) { }
public static string ClassName { get { return "monoStockPortfolio.AddPositionActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.layout.addposition);
var saveButton = FindViewById<Button>(Resource.id.addPositionSaveButton);
saveButton.Click += saveButton_Click;
}
void saveButton_Click(object sender, System.EventArgs e)
{
var position = new Position();
if(Validate(position))
{
// save it
// go back
Toast.MakeText(this, "Saved!", ToastLength.Long).Show();
}
}
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);
string errorMessage = string.Empty;
if (string.IsNullOrEmpty(tickerTextBox.Text.ToString()))
{
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.ContainingPortfolioID = Intent.GetLongExtra(Extra_PortfolioID, -1);
return true;
}
Toast.MakeText(this, errorMessage, ToastLength.Long).Show();
return false;
}
}
}

View file

@ -44,6 +44,7 @@
<Compile Include="AddPortfolioActivity.cs" />
<Compile Include="PortfolioActivity.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="AddPositionActivity.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@ -86,6 +87,11 @@
<Name>MonoStockPortfolio.Entities</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\addposition.xml">
<SubType>Designer</SubType>
</AndroidResource>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View file

@ -21,23 +21,26 @@ namespace MonoStockPortfolio
private IPortfolioService _svc;
private IEnumerable<char>[] longClickOptions;
private long ThisPortofolioId { get { return Intent.GetLongExtra(Extra_PortfolioID, -1); } }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.layout.portfolio);
var portfolioId = Intent.GetLongExtra(Extra_PortfolioID, -1);
var addPositionButton = FindViewById<Button>(Resource.id.btnAddPosition);
addPositionButton.Click += addPositionButton_Click;
_svc = new PortfolioService(this);
var portfolio = _svc.GetPortolioById(portfolioId);
var portfolio = _svc.GetPortolioById(ThisPortofolioId);
this.Title = "Portfolio: " + portfolio.Name;
var items = new List<StockDataItem>();
items.Add(StockDataItem.Ticker);
items.Add(StockDataItem.LastTradePrice);
var tickers = _svc.GetDetailedItems(portfolioId, items);
var tickers = _svc.GetDetailedItems(ThisPortofolioId, items);
if (tickers.Any())
{
@ -49,6 +52,14 @@ namespace MonoStockPortfolio
}
}
void addPositionButton_Click(object sender, EventArgs e)
{
var intent = new Intent();
intent.SetClassName(this, AddPositionActivity.ClassName);
intent.PutExtra(AddPositionActivity.Extra_PortfolioID, ThisPortofolioId);
StartActivityForResult(intent, 0);
}
private void WriteTickerHeader(IDictionary<StockDataItem, string> ticker)
{
var tr = new TableRow(this);

View file

@ -41,10 +41,13 @@ namespace MonoStockPortfolio
public const int addportfolio = 2130903040;
// aapt resource value: 0x7f030001
public const int main = 2130903041;
public const int addposition = 2130903041;
// aapt resource value: 0x7f030002
public const int portfolio = 2130903042;
public const int main = 2130903042;
// aapt resource value: 0x7f030003
public const int portfolio = 2130903043;
private layout()
{
@ -75,13 +78,28 @@ namespace MonoStockPortfolio
public const int btnSave = 2131034113;
// aapt resource value: 0x7f050002
public const int btnAddPortfolio = 2131034114;
public const int addPositionTicker = 2131034114;
// aapt resource value: 0x7f050003
public const int portfolioList = 2131034115;
public const int addPositionShares = 2131034115;
// aapt resource value: 0x7f050004
public const int quoteTable = 2131034116;
public const int addPositionPrice = 2131034116;
// aapt resource value: 0x7f050005
public const int addPositionSaveButton = 2131034117;
// aapt resource value: 0x7f050006
public const int btnAddPortfolio = 2131034118;
// aapt resource value: 0x7f050007
public const int portfolioList = 2131034119;
// aapt resource value: 0x7f050008
public const int btnAddPosition = 2131034120;
// aapt resource value: 0x7f050009
public const int quoteTable = 2131034121;
private id()
{

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Ticker" />
<EditText android:id="@+id/addPositionTicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="# of shares" />
<EditText android:id="@+id/addPositionShares"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Price per share" />
<EditText android:id="@+id/addPositionPrice"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:text="Save" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/addPositionSaveButton" />
</LinearLayout>
</LinearLayout>

View file

@ -7,10 +7,15 @@
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<Button android:id="@+id/btnAddPosition"
android:text="Add Position"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<TableLayout
android:id="@+id/quoteTable"
android:layout_width="fill_parent"