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;
|
|
|
|
|
|
|
|
|
|
namespace MonoStockPortfolio
|
|
|
|
|
{
|
2010-11-08 19:03:01 +00:00
|
|
|
|
[Activity(Label = "Stock Portfolio", MainLauncher = true, Icon = "@drawable/icon")]
|
2010-11-02 16:26:36 +00:00
|
|
|
|
public class MainActivity : Activity
|
|
|
|
|
{
|
2010-12-09 02:02:03 +00:00
|
|
|
|
public static string ClassName { get { return "monostockportfolio.MainActivity"; } }
|
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;
|
2010-12-09 03:13:35 +00:00
|
|
|
|
private string[] _longClickOptions;
|
2010-11-02 16:26:36 +00:00
|
|
|
|
|
|
|
|
|
protected override void OnCreate(Bundle bundle)
|
|
|
|
|
{
|
|
|
|
|
base.OnCreate(bundle);
|
|
|
|
|
|
|
|
|
|
SetContentView(Resource.layout.main);
|
|
|
|
|
|
|
|
|
|
RefreshList();
|
|
|
|
|
|
|
|
|
|
WireUpEvents();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Button AddPortfolioButton { get { return FindViewById<Button>(Resource.id.btnAddPortfolio); } }
|
|
|
|
|
private ListView PortfolioListView { get { return FindViewById<ListView>(Resource.id.portfolioList); } }
|
|
|
|
|
|
|
|
|
|
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;
|
2010-12-09 03:13:35 +00:00
|
|
|
|
PortfolioListView.ItemLongClick += PortfolioListView_ItemLongClick;
|
2010-11-02 16:26:36 +00:00
|
|
|
|
PortfolioListView.ItemClick += listView_ItemClick;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-09 03:13:35 +00:00
|
|
|
|
void PortfolioListView_ItemLongClick(object sender, ItemEventArgs e)
|
|
|
|
|
{
|
2010-12-18 16:33:29 +00:00
|
|
|
|
_longClickOptions = new[] {"Edit", "Delete"};
|
2010-12-18 19:39:57 +00:00
|
|
|
|
var selectedPortfolioName = ((TextView) e.View).Text.ToS();
|
|
|
|
|
var selectedPortfolio = _repo.GetPortfolioByName(selectedPortfolioName);
|
2010-12-09 03:13:35 +00:00
|
|
|
|
var dialogBuilder = new AlertDialog.Builder(this);
|
|
|
|
|
dialogBuilder.SetTitle("Options");
|
2010-12-18 16:33:29 +00:00
|
|
|
|
dialogBuilder.SetItems(_longClickOptions,
|
2010-12-20 05:04:46 +00:00
|
|
|
|
(senderi, ei) => tr_LongClick_Options(ei, selectedPortfolio));
|
2010-12-09 03:13:35 +00:00
|
|
|
|
dialogBuilder.Create().Show();
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-20 05:04:46 +00:00
|
|
|
|
private void tr_LongClick_Options(DialogClickEventArgs e, Portfolio selectedPortfolio)
|
2010-12-09 03:13:35 +00:00
|
|
|
|
{
|
2010-12-18 16:33:29 +00:00
|
|
|
|
if(_longClickOptions[e.Which] == "Edit")
|
|
|
|
|
{
|
|
|
|
|
// Edit
|
2010-12-18 19:39:57 +00:00
|
|
|
|
var intent = new Intent();
|
|
|
|
|
intent.SetClassName(this, EditPortfolioActivity.ClassName);
|
|
|
|
|
intent.PutExtra(EditPortfolioActivity.Extra_PortfolioID, selectedPortfolio.ID ?? -1);
|
|
|
|
|
StartActivityForResult(intent, 0);
|
2010-12-18 16:33:29 +00:00
|
|
|
|
}
|
|
|
|
|
else if (_longClickOptions[e.Which] == "Delete")
|
|
|
|
|
{
|
|
|
|
|
// Delete
|
2010-12-18 19:39:57 +00:00
|
|
|
|
_repo.DeletePortfolio(selectedPortfolio.Name);
|
2010-12-18 16:33:29 +00:00
|
|
|
|
RefreshList();
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|