minor cleanup & refactoring

This commit is contained in:
mgroves 2011-01-02 17:47:20 -05:00
parent b9a30b6d62
commit 3709c553cb
13 changed files with 54 additions and 63 deletions

View file

@ -1,7 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MonoStockPortfolio.Entities; using MonoStockPortfolio.Entities;
namespace MonoStockPortfolio.Core namespace MonoStockPortfolio.Core
@ -24,16 +21,5 @@ namespace MonoStockPortfolio.Core
} }
return string.Empty; return string.Empty;
} }
public static IEnumerable<T> GetValues<T>(this Enum value)
{
var enumerations = new List<Enum>();
var fields = value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
foreach (var fieldInfo in fields)
{
enumerations.Add((Enum)fieldInfo.GetValue(value));
}
return enumerations.Cast<T>();
}
} }
} }

View file

@ -8,10 +8,11 @@ namespace MonoStockPortfolio.Core.PortfolioRepositories
IList<Portfolio> GetAllPortfolios(); IList<Portfolio> GetAllPortfolios();
void SavePortfolio(Portfolio portfolio); void SavePortfolio(Portfolio portfolio);
void DeletePortfolioById(int portfolioId); void DeletePortfolioById(int portfolioId);
IList<Position> GetAllPositions(long portfolioId);
Portfolio GetPortfolioById(long portfolioId); Portfolio GetPortfolioById(long portfolioId);
void SavePosition(Position position);
Portfolio GetPortfolioByName(string portfolioName); Portfolio GetPortfolioByName(string portfolioName);
IList<Position> GetAllPositions(long portfolioId);
void SavePosition(Position position);
void DeletePositionById(long positionId); void DeletePositionById(long positionId);
Position GetPositionById(long positionId); Position GetPositionById(long positionId);
} }

View file

@ -5,8 +5,6 @@ namespace MonoStockPortfolio.Core.Services
{ {
public interface IPortfolioService public interface IPortfolioService
{ {
IList<Portfolio> GetAllPortfolios();
IEnumerable<PositionResultsViewModel> GetDetailedItems(long portfolioID, IEnumerable<StockDataItem> items); IEnumerable<PositionResultsViewModel> GetDetailedItems(long portfolioID, IEnumerable<StockDataItem> items);
Portfolio GetPortolioById(long portfolioId);
} }
} }

View file

@ -13,11 +13,6 @@ namespace MonoStockPortfolio.Core.Services
private readonly IPortfolioRepository _portRepo; private readonly IPortfolioRepository _portRepo;
private readonly IStockDataProvider _stockRepo; private readonly IStockDataProvider _stockRepo;
// public PortfolioService(Context context) : this(new AndroidSqlitePortfolioRepository(context),
// new YahooStockDataProvider())
// {
// }
public PortfolioService(IPortfolioRepository portfolioRepository, IStockDataProvider stockDataProvider) public PortfolioService(IPortfolioRepository portfolioRepository, IStockDataProvider stockDataProvider)
{ {
_portRepo = portfolioRepository; _portRepo = portfolioRepository;
@ -60,11 +55,6 @@ namespace MonoStockPortfolio.Core.Services
} }
} }
public Portfolio GetPortolioById(long portfolioId)
{
return _portRepo.GetPortfolioById(portfolioId);
}
private IDictionary<StockDataItem, string> GetStockItems(IEnumerable<StockDataItem> items, StockQuote quote) private IDictionary<StockDataItem, string> GetStockItems(IEnumerable<StockDataItem> items, StockQuote quote)
{ {
var dict = new Dictionary<StockDataItem, string>(); var dict = new Dictionary<StockDataItem, string>();

View file

@ -1,4 +1,3 @@
using System;
using FileHelpers; using FileHelpers;
namespace MonoStockPortfolio.Core.StockData namespace MonoStockPortfolio.Core.StockData

View file

@ -1,8 +1,6 @@
using System; using System;
using Android.App; using Android.App;
using Android.Content;
using Android.OS; using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories; using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Entities; using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework; using MonoStockPortfolio.Framework;
@ -22,7 +20,7 @@ namespace MonoStockPortfolio.Activites
WireUpEvents(); WireUpEvents();
var portfolioId = Intent.GetLongExtra(Extra_PortfolioID, -1); var portfolioId = ThisPortfolioId;
if(portfolioId != -1) if(portfolioId != -1)
{ {
this.Title = "Edit Portfolio"; this.Title = "Edit Portfolio";
@ -49,11 +47,9 @@ namespace MonoStockPortfolio.Activites
{ {
_repo.SavePortfolio(portfolioToSave); _repo.SavePortfolio(portfolioToSave);
Toast.MakeText(this, "You saved: " + PortfolioName.Text, ToastLength.Short).Show(); this.LongToast("You saved: " + PortfolioName.Text);
var intent = new Intent(); this.EndActivity();
SetResult(Result.Ok, intent);
Finish();
} }
} }
@ -66,7 +62,7 @@ namespace MonoStockPortfolio.Activites
var result = validator.Apply(); var result = validator.Apply();
if(result != string.Empty) if(result != string.Empty)
{ {
Toast.MakeText(this, result, ToastLength.Long).Show(); this.LongToast(result);
return false; return false;
} }
return true; return true;
@ -85,7 +81,7 @@ namespace MonoStockPortfolio.Activites
private Portfolio GetPortfolioToSave() private Portfolio GetPortfolioToSave()
{ {
Portfolio portfolioToSave; Portfolio portfolioToSave;
var portfolioId = Intent.GetLongExtra(Extra_PortfolioID, -1); var portfolioId = ThisPortfolioId;
if (portfolioId != -1) if (portfolioId != -1)
{ {
portfolioToSave = new Portfolio(portfolioId); portfolioToSave = new Portfolio(portfolioId);

View file

@ -6,6 +6,7 @@ namespace MonoStockPortfolio.Activites
{ {
public static string ClassName { get { return "monostockportfolio.activites.EditPortfolioActivity"; } } public static string ClassName { get { return "monostockportfolio.activites.EditPortfolioActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPortfolioActivity.PortfolioID"; } } public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPortfolioActivity.PortfolioID"; } }
public long ThisPortfolioId { get { return Intent.GetLongExtra(Extra_PortfolioID, -1); } }
protected Button SaveButton { get { return FindViewById<Button>(Resource.id.btnSave); } } protected Button SaveButton { get { return FindViewById<Button>(Resource.id.btnSave); } }
protected EditText PortfolioName { get { return FindViewById<EditText>(Resource.id.portfolioName); } } protected EditText PortfolioName { get { return FindViewById<EditText>(Resource.id.portfolioName); } }

View file

@ -1,8 +1,6 @@
using System; using System;
using Android.App; using Android.App;
using Android.Content;
using Android.OS; using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories; using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Entities; using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework; using MonoStockPortfolio.Framework;
@ -20,7 +18,7 @@ namespace MonoStockPortfolio.Activites
SetContentView(Resource.layout.addposition); SetContentView(Resource.layout.addposition);
var positionId = Intent.GetLongExtra(Extra_PositionID, -1); var positionId = ThisPositionId;
if (positionId != -1) if (positionId != -1)
{ {
this.Title = "Edit Position"; this.Title = "Edit Position";
@ -50,9 +48,7 @@ namespace MonoStockPortfolio.Activites
var positionToSave = GetPositionToSave(); var positionToSave = GetPositionToSave();
_repo.SavePosition(positionToSave); _repo.SavePosition(positionToSave);
var intent = new Intent(); this.EndActivity();
SetResult(Result.Ok, intent);
Finish();
} }
} }
@ -65,7 +61,7 @@ namespace MonoStockPortfolio.Activites
return true; return true;
} }
Toast.MakeText(this, result, ToastLength.Long).Show(); this.LongToast(result);
return false; return false;
} }
@ -84,7 +80,7 @@ namespace MonoStockPortfolio.Activites
private Position GetPositionToSave() private Position GetPositionToSave()
{ {
Position positionToSave; Position positionToSave;
var positionId = Intent.GetLongExtra(Extra_PortfolioID, -1); var positionId = ThisPositionId;
if (positionId != -1) if (positionId != -1)
{ {
positionToSave = new Position(positionId); positionToSave = new Position(positionId);
@ -97,7 +93,7 @@ namespace MonoStockPortfolio.Activites
positionToSave.Shares = decimal.Parse(SharesTextBox.Text.ToString()); positionToSave.Shares = decimal.Parse(SharesTextBox.Text.ToString());
positionToSave.PricePerShare = decimal.Parse(PriceTextBox.Text.ToString()); positionToSave.PricePerShare = decimal.Parse(PriceTextBox.Text.ToString());
positionToSave.Ticker = TickerTextBox.Text.ToString(); positionToSave.Ticker = TickerTextBox.Text.ToString();
positionToSave.ContainingPortfolioID = Intent.GetLongExtra(Extra_PortfolioID, -1); positionToSave.ContainingPortfolioID = ThisPortfolioId;
return positionToSave; return positionToSave;
} }
} }

View file

@ -6,7 +6,9 @@ namespace MonoStockPortfolio.Activites
{ {
public static string ClassName { get { return "monostockportfolio.activites.EditPositionActivity"; } } public static string ClassName { get { return "monostockportfolio.activites.EditPositionActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPositionActivity.PortfolioID"; } } public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPositionActivity.PortfolioID"; } }
public long ThisPortfolioId { get { return Intent.GetLongExtra(Extra_PortfolioID, -1); } }
public static string Extra_PositionID { get { return "monoStockPortfolio.EditPositionActivity.PositionID"; } } public static string Extra_PositionID { get { return "monoStockPortfolio.EditPositionActivity.PositionID"; } }
public long ThisPositionId { get { return Intent.GetLongExtra(Extra_PositionID, -1); } }
protected EditText TickerTextBox { get { return FindViewById<EditText>(Resource.id.addPositionTicker); } } protected EditText TickerTextBox { get { return FindViewById<EditText>(Resource.id.addPositionTicker); } }
protected EditText PriceTextBox { get { return FindViewById<EditText>(Resource.id.addPositionPrice); } } protected EditText PriceTextBox { get { return FindViewById<EditText>(Resource.id.addPositionPrice); } }

View file

@ -6,7 +6,6 @@ using Android.Content;
using Android.OS; using Android.OS;
using Android.Widget; using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories; using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.Services;
using MonoStockPortfolio.Entities; using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework; using MonoStockPortfolio.Framework;
@ -15,7 +14,6 @@ namespace MonoStockPortfolio.Activites
[Activity(Label = "Stock Portfolio", MainLauncher = true, Icon = "@drawable/icon")] [Activity(Label = "Stock Portfolio", MainLauncher = true, Icon = "@drawable/icon")]
public partial class MainActivity : Activity public partial class MainActivity : Activity
{ {
[IoC] private IPortfolioService _svc;
[IoC] private IPortfolioRepository _repo; [IoC] private IPortfolioRepository _repo;
private IList<Portfolio> _portfolios; private IList<Portfolio> _portfolios;
@ -33,7 +31,7 @@ namespace MonoStockPortfolio.Activites
private void RefreshList() private void RefreshList()
{ {
_portfolios = _svc.GetAllPortfolios(); _portfolios = _repo.GetAllPortfolios();
var listAdapter = new ArrayAdapter<string>(this, Android.R.Layout.SimpleListItem1, _portfolios.Select(p => p.Name).ToList()); var listAdapter = new ArrayAdapter<string>(this, Android.R.Layout.SimpleListItem1, _portfolios.Select(p => p.Name).ToList());
PortfolioListView.Adapter = listAdapter; PortfolioListView.Adapter = listAdapter;

View file

@ -12,7 +12,6 @@ using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.Services; using MonoStockPortfolio.Core.Services;
using MonoStockPortfolio.Entities; using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework; using MonoStockPortfolio.Framework;
using Orientation = Android.Widget.Orientation;
namespace MonoStockPortfolio.Activites namespace MonoStockPortfolio.Activites
{ {
@ -178,21 +177,10 @@ namespace MonoStockPortfolio.Activites
private void SetTitle() private void SetTitle()
{ {
var portfolio = _svc.GetPortolioById(ThisPortofolioId); var portfolio = _repo.GetPortfolioById(ThisPortofolioId);
this.Title = "Portfolio: " + portfolio.Name; this.Title = "Portfolio: " + portfolio.Name;
} }
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;
}
void addPositionButton_Click(object sender, EventArgs e) void addPositionButton_Click(object sender, EventArgs e)
{ {
var intent = new Intent(); var intent = new Intent();
@ -207,5 +195,19 @@ namespace MonoStockPortfolio.Activites
Refresh(); Refresh();
} }
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;
}
} }
} }

View file

@ -0,0 +1,21 @@
using Android.App;
using Android.Content;
using Android.Widget;
namespace MonoStockPortfolio.Framework
{
public static class ActivityExtensions
{
public static void EndActivity(this Activity @this)
{
var intent = new Intent();
@this.SetResult(Result.Ok, intent);
@this.Finish();
}
public static void LongToast(this Activity @this, string message)
{
Toast.MakeText(@this, message, ToastLength.Long).Show();
}
}
}

View file

@ -55,6 +55,7 @@
<Compile Include="Activites\EditPortfolioActivity.designer.cs"> <Compile Include="Activites\EditPortfolioActivity.designer.cs">
<DependentUpon>EditPortfolioActivity.cs</DependentUpon> <DependentUpon>EditPortfolioActivity.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Framework\ActivityExtensions.cs" />
<Compile Include="Framework\ContextExtensions.cs" /> <Compile Include="Framework\ContextExtensions.cs" />
<Compile Include="Framework\GenericArrayAdapter.cs" /> <Compile Include="Framework\GenericArrayAdapter.cs" />
<Compile Include="Framework\IoCAttribute.cs" /> <Compile Include="Framework\IoCAttribute.cs" />