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;
}
public void SavePosition(Position position)
{
_db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position));
}
public void DeletePortfolio(Portfolio portfolio)
{
@ -112,6 +117,16 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
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
{
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);
IList<Position> GetAllPositions(long portfolioId);
Portfolio GetPortfolioById(long portfolioId);
void SavePosition(Position position);
}
}

View file

@ -6,7 +6,6 @@ namespace MonoStockPortfolio.Core.Services
public interface IPortfolioService
{
IList<Portfolio> GetAllPortfolios();
IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(Portfolio portfolio, IEnumerable<StockDataItem> items);
IEnumerable<IDictionary<StockDataItem, string>> GetDetailedItems(long portfolioID, IEnumerable<StockDataItem> items);
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)
{
IDictionary<StockDataItem, string> dict = new Dictionary<StockDataItem, string>();
dict.Add(StockDataItem.Ticker, "DENN");
dict.Add(StockDataItem.LastTradePrice, "4.99");
dict.Add(StockDataItem.Time, "3:59pm");
var positions = _portRepo.GetAllPositions(portfolioID);
var tickers = positions.Select(p => p.Ticker);
var stockData = _stockRepo.GetStockQuotes(tickers);
IDictionary<StockDataItem, string> dict2 = new Dictionary<StockDataItem, string>();
dict2.Add(StockDataItem.Ticker, "XIN");
dict2.Add(StockDataItem.LastTradePrice, "3.02");
dict2.Add(StockDataItem.Time, "4:00pm");
var list = new List<IDictionary<StockDataItem, string>>();
list.Add(dict);
list.Add(dict2);
return list;
// 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;
// }
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)
@ -68,24 +53,6 @@ namespace MonoStockPortfolio.Core.Services
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)
{
var dict = new Dictionary<StockDataItem, string>();

View file

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

View file

@ -14,7 +14,10 @@ namespace MonoStockPortfolio
[Activity(Label = "Portfolio")]
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 Extra_PortfolioID { get { return "monoStockPortfolio.PortfolioActivity.PortfolioID"; } }
@ -32,18 +35,20 @@ namespace MonoStockPortfolio
var addPositionButton = FindViewById<Button>(Resource.id.btnAddPosition);
addPositionButton.Click += addPositionButton_Click;
_svc = new PortfolioService(this);
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(ThisPortofolioId, items);
private void Refresh()
{
// TODO: put this in another thread or something, perhaps?
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
if (tickers.Any())
{
var tableLayout = FindViewById<TableLayout>(Resource.id.quoteTable);
tableLayout.RemoveAllViews();
WriteTickerHeader(tickers.First());
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)
{
var intent = new Intent();
@ -60,6 +76,13 @@ namespace MonoStockPortfolio
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)
{
var tr = new TableRow(this);