mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2025-03-24 17:00:18 -09:00
now pulling config from DB instead of hardcoded list
This commit is contained in:
parent
8de53824b0
commit
f1fc7275f0
5 changed files with 100 additions and 74 deletions
MonoStockPortfolio.Core
MonoStockPortfolio.Entities
46
MonoStockPortfolio.Core/AndroidSqliteBase.cs
Normal file
46
MonoStockPortfolio.Core/AndroidSqliteBase.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
using Android.Content;
|
||||
using Android.Database.Sqlite;
|
||||
using Android.Util;
|
||||
|
||||
namespace MonoStockPortfolio.Core
|
||||
{
|
||||
public abstract class AndroidSqliteBase : SQLiteOpenHelper
|
||||
{
|
||||
public const string PORTFOLIO_TABLE_NAME = "Portfolios";
|
||||
public const string POSITION_TABLE_NAME = "Positions";
|
||||
public const string CONFIG_TABLE_NAME = "Config";
|
||||
|
||||
public const string DATABASE_NAME = "stockportfolio.db";
|
||||
public const int DATABASE_VERSION = 1;
|
||||
|
||||
protected AndroidSqliteBase(Context context)
|
||||
: base(context, DATABASE_NAME, null, DATABASE_VERSION)
|
||||
{
|
||||
}
|
||||
|
||||
protected SQLiteDatabase Db { get { return WritableDatabase; } }
|
||||
|
||||
public override void OnCreate(SQLiteDatabase db)
|
||||
{
|
||||
db.ExecSQL("CREATE TABLE " + PORTFOLIO_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT)");
|
||||
db.ExecSQL("CREATE TABLE " + POSITION_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, Ticker TEXT, Shares REAL, PricePerShare REAL, ContainingPortfolioID INT)");
|
||||
db.ExecSQL("CREATE TABLE " + CONFIG_TABLE_NAME + " (StockItems TEXT)");
|
||||
|
||||
db.ExecSQL("INSERT INTO " + CONFIG_TABLE_NAME + " (StockItems) VALUES ('2,0,1,3')");
|
||||
db.ExecSQL("INSERT INTO " + PORTFOLIO_TABLE_NAME + " (Name) VALUES ('Sample portfolio')");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('GOOG', '500', '593.97', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('AMZN', '500', '180.00', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('AAPL', '500', '322.56', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('MSFT', '500', '27.91', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('NOVL', '500', '5.92', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('S', '500', '4.23', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('VZ', '500', '35.78', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('T', '500', '29.38', 1)");
|
||||
}
|
||||
|
||||
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
|
||||
{
|
||||
Log.W("Upgrade", "Nothing to upgrade");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,36 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Android.Content;
|
||||
using MonoStockPortfolio.Entities;
|
||||
|
||||
namespace MonoStockPortfolio.Core.Config
|
||||
{
|
||||
public class AndroidSqliteConfigRepository : IConfigRepository
|
||||
public class AndroidSqliteConfigRepository : AndroidSqliteBase, IConfigRepository
|
||||
{
|
||||
public AndroidSqliteConfigRepository(Context context) : base(context)
|
||||
{ }
|
||||
|
||||
public IEnumerable<StockDataItem> GetStockItems()
|
||||
{
|
||||
var cursor = Db.Query(CONFIG_TABLE_NAME, new[] { "StockItems" }, null, null, null, null, null);
|
||||
string stockItemsCsv = null;
|
||||
if (cursor.Count > 0)
|
||||
{
|
||||
cursor.MoveToNext();
|
||||
stockItemsCsv = cursor.GetString(0);
|
||||
if (!cursor.IsClosed) cursor.Close();
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(stockItemsCsv))
|
||||
{
|
||||
return DefaultItems();
|
||||
}
|
||||
|
||||
return stockItemsCsv.Split(',').Select(i => (StockDataItem)int.Parse(i));
|
||||
}
|
||||
|
||||
// this should never be called, but it's here anyway in case of some catastrophe
|
||||
private static IEnumerable<StockDataItem> DefaultItems()
|
||||
{
|
||||
var items = new List<StockDataItem>();
|
||||
items.Add(StockDataItem.Ticker);
|
||||
|
|
|
@ -46,6 +46,7 @@
|
|||
<Compile Include="Config\IConfigRepository.cs" />
|
||||
<Compile Include="DictionaryExtensions.cs" />
|
||||
<Compile Include="EnumExtensions.cs" />
|
||||
<Compile Include="AndroidSqliteBase.cs" />
|
||||
<Compile Include="PortfolioRepositories\AndroidSqlitePortfolioRepository.cs" />
|
||||
<Compile Include="PortfolioRepositories\IPortfolioRepository.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
|
|
@ -7,25 +7,15 @@ using MonoStockPortfolio.Entities;
|
|||
|
||||
namespace MonoStockPortfolio.Core.PortfolioRepositories
|
||||
{
|
||||
public class AndroidSqlitePortfolioRepository : IPortfolioRepository
|
||||
public class AndroidSqlitePortfolioRepository : AndroidSqliteBase, IPortfolioRepository
|
||||
{
|
||||
private OpenHelper _dbHelper;
|
||||
private SQLiteDatabase _db;
|
||||
private const string PORTFOLIO_TABLE_NAME = "Portfolios";
|
||||
private const string DATABASE_NAME = "stockportfolio.db";
|
||||
private const int DATABASE_VERSION = 1;
|
||||
private const string POSITION_TABLE_NAME = "Positions";
|
||||
|
||||
public AndroidSqlitePortfolioRepository(Context context)
|
||||
{
|
||||
_dbHelper = new OpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
_db = _dbHelper.WritableDatabase;
|
||||
}
|
||||
public AndroidSqlitePortfolioRepository(Context context) : base(context)
|
||||
{ }
|
||||
|
||||
public IList<Portfolio> GetAllPortfolios()
|
||||
{
|
||||
var list = new List<Portfolio>();
|
||||
var cursor = _db.Query(PORTFOLIO_TABLE_NAME, new[] {"id", "Name"}, null, null, null, null, null);
|
||||
var cursor = Db.Query(PORTFOLIO_TABLE_NAME, new[] { "id", "Name" }, null, null, null, null, null);
|
||||
if(cursor.Count > 0)
|
||||
{
|
||||
while(cursor.MoveToNext())
|
||||
|
@ -41,7 +31,7 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
|
||||
public Portfolio GetPortfolioById(long portfolioId)
|
||||
{
|
||||
var cursor = _db.Query(PORTFOLIO_TABLE_NAME, new[] { "id", "Name" }, " ID = " + portfolioId, null, null, null, null);
|
||||
var cursor = Db.Query(PORTFOLIO_TABLE_NAME, new[] { "id", "Name" }, " ID = " + portfolioId, null, null, null, null);
|
||||
if (cursor.Count > 0)
|
||||
{
|
||||
cursor.MoveToNext();
|
||||
|
@ -67,12 +57,12 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
|
||||
public void DeletePortfolioById(int portfolioId)
|
||||
{
|
||||
_db.BeginTransaction();
|
||||
Db.BeginTransaction();
|
||||
try
|
||||
{
|
||||
_db.Delete(PORTFOLIO_TABLE_NAME, "id = " + portfolioId, null);
|
||||
_db.Delete(POSITION_TABLE_NAME, "ContainingPortfolioID = " + portfolioId, null);
|
||||
_db.SetTransactionSuccessful();
|
||||
Db.Delete(PORTFOLIO_TABLE_NAME, "id = " + portfolioId, null);
|
||||
Db.Delete(POSITION_TABLE_NAME, "ContainingPortfolioID = " + portfolioId, null);
|
||||
Db.SetTransactionSuccessful();
|
||||
}
|
||||
catch (SQLiteException)
|
||||
{
|
||||
|
@ -80,13 +70,13 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
}
|
||||
finally
|
||||
{
|
||||
_db.EndTransaction();
|
||||
Db.EndTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
public Portfolio GetPortfolioByName(string portfolioName)
|
||||
{
|
||||
var cursor = _db.Query(PORTFOLIO_TABLE_NAME, new[] { "id", "Name" }, " Name = '" + portfolioName + "'", null, null, null, null);
|
||||
var cursor = Db.Query(PORTFOLIO_TABLE_NAME, new[] { "id", "Name" }, " Name = '" + portfolioName + "'", null, null, null, null);
|
||||
if (cursor.Count > 0)
|
||||
{
|
||||
cursor.MoveToNext();
|
||||
|
@ -100,14 +90,14 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
|
||||
public void DeletePositionById(long positionId)
|
||||
{
|
||||
_db.Delete(POSITION_TABLE_NAME, "id = " + positionId, null);
|
||||
Db.Delete(POSITION_TABLE_NAME, "id = " + positionId, null);
|
||||
}
|
||||
|
||||
public Position GetPositionById(long positionId)
|
||||
{
|
||||
Position position = null;
|
||||
|
||||
var cursor = _db.Query(POSITION_TABLE_NAME, new[] { "id", "Ticker", "Shares", "PricePerShare" }, " id = " + positionId, null, null, null, null);
|
||||
var cursor = Db.Query(POSITION_TABLE_NAME, new[] { "id", "Ticker", "Shares", "PricePerShare" }, " id = " + positionId, null, null, null, null);
|
||||
if (cursor.Count > 0)
|
||||
{
|
||||
while (cursor.MoveToNext())
|
||||
|
@ -126,7 +116,7 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
{
|
||||
var list = new List<Position>();
|
||||
|
||||
var cursor = _db.Query(POSITION_TABLE_NAME, new[] { "id", "Ticker", "Shares", "PricePerShare" }, " ContainingPortfolioID = " + portfolioId, null, null, null, null);
|
||||
var cursor = Db.Query(POSITION_TABLE_NAME, new[] { "id", "Ticker", "Shares", "PricePerShare" }, " ContainingPortfolioID = " + portfolioId, null, null, null, null);
|
||||
if (cursor.Count > 0)
|
||||
{
|
||||
while (cursor.MoveToNext())
|
||||
|
@ -157,23 +147,23 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
private void UpdateExistingPortfolio(Portfolio portfolio)
|
||||
{
|
||||
var portfolioID = portfolio.ID ?? -1;
|
||||
Log.E("UpdateExistingPortfolio", "Portfolios updated: " + _db.Update(PORTFOLIO_TABLE_NAME, GetPortfolioContentValues(portfolio), "id = " + portfolioID, null));
|
||||
Log.E("UpdateExistingPortfolio", "Portfolios updated: " + Db.Update(PORTFOLIO_TABLE_NAME, GetPortfolioContentValues(portfolio), "id = " + portfolioID, null));
|
||||
}
|
||||
|
||||
private void InsertNewPortfolio(Portfolio portfolio)
|
||||
{
|
||||
Log.E("InsertNewPortfolio", "Portfolios inserted: " + _db.Insert(PORTFOLIO_TABLE_NAME, null, GetPortfolioContentValues(portfolio)));
|
||||
Log.E("InsertNewPortfolio", "Portfolios inserted: " + Db.Insert(PORTFOLIO_TABLE_NAME, null, GetPortfolioContentValues(portfolio)));
|
||||
}
|
||||
|
||||
private void UpdateExistingPosition(Position position)
|
||||
{
|
||||
var positionID = position.ID ?? -1;
|
||||
Log.E("UpdateExistingPosition", "Positions updated: " + _db.Update(POSITION_TABLE_NAME, GetPositionContentValues(position), "id = " + positionID, null));
|
||||
Log.E("UpdateExistingPosition", "Positions updated: " + Db.Update(POSITION_TABLE_NAME, GetPositionContentValues(position), "id = " + positionID, null));
|
||||
}
|
||||
|
||||
private void InsertNewPosition(Position position)
|
||||
{
|
||||
Log.E("InsertNewPosition", "Positions inserted: " + _db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position)));
|
||||
Log.E("InsertNewPosition", "Positions inserted: " + Db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position)));
|
||||
}
|
||||
|
||||
private static ContentValues GetPortfolioContentValues(Portfolio portfolio)
|
||||
|
@ -192,41 +182,5 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
|
|||
positionValues.Put("ContainingPortfolioID", position.ContainingPortfolioID);
|
||||
return positionValues;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private class OpenHelper : SQLiteOpenHelper
|
||||
{
|
||||
public OpenHelper(Context context, string name, SQLiteDatabase.ICursorFactory factory, int version)
|
||||
: base(context, name, factory, version)
|
||||
{
|
||||
}
|
||||
|
||||
public override void OnCreate(SQLiteDatabase db)
|
||||
{
|
||||
db.ExecSQL("CREATE TABLE " + PORTFOLIO_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT)");
|
||||
db.ExecSQL("CREATE TABLE " + POSITION_TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, Ticker TEXT, Shares REAL, PricePerShare REAL, ContainingPortfolioID INT)");
|
||||
|
||||
db.ExecSQL("INSERT INTO " + PORTFOLIO_TABLE_NAME + " (Name) VALUES ('Sample portfolio')");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('GOOG', '500', '593.97', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('AMZN', '500', '180.00', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('AAPL', '500', '322.56', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('MSFT', '500', '27.91', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('NOVL', '500', '5.92', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('S', '500', '4.23', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('VZ', '500', '35.78', 1)");
|
||||
db.ExecSQL("INSERT INTO " + POSITION_TABLE_NAME + " (Ticker, Shares, PricePerShare, ContainingPortfolioID) VALUES ('T', '500', '29.38', 1)");
|
||||
}
|
||||
|
||||
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
|
||||
{
|
||||
Log.W("Upgrade", "Nothing to upgrade");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,22 +3,22 @@ namespace MonoStockPortfolio.Entities
|
|||
public enum StockDataItem
|
||||
{
|
||||
[StringValue("Change")]
|
||||
Change,
|
||||
Change = 0,
|
||||
[StringValue("Gain/Loss")]
|
||||
GainLoss,
|
||||
GainLoss = 1,
|
||||
[StringValue("Ticker")]
|
||||
Ticker,
|
||||
Ticker = 2,
|
||||
[StringValue("Time")]
|
||||
Time,
|
||||
Time = 3,
|
||||
[StringValue("Volume")]
|
||||
Volume,
|
||||
Volume = 4,
|
||||
[StringValue("Price")]
|
||||
LastTradePrice,
|
||||
LastTradePrice = 5,
|
||||
[StringValue("Price-RT")]
|
||||
RealTimeLastTradeWithTime,
|
||||
RealTimeLastTradeWithTime = 6,
|
||||
[StringValue("Change-RT")]
|
||||
ChangeRealTime,
|
||||
ChangeRealTime = 7,
|
||||
[StringValue("Gain/Loss-RT")]
|
||||
GainLossRealTime
|
||||
GainLossRealTime = 8
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue