refactored to use built-in Context menu stuff instead of my own long-click handling

This commit is contained in:
mgroves 2010-12-26 23:04:42 -05:00
parent a39911f7ff
commit 1f3fcf49e2
3 changed files with 27 additions and 23 deletions

View file

@ -58,19 +58,18 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
_db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position)); _db.Insert(POSITION_TABLE_NAME, null, GetPositionContentValues(position));
} }
public void DeletePortfolio(string portfolioName) public void DeletePortfolioById(int portfolioId)
{ {
_db.BeginTransaction(); _db.BeginTransaction();
try try
{ {
var portfolio = GetPortfolioByName(portfolioName); _db.Delete(PORTFOLIO_TABLE_NAME, "id = " + portfolioId, null);
_db.Delete(PORTFOLIO_TABLE_NAME, "id = " + portfolio.ID, null); _db.Delete(POSITION_TABLE_NAME, "ContainingPortfolioID = " + portfolioId, null);
_db.Delete(POSITION_TABLE_NAME, "ContainingPortfolioID = " + portfolio.ID, null);
_db.SetTransactionSuccessful(); _db.SetTransactionSuccessful();
} }
catch (SQLiteException ex) catch (SQLiteException)
{ {
Log.E("DeletePortfolio", "Name = " + portfolioName); Log.E("DeletePortfolio", "SQLiteException => Id = " + portfolioId);
} }
finally finally
{ {

View file

@ -5,9 +5,9 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
{ {
public interface IPortfolioRepository public interface IPortfolioRepository
{ {
IList<Entities.Portfolio> GetAllPortfolios(); IList<Portfolio> GetAllPortfolios();
void SavePortfolio(Entities.Portfolio portfolio); void SavePortfolio(Portfolio portfolio);
void DeletePortfolio(string portfolioName); void DeletePortfolioById(int portfolioId);
IList<Position> GetAllPositions(long portfolioId); IList<Position> GetAllPositions(long portfolioId);
Portfolio GetPortfolioById(long portfolioId); Portfolio GetPortfolioById(long portfolioId);
void SavePosition(Position position); void SavePosition(Position position);

View file

@ -18,7 +18,6 @@ namespace MonoStockPortfolio
[IoC] private IPortfolioRepository _repo; [IoC] private IPortfolioRepository _repo;
private IList<Portfolio> _portfolios; private IList<Portfolio> _portfolios;
private readonly string[] _longClickOptions = new[] {"Rename", "Delete"};
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
@ -42,37 +41,43 @@ namespace MonoStockPortfolio
private void WireUpEvents() private void WireUpEvents()
{ {
AddPortfolioButton.Click += addPortfolioButton_Click; AddPortfolioButton.Click += addPortfolioButton_Click;
PortfolioListView.ItemLongClick += PortfolioListView_ItemLongClick;
PortfolioListView.ItemClick += listView_ItemClick; PortfolioListView.ItemClick += listView_ItemClick;
RegisterForContextMenu(PortfolioListView);
} }
void PortfolioListView_ItemLongClick(object sender, ItemEventArgs e) public override void OnCreateContextMenu(Android.Views.IContextMenu menu, Android.Views.View v, Android.Views.IContextMenuContextMenuInfo menuInfo)
{ {
var selectedPortfolioName = ((TextView) e.View).Text.ToS(); base.OnCreateContextMenu(menu, v, menuInfo);
var info = (AdapterView.AdapterContextMenuInfo)menuInfo;
var selectedPortfolioName = ((TextView)info.TargetView).Text.ToS();
var selectedPortfolio = _repo.GetPortfolioByName(selectedPortfolioName); var selectedPortfolio = _repo.GetPortfolioByName(selectedPortfolioName);
var dialogBuilder = new AlertDialog.Builder(this); var id = (int)(selectedPortfolio.ID ?? -1);
dialogBuilder.SetTitle("Options");
dialogBuilder.SetItems(_longClickOptions, menu.SetHeaderTitle("Options");
(senderi, ei) => tr_LongClick_Options(ei, selectedPortfolio)); menu.Add(0, id, 1, "Rename");
dialogBuilder.Create().Show(); menu.Add(0, id, 2, "Delete");
} }
private void tr_LongClick_Options(DialogClickEventArgs e, Portfolio selectedPortfolio) public override bool OnContextItemSelected(Android.Views.IMenuItem item)
{ {
if(_longClickOptions[e.Which] == "Rename") if (item.Title.ToS() == "Rename")
{ {
// Edit // Edit
var intent = new Intent(); var intent = new Intent();
intent.SetClassName(this, EditPortfolioActivity.ClassName); intent.SetClassName(this, EditPortfolioActivity.ClassName);
intent.PutExtra(EditPortfolioActivity.Extra_PortfolioID, selectedPortfolio.ID ?? -1); intent.PutExtra(EditPortfolioActivity.Extra_PortfolioID, (long)item.ItemId);
StartActivityForResult(intent, 0); StartActivityForResult(intent, 0);
return true;
} }
else if (_longClickOptions[e.Which] == "Delete") if (item.Title.ToS() == "Delete")
{ {
// Delete // Delete
_repo.DeletePortfolio(selectedPortfolio.Name); _repo.DeletePortfolioById(item.ItemId);
RefreshList(); RefreshList();
return true;
} }
return base.OnContextItemSelected(item);
} }
private void listView_ItemClick(object sender, ItemEventArgs e) private void listView_ItemClick(object sender, ItemEventArgs e)