2010-11-02 16:26:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Android.App;
|
|
|
|
|
using Android.Content;
|
|
|
|
|
using Android.OS;
|
|
|
|
|
using Android.Widget;
|
2010-12-18 16:33:29 +00:00
|
|
|
|
using MonoStockPortfolio.Core.PortfolioRepositories;
|
2010-11-02 16:26:36 +00:00
|
|
|
|
using MonoStockPortfolio.Core.Services;
|
|
|
|
|
using MonoStockPortfolio.Entities;
|
2010-12-27 04:18:10 +00:00
|
|
|
|
using MonoStockPortfolio.Framework;
|
2010-11-02 16:26:36 +00:00
|
|
|
|
|
2010-12-27 04:18:10 +00:00
|
|
|
|
namespace MonoStockPortfolio.Activites
|
2010-11-02 16:26:36 +00:00
|
|
|
|
{
|
2010-11-08 19:03:01 +00:00
|
|
|
|
[Activity(Label = "Stock Portfolio", MainLauncher = true, Icon = "@drawable/icon")]
|
2010-12-27 02:47:10 +00:00
|
|
|
|
public partial class MainActivity : Activity
|
2010-11-02 16:26:36 +00:00
|
|
|
|
{
|
2010-12-19 20:34:20 +00:00
|
|
|
|
[IoC] private IPortfolioService _svc;
|
|
|
|
|
[IoC] private IPortfolioRepository _repo;
|
|
|
|
|
|
2010-11-02 16:26:36 +00:00
|
|
|
|
private IList<Portfolio> _portfolios;
|
|
|
|
|
|
|
|
|
|
protected override void OnCreate(Bundle bundle)
|
|
|
|
|
{
|
|
|
|
|
base.OnCreate(bundle);
|
|
|
|
|
|
|
|
|
|
SetContentView(Resource.layout.main);
|
|
|
|
|
|
|
|
|
|
RefreshList();
|
|
|
|
|
|
|
|
|
|
WireUpEvents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RefreshList()
|
|
|
|
|
{
|
|
|
|
|
_portfolios = _svc.GetAllPortfolios();
|
|
|
|
|
|
|
|
|
|
var listAdapter = new ArrayAdapter<string>(this, Android.R.Layout.SimpleListItem1, _portfolios.Select(p => p.Name).ToList());
|
|
|
|
|
PortfolioListView.Adapter = listAdapter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WireUpEvents()
|
|
|
|
|
{
|
|
|
|
|
AddPortfolioButton.Click += addPortfolioButton_Click;
|
|
|
|
|
PortfolioListView.ItemClick += listView_ItemClick;
|
2010-12-27 04:04:42 +00:00
|
|
|
|
RegisterForContextMenu(PortfolioListView);
|
2010-11-02 16:26:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-12-27 04:04:42 +00:00
|
|
|
|
public override void OnCreateContextMenu(Android.Views.IContextMenu menu, Android.Views.View v, Android.Views.IContextMenuContextMenuInfo menuInfo)
|
2010-12-09 03:13:35 +00:00
|
|
|
|
{
|
2010-12-27 04:04:42 +00:00
|
|
|
|
base.OnCreateContextMenu(menu, v, menuInfo);
|
|
|
|
|
|
|
|
|
|
var info = (AdapterView.AdapterContextMenuInfo)menuInfo;
|
|
|
|
|
var selectedPortfolioName = ((TextView)info.TargetView).Text.ToS();
|
2010-12-18 19:39:57 +00:00
|
|
|
|
var selectedPortfolio = _repo.GetPortfolioByName(selectedPortfolioName);
|
2010-12-27 04:04:42 +00:00
|
|
|
|
var id = (int)(selectedPortfolio.ID ?? -1);
|
|
|
|
|
|
|
|
|
|
menu.SetHeaderTitle("Options");
|
|
|
|
|
menu.Add(0, id, 1, "Rename");
|
|
|
|
|
menu.Add(0, id, 2, "Delete");
|
2010-12-09 03:13:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-12-27 04:04:42 +00:00
|
|
|
|
public override bool OnContextItemSelected(Android.Views.IMenuItem item)
|
2010-12-09 03:13:35 +00:00
|
|
|
|
{
|
2010-12-27 04:04:42 +00:00
|
|
|
|
if (item.Title.ToS() == "Rename")
|
2010-12-18 16:33:29 +00:00
|
|
|
|
{
|
|
|
|
|
// Edit
|
2010-12-18 19:39:57 +00:00
|
|
|
|
var intent = new Intent();
|
|
|
|
|
intent.SetClassName(this, EditPortfolioActivity.ClassName);
|
2010-12-27 04:04:42 +00:00
|
|
|
|
intent.PutExtra(EditPortfolioActivity.Extra_PortfolioID, (long)item.ItemId);
|
2010-12-18 19:39:57 +00:00
|
|
|
|
StartActivityForResult(intent, 0);
|
2010-12-27 04:04:42 +00:00
|
|
|
|
return true;
|
2010-12-18 16:33:29 +00:00
|
|
|
|
}
|
2010-12-27 04:04:42 +00:00
|
|
|
|
if (item.Title.ToS() == "Delete")
|
2010-12-18 16:33:29 +00:00
|
|
|
|
{
|
|
|
|
|
// Delete
|
2010-12-27 04:04:42 +00:00
|
|
|
|
_repo.DeletePortfolioById(item.ItemId);
|
2010-12-18 16:33:29 +00:00
|
|
|
|
RefreshList();
|
2010-12-27 04:04:42 +00:00
|
|
|
|
return true;
|
2010-12-18 16:33:29 +00:00
|
|
|
|
}
|
2010-12-27 04:04:42 +00:00
|
|
|
|
return base.OnContextItemSelected(item);
|
2010-12-09 03:13:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-02 16:26:36 +00:00
|
|
|
|
private void listView_ItemClick(object sender, ItemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var intent = new Intent();
|
|
|
|
|
intent.SetClassName(this, PortfolioActivity.ClassName);
|
|
|
|
|
intent.PutExtra(PortfolioActivity.Extra_PortfolioID, _portfolios[e.Position].ID ?? -1);
|
|
|
|
|
StartActivityForResult(intent, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addPortfolioButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var intent = new Intent();
|
2010-12-18 19:39:57 +00:00
|
|
|
|
intent.SetClassName(this, EditPortfolioActivity.ClassName);
|
2010-11-02 16:26:36 +00:00
|
|
|
|
StartActivityForResult(intent, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
|
|
|
|
|
{
|
|
|
|
|
base.OnActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
|
|
RefreshList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|