MonodroidStockPortfolio/MonoStockPortfolio/Activites/PortfolioActivity.cs

213 lines
7.5 KiB
C#
Raw Normal View History

2010-11-02 16:26:36 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
2010-11-02 16:26:36 +00:00
using Android.App;
using Android.Content;
using Android.OS;
2010-12-27 03:25:52 +00:00
using Android.Views;
2010-11-02 16:26:36 +00:00
using Android.Widget;
using MonoStockPortfolio.Core;
2011-01-02 21:09:39 +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
{
[Activity(Label = "Portfolio")]
public partial class PortfolioActivity : Activity
2010-11-02 16:26:36 +00:00
{
[IoC] private IPortfolioService _svc;
2011-01-02 21:09:39 +00:00
[IoC] private IPortfolioRepository _repo;
2010-11-02 16:26:36 +00:00
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.layout.portfolio);
Refresh();
2010-11-02 16:26:36 +00:00
WireUpEvents();
SetTitle();
}
2010-11-02 16:26:36 +00:00
public override bool OnCreateOptionsMenu(IMenu menu)
{
var item = menu.Add(0,1,1,"Refresh");
item.SetIcon(Resource.drawable.ic_menu_refresh);
return true;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.Title.ToS())
{
case "Refresh": Refresh();
return true;
default:
return base.OnOptionsItemSelected(item);
}
}
public override void OnCreateContextMenu(IContextMenu menu, View v, IContextMenuContextMenuInfo menuInfo)
{
base.OnCreateContextMenu(menu, v, menuInfo);
var info = (AdapterView.AdapterContextMenuInfo)menuInfo;
var selectedPositionId = int.Parse(info.TargetView.Tag.ToString());
menu.SetHeaderTitle("Options");
menu.Add(0, selectedPositionId, 1, "Edit");
menu.Add(0, selectedPositionId, 2, "Delete");
}
public override bool OnContextItemSelected(IMenuItem item)
{
if (item.Title.ToS() == "Edit")
{
2011-01-02 21:09:39 +00:00
// Edit
2011-01-02 21:51:19 +00:00
var intent = new Intent();
intent.SetClassName(this, EditPositionActivity.ClassName);
intent.PutExtra(EditPositionActivity.Extra_PositionID, (long)item.ItemId);
intent.PutExtra(EditPositionActivity.Extra_PortfolioID, ThisPortofolioId);
StartActivityForResult(intent, 0);
return true;
}
2011-01-02 21:51:19 +00:00
if (item.Title.ToS() == "Delete")
{
2011-01-02 21:09:39 +00:00
// Delete
_repo.DeletePositionById(item.ItemId);
Refresh();
return true;
}
return base.OnContextItemSelected(item);
}
private void Refresh()
{
2010-12-27 03:25:52 +00:00
var pd = new ProgressDialog(this);
pd.SetMessage("Loading...Please wait...");
pd.SetProgressStyle(ProgressDialogStyle.Spinner);
pd.Show();
Action refresh = () =>
{
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
if (tickers.Any())
{
RunOnUiThread(() => RefreshUI(tickers));
}
else
{
2010-12-27 03:25:52 +00:00
RunOnUiThread(() => ShowMessage("Please add positions!"));
}
RunOnUiThread(pd.Dismiss);
};
var background = new Thread(() => refresh());
background.Start();
UpdateHeader(GetStockItemsFromConfig());
}
private void ShowMessage(string message)
{
var listAdapter = new ArrayAdapter<string>(this, Android.R.Layout.SimpleListItem1, new[] {message});
QuoteListview.Adapter = listAdapter;
}
private void RefreshUI(IEnumerable<PositionResultsViewModel> tickers)
{
var listAdapter = new PositionArrayAdapter(this, tickers.ToArray());
QuoteListview.Adapter = listAdapter;
}
private void UpdateHeader(ICollection<StockDataItem> items)
{
QuoteListviewHeader.RemoveAllViews();
var cellwidth = this.GetScreenWidth()/items.Count;
foreach (var stockDataItem in items)
{
var textItem = new TextView(this);
textItem.Text = stockDataItem.GetStringValue();
textItem.SetWidth(cellwidth);
textItem.SetTextColor(Resources.GetColor(Android.R.Color.Black));
QuoteListviewHeader.AddView(textItem);
}
QuoteListviewHeader.SetBackgroundResource(Android.R.Color.BackgroundLight);
}
public class PositionArrayAdapter : GenericArrayAdapter<PositionResultsViewModel>
{
public PositionArrayAdapter(Context context, IEnumerable<PositionResultsViewModel> results)
: base(context, results) { }
public override long GetItemId(int position)
{
return this[position].PositionId;
2010-11-02 16:26:36 +00:00
}
2010-12-27 03:25:52 +00:00
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = this[position];
var width = Context.GetScreenWidth();
var columnWidth = width / item.Items.Count;
var row = new LinearLayout(Context);
row.Orientation = Orientation.Horizontal;
foreach (var stockDataItem in GetStockItemsFromConfig())
{
var cell = new TextView(Context);
cell.Text = item.Items[stockDataItem];
cell.SetWidth(columnWidth);
row.Tag = item.PositionId;
row.AddView(cell);
}
return row;
}
2010-11-02 16:26:36 +00:00
}
private void WireUpEvents()
{
AddPositionButton.Click += addPositionButton_Click;
RegisterForContextMenu(QuoteListview);
}
private void SetTitle()
{
2011-01-02 22:47:20 +00:00
var portfolio = _repo.GetPortfolioById(ThisPortofolioId);
this.Title = "Portfolio: " + portfolio.Name;
}
void addPositionButton_Click(object sender, EventArgs e)
{
var intent = new Intent();
2011-01-02 21:51:19 +00:00
intent.SetClassName(this, EditPositionActivity.ClassName);
intent.PutExtra(EditPositionActivity.Extra_PortfolioID, ThisPortofolioId);
StartActivityForResult(intent, 0);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
Refresh();
}
2011-01-02 22:47:20 +00:00
public static List<StockDataItem> GetStockItemsFromConfig()
{
// TODO: load this from a config
var items = new List<StockDataItem>();
items.Add(StockDataItem.Ticker);
items.Add(StockDataItem.LastTradePrice);
items.Add(StockDataItem.GainLossRealTime);
items.Add(StockDataItem.Time);
return items;
}
2010-11-02 16:26:36 +00:00
}
}