working on long click for portfolio list

This commit is contained in:
mgroves 2010-12-18 11:33:29 -05:00
parent c39d0652ab
commit f98d073569
7 changed files with 116 additions and 33 deletions

View file

@ -16,6 +16,11 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
private const int DATABASE_VERSION = 1;
private const string POSITION_TABLE_NAME = "Positions";
public AndroidSqlitePortfolioRepository(SQLiteDatabase db)
{
_db = db;
}
public AndroidSqlitePortfolioRepository(Context context)
{
_dbHelper = new OpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
@ -41,7 +46,7 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
public Portfolio GetPortfolioById(long portfolioId)
{
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" }, " ID = " + portfolioId, null, null, null, null);
if (cursor.Count > 0)
{
cursor.MoveToNext();
@ -59,9 +64,42 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
}
public void DeletePortfolio(Portfolio portfolio)
public void DeletePortfolio(string portfolioName)
{
throw new NotImplementedException();
_db.BeginTransaction();
try
{
var portfolio = GetPortfolioByName(portfolioName);
Log.E("DeletePortfolio", "Name: " + portfolio.Name);
Log.E("DeletePortfolio", "Name: " + portfolio.ID);
_db.RawQuery("DELETE FROM " + PORTFOLIO_TABLE_NAME + " WHERE id = " + portfolio.ID, null);
_db.RawQuery("DELETE FROM " + POSITION_TABLE_NAME + " WHERE ContainingPortfolioID = " + portfolio.ID, null);
//_db.Delete(PORTFOLIO_TABLE_NAME, " WHERE id = " + portfolio.ID, null);
//_db.Delete(POSITION_TABLE_NAME, " WHERE ContainingPortfolioID = " + portfolio.ID, null);
_db.SetTransactionSuccessful();
}
catch (SQLiteException ex)
{
Log.E("DeletePortfolio", "Name = " + portfolioName);
}
finally
{
_db.EndTransaction();
}
}
public Portfolio GetPortfolioByName(string portfolioName)
{
var cursor = _db.Query(PORTFOLIO_TABLE_NAME, new[] { "id", "Name" }, " Name = '" + portfolioName + "'", null, null, null, null);
if (cursor.Count > 0)
{
cursor.MoveToNext();
var portfolio = new Portfolio(cursor.GetLong(0));
portfolio.Name = cursor.GetString(1);
if (!cursor.IsClosed) cursor.Close();
return portfolio;
}
return null;
}
public IList<Position> GetAllPositions(long portfolioId)
@ -124,6 +162,13 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
return positionValues;
}
private class OpenHelper : SQLiteOpenHelper
{
public OpenHelper(Context context, string name, SQLiteDatabase.ICursorFactory factory, int version)

View file

@ -7,9 +7,10 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
{
IList<Entities.Portfolio> GetAllPortfolios();
void SavePortfolio(Entities.Portfolio portfolio);
void DeletePortfolio(Entities.Portfolio portfolio);
void DeletePortfolio(string portfolioName);
IList<Position> GetAllPositions(long portfolioId);
Portfolio GetPortfolioById(long portfolioId);
void SavePosition(Position position);
Portfolio GetPortfolioByName(string portfolioName);
}
}

View file

@ -7,7 +7,8 @@ namespace MonoStockPortfolio.Entities
public class Portfolio
{
public Portfolio() { }
public Portfolio(int id) { ID = id; }
public Portfolio(long id) { ID = id; }
public long? ID { get; private set; }
public string Name { get; set; }

View file

@ -5,6 +5,7 @@ using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.Services;
using MonoStockPortfolio.Entities;
@ -18,12 +19,14 @@ namespace MonoStockPortfolio
private IPortfolioService _svc;
private IList<Portfolio> _portfolios;
private string[] _longClickOptions;
private IPortfolioRepository _repo;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_svc = new PortfolioService(this);
_repo = new AndroidSqlitePortfolioRepository(this);
SetContentView(Resource.layout.main);
@ -52,16 +55,28 @@ namespace MonoStockPortfolio
void PortfolioListView_ItemLongClick(object sender, ItemEventArgs e)
{
_longClickOptions = new[] { "Edit", "Delete" };
_longClickOptions = new[] {"Edit", "Delete"};
var selectedPortfolio = ((TextView) e.View).Text.ToS();
var dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.SetTitle("Options");
dialogBuilder.SetItems(_longClickOptions, tr_LongClick_Options);
dialogBuilder.SetItems(_longClickOptions,
(senderi, ei) => tr_LongClick_Options(senderi, ei, selectedPortfolio));
dialogBuilder.Create().Show();
}
private void tr_LongClick_Options(object sender, DialogClickEventArgs e)
private void tr_LongClick_Options(object sender, DialogClickEventArgs e, string selectedPortfolio)
{
Toast.MakeText(this, "Option: " + _longClickOptions[e.Which], ToastLength.Long).Show();
//Toast.MakeText(this, "Option: " + _longClickOptions[e.Which], ToastLength.Long).Show();
if(_longClickOptions[e.Which] == "Edit")
{
// Edit
}
else if (_longClickOptions[e.Which] == "Delete")
{
// Delete
_repo.DeletePortfolio(selectedPortfolio);
RefreshList();
}
}
private void listView_ItemClick(object sender, ItemEventArgs e)

View file

@ -50,6 +50,7 @@
<Compile Include="AddPositionActivity.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="Validation\FormValidator.cs" />
</ItemGroup>
<ItemGroup>

View file

@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Util;
using Android.Widget;
using MonoStockPortfolio.Core;
using MonoStockPortfolio.Core.Services;
@ -26,38 +26,40 @@ namespace MonoStockPortfolio
{
base.OnCreate(bundle);
try
{
SetContentView(Resource.layout.portfolio);
SetContentView(Resource.layout.portfolio);
_svc = new PortfolioService(this);
_svc = new PortfolioService(this);
Refresh();
Refresh();
WireUpEvents();
WireUpEvents();
SetTitle();
}
catch (Exception ex)
{
Log.E("EXCEPTION", ex.ToString());
Toast.MakeText(this, ex.ToString(), ToastLength.Long);
}
SetTitle();
}
private void Refresh()
{
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
if (tickers.Any())
{
var tableLayout = FindViewById<TableLayout>(Resource.id.quoteTable);
tableLayout.RemoveAllViews();
Action refresh = () =>
{
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
if (tickers.Any())
{
RunOnUiThread(() => RefreshUI(tickers));
}
};
var background = new Thread(() => refresh());
background.Start();
}
WriteTickerHeader(tickers.First());
foreach (var ticker in tickers)
{
WriteTickerRow(ticker);
}
private void RefreshUI(IEnumerable<IDictionary<StockDataItem, string>> tickers)
{
var tableLayout = FindViewById<TableLayout>(Resource.id.quoteTable);
tableLayout.RemoveAllViews();
WriteTickerHeader(tickers.First());
foreach (var ticker in tickers)
{
WriteTickerRow(ticker);
}
}

View file

@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Text;
namespace MonoStockPortfolio
{
public static class StringExtensions
{
public static string ToS(this IEnumerable<char> @this)
{
var sb = new StringBuilder();
foreach (char c in @this)
{
sb.Append(c);
}
return sb.ToString();
}
}
}