wired up the 'add position' form to sqlite, and also put back in the Yahoo API calls

This commit is contained in:
mgroves 2010-11-08 13:41:58 -05:00
parent 7c71fa0f0d
commit ac337754e0
6 changed files with 69 additions and 57 deletions

View file

@ -56,6 +56,11 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
return null; return null;
} }
public void SavePosition(Position position)
{
_db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position));
}
public void DeletePortfolio(Portfolio portfolio) public void DeletePortfolio(Portfolio portfolio)
{ {
@ -112,6 +117,16 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
return contentValues; return contentValues;
} }
private ContentValues GetPositionContentValues(Position position)
{
var positionValues = new ContentValues();
positionValues.Put("PricePerShare", (double)position.PricePerShare);
positionValues.Put("Ticker", position.Ticker);
positionValues.Put("Shares", (double)position.Shares);
positionValues.Put("ContainingPortfolioID", position.ContainingPortfolioID);
return positionValues;
}
private class OpenHelper : SQLiteOpenHelper private class OpenHelper : SQLiteOpenHelper
{ {
public OpenHelper(Context context, string name, SQLiteDatabase.ICursorFactory factory, int version) public OpenHelper(Context context, string name, SQLiteDatabase.ICursorFactory factory, int version)

View file

@ -10,5 +10,6 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
void DeletePortfolio(Entities.Portfolio portfolio); void DeletePortfolio(Entities.Portfolio portfolio);
IList<Position> GetAllPositions(long portfolioId); IList<Position> GetAllPositions(long portfolioId);
Portfolio GetPortfolioById(long portfolioId); Portfolio GetPortfolioById(long portfolioId);
void SavePosition(Position position);
} }
} }

View file

@ -6,7 +6,6 @@ namespace MonoStockPortfolio.Core.Services
public interface IPortfolioService public interface IPortfolioService
{ {
IList<Portfolio> GetAllPortfolios(); IList<Portfolio> GetAllPortfolios();
IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(Portfolio portfolio, IEnumerable<StockDataItem> items);
IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(long portfolioID, IEnumerable<StockDataItem> items); IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(long portfolioID, IEnumerable<StockDataItem> items);
Portfolio GetPortolioById(long portfolioId); Portfolio GetPortolioById(long portfolioId);
} }

View file

@ -32,35 +32,20 @@ namespace MonoStockPortfolio.Core.Services
public IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(long portfolioID, IEnumerable<StockDataItem> items) public IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(long portfolioID, IEnumerable<StockDataItem> items)
{ {
IDictionary<StockDataItem, string> dict = new Dictionary<StockDataItem, string>(); var positions = _portRepo.GetAllPositions(portfolioID);
dict.Add(StockDataItem.Ticker, "DENN"); var tickers = positions.Select(p => p.Ticker);
dict.Add(StockDataItem.LastTradePrice, "4.99"); var stockData = _stockRepo.GetStockQuotes(tickers);
dict.Add(StockDataItem.Time, "3:59pm");
IDictionary<StockDataItem, string> dict2 = new Dictionary<StockDataItem, string>(); foreach (var position in positions)
dict2.Add(StockDataItem.Ticker, "XIN"); {
dict2.Add(StockDataItem.LastTradePrice, "3.02"); var ticker = position.Ticker;
dict2.Add(StockDataItem.Time, "4:00pm"); var tickerStockData = stockData.Single(stock => stock.Ticker == ticker);
var stockItems = GetStockItems(items, tickerStockData);
var list = new List<IDictionary<StockDataItem, string>>(); var remainingItemsToGet = items.Except(stockItems.Keys);
list.Add(dict); stockItems.AddRange(CalculateItems(remainingItemsToGet, position, tickerStockData));
list.Add(dict2);
return list; yield return stockItems;
}
// var positions = _portRepo.GetAllPositions(portfolioID);
// var tickers = positions.Select(p => p.Ticker);
// var stockData = _stockRepo.GetStockQuotes(tickers);
//
// foreach (var position in positions)
// {
// var ticker = position.Ticker;
// var tickerStockData = stockData.Single(stock => stock.Ticker == ticker);
// var stockItems = GetStockItems(items, tickerStockData);
// var remainingItemsToGet = items.Except(stockItems.Keys);
// stockItems.AddRange(CalculateItems(remainingItemsToGet, position, tickerStockData));
//
// yield return stockItems;
// }
} }
public Portfolio GetPortolioById(long portfolioId) public Portfolio GetPortolioById(long portfolioId)
@ -68,24 +53,6 @@ namespace MonoStockPortfolio.Core.Services
return _portRepo.GetPortfolioById(portfolioId); return _portRepo.GetPortfolioById(portfolioId);
} }
public IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(Portfolio portfolio, IEnumerable<StockDataItem> items)
{
// var tickers = portfolio.Positions.Select(p => p.Ticker);
// var stockData = _stockRepo.GetStockQuotes(tickers);
//
// foreach (var position in portfolio.Positions)
// {
// var ticker = position.Ticker;
// var tickerStockData = stockData.Single(stock => stock.Ticker == ticker);
// var stockItems = GetStockItems(items, tickerStockData);
// var remainingItemsToGet = items.Except(stockItems.Keys);
// stockItems.AddRange(CalculateItems(remainingItemsToGet, position, tickerStockData));
//
// yield return stockItems;
// }
throw new NotImplementedException();
}
private IDictionary<StockDataItem, string> GetStockItems(IEnumerable<StockDataItem> items, StockQuote quote) private IDictionary<StockDataItem, string> GetStockItems(IEnumerable<StockDataItem> items, StockQuote quote)
{ {
var dict = new Dictionary<StockDataItem, string>(); var dict = new Dictionary<StockDataItem, string>();

View file

@ -1,7 +1,10 @@
using System; using System;
using Android.App; using Android.App;
using Android.Content;
using Android.OS; using Android.OS;
using Android.Widget; using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.Services;
using MonoStockPortfolio.Entities; using MonoStockPortfolio.Entities;
namespace MonoStockPortfolio namespace MonoStockPortfolio
@ -12,6 +15,7 @@ namespace MonoStockPortfolio
public AddPositionActivity(IntPtr handle) : base(handle) { } public AddPositionActivity(IntPtr handle) : base(handle) { }
public static string ClassName { get { return "monoStockPortfolio.AddPositionActivity"; } } public static string ClassName { get { return "monoStockPortfolio.AddPositionActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } } public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
private IPortfolioRepository _repo;
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
@ -28,9 +32,12 @@ namespace MonoStockPortfolio
var position = new Position(); var position = new Position();
if(Validate(position)) if(Validate(position))
{ {
// save it _repo = new AndroidSqlitePortfolioRepository(this);
// go back _repo.SavePosition(position);
Toast.MakeText(this, "Saved!", ToastLength.Long).Show();
var intent = new Intent();
SetResult(Result.Ok, intent);
Finish();
} }
} }

View file

@ -14,7 +14,10 @@ namespace MonoStockPortfolio
[Activity(Label = "Portfolio")] [Activity(Label = "Portfolio")]
public class PortfolioActivity : Activity public class PortfolioActivity : Activity
{ {
public PortfolioActivity(IntPtr handle) : base(handle) { } public PortfolioActivity(IntPtr handle) : base(handle)
{
_svc = new PortfolioService(this);
}
public static string ClassName { get { return "monoStockPortfolio.PortfolioActivity"; } } public static string ClassName { get { return "monoStockPortfolio.PortfolioActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.PortfolioActivity.PortfolioID"; } } public static string Extra_PortfolioID { get { return "monoStockPortfolio.PortfolioActivity.PortfolioID"; } }
@ -32,18 +35,20 @@ namespace MonoStockPortfolio
var addPositionButton = FindViewById<Button>(Resource.id.btnAddPosition); var addPositionButton = FindViewById<Button>(Resource.id.btnAddPosition);
addPositionButton.Click += addPositionButton_Click; addPositionButton.Click += addPositionButton_Click;
_svc = new PortfolioService(this);
var portfolio = _svc.GetPortolioById(ThisPortofolioId); var portfolio = _svc.GetPortolioById(ThisPortofolioId);
this.Title = "Portfolio: " + portfolio.Name; this.Title = "Portfolio: " + portfolio.Name;
}
var items = new List<StockDataItem>(); private void Refresh()
items.Add(StockDataItem.Ticker); {
items.Add(StockDataItem.LastTradePrice); // TODO: put this in another thread or something, perhaps?
var tickers = _svc.GetDetailedItems(ThisPortofolioId, items); var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
if (tickers.Any()) if (tickers.Any())
{ {
var tableLayout = FindViewById<TableLayout>(Resource.id.quoteTable);
tableLayout.RemoveAllViews();
WriteTickerHeader(tickers.First()); WriteTickerHeader(tickers.First());
foreach (var ticker in tickers) foreach (var ticker in tickers)
{ {
@ -52,6 +57,17 @@ namespace MonoStockPortfolio
} }
} }
private List<StockDataItem> GetStockItemsFromConfig()
{
// TODO: load this from a config
var items = new List<StockDataItem>();
items.Add(StockDataItem.Ticker);
items.Add(StockDataItem.LastTradePrice);
items.Add(StockDataItem.GainLossRealTime);
items.Add(StockDataItem.Time);
return items;
}
void addPositionButton_Click(object sender, EventArgs e) void addPositionButton_Click(object sender, EventArgs e)
{ {
var intent = new Intent(); var intent = new Intent();
@ -60,6 +76,13 @@ namespace MonoStockPortfolio
StartActivityForResult(intent, 0); StartActivityForResult(intent, 0);
} }
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Refresh();
}
private void WriteTickerHeader(IDictionary<StockDataItem, string> ticker) private void WriteTickerHeader(IDictionary<StockDataItem, string> ticker)
{ {
var tr = new TableRow(this); var tr = new TableRow(this);