reorganized activity classes into partials, also changed how IoCAttribute handles a long-running static Context

This commit is contained in:
mgroves 2010-12-26 21:47:10 -05:00
parent f5520b1ad5
commit 86a9854c88
11 changed files with 131 additions and 93 deletions

View file

@ -10,17 +10,9 @@ using MonoStockPortfolio.Validation;
namespace MonoStockPortfolio namespace MonoStockPortfolio
{ {
[Activity(Label = "Add Position", MainLauncher = false)] [Activity(Label = "Add Position", MainLauncher = false)]
public class AddPositionActivity : Activity public partial class AddPositionActivity : Activity
{ {
public static string ClassName { get { return "monostockportfolio.AddPositionActivity"; } } [IoC] private IPortfolioRepository _repo;
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
private IPortfolioRepository _repo;
private EditText TickerTextBox { get { return FindViewById<EditText>(Resource.id.addPositionTicker); } }
private EditText PriceTextBox { get { return FindViewById<EditText>(Resource.id.addPositionPrice); } }
private EditText SharesTextBox { get { return FindViewById<EditText>(Resource.id.addPositionShares); } }
private Button SaveButton { get { return FindViewById<Button>(Resource.id.addPositionSaveButton); } }
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
@ -28,8 +20,6 @@ namespace MonoStockPortfolio
SetContentView(Resource.layout.addposition); SetContentView(Resource.layout.addposition);
_repo = new AndroidSqlitePortfolioRepository(this);
SaveButton.Click += saveButton_Click; SaveButton.Click += saveButton_Click;
} }

View file

@ -0,0 +1,15 @@
using Android.Widget;
namespace MonoStockPortfolio
{
public partial class AddPositionActivity
{
public static string ClassName { get { return "monostockportfolio.AddPositionActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.AddPositionActivity.PortfolioID"; } }
protected EditText TickerTextBox { get { return FindViewById<EditText>(Resource.id.addPositionTicker); } }
protected EditText PriceTextBox { get { return FindViewById<EditText>(Resource.id.addPositionPrice); } }
protected EditText SharesTextBox { get { return FindViewById<EditText>(Resource.id.addPositionShares); } }
protected Button SaveButton { get { return FindViewById<Button>(Resource.id.addPositionSaveButton); } }
}
}

View file

@ -10,22 +10,14 @@ using MonoStockPortfolio.Validation;
namespace MonoStockPortfolio namespace MonoStockPortfolio
{ {
[Activity(Label = "Add Portfolio", MainLauncher = false)] [Activity(Label = "Add Portfolio", MainLauncher = false)]
public class EditPortfolioActivity : Activity public partial class EditPortfolioActivity : Activity
{ {
public static string ClassName { get { return "monostockportfolio.EditPortfolioActivity"; } } [IoC] private IPortfolioRepository _repo;
public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPortfolioActivity.PortfolioID"; } }
private Button SaveButton { get { return FindViewById<Button>(Resource.id.btnSave); } }
private EditText PortfolioName { get { return FindViewById<EditText>(Resource.id.portfolioName); } }
private IPortfolioRepository _repo;
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
base.OnCreate(bundle); base.OnCreate(bundle);
_repo = new AndroidSqlitePortfolioRepository(this);
SetContentView(Resource.layout.addportfolio); SetContentView(Resource.layout.addportfolio);
WireUpEvents(); WireUpEvents();

View file

@ -0,0 +1,13 @@
using Android.Widget;
namespace MonoStockPortfolio
{
public partial class EditPortfolioActivity
{
public static string ClassName { get { return "monostockportfolio.EditPortfolioActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPortfolioActivity.PortfolioID"; } }
protected Button SaveButton { get { return FindViewById<Button>(Resource.id.btnSave); } }
protected EditText PortfolioName { get { return FindViewById<EditText>(Resource.id.portfolioName); } }
}
}

View file

@ -13,7 +13,11 @@ namespace MonoStockPortfolio
public override void OnGetValue(LocationInterceptionArgs args) public override void OnGetValue(LocationInterceptionArgs args)
{ {
if(_context == null) _context = (Context)args.Instance; if(_context == null)
{
var activityContext= (Context)args.Instance;
_context = activityContext.ApplicationContext.ApplicationContext;
}
var locationType = args.Location.LocationType; var locationType = args.Location.LocationType;
var instantiation = GetInstance(locationType); var instantiation = GetInstance(locationType);
@ -24,7 +28,7 @@ namespace MonoStockPortfolio
args.ProceedGetValue(); args.ProceedGetValue();
} }
private static object GetInstance(Type locationType) private object GetInstance(Type locationType)
{ {
if (DependencyMap.ContainsKey(locationType)) if (DependencyMap.ContainsKey(locationType))
{ {
@ -33,13 +37,13 @@ namespace MonoStockPortfolio
return null; return null;
} }
private static IDictionary<Type, Func<object>> DependencyMap private IDictionary<Type, Func<object>> DependencyMap
{ {
get { return _dependencyMap ?? (_dependencyMap = DefaultDependencies()); } get { return _dependencyMap ?? (_dependencyMap = DefaultDependencies()); }
} }
private static IDictionary<Type, Func<object>> _dependencyMap; private static IDictionary<Type, Func<object>> _dependencyMap;
private static IDictionary<Type, Func<object>> DefaultDependencies() private IDictionary<Type, Func<object>> DefaultDependencies()
{ {
var map = new Dictionary<Type, Func<object>>(); var map = new Dictionary<Type, Func<object>>();
map.Add(typeof(IPortfolioService), () => new PortfolioService(_context)); map.Add(typeof(IPortfolioService), () => new PortfolioService(_context));

View file

@ -12,10 +12,8 @@ using MonoStockPortfolio.Entities;
namespace MonoStockPortfolio namespace MonoStockPortfolio
{ {
[Activity(Label = "Stock Portfolio", MainLauncher = true, Icon = "@drawable/icon")] [Activity(Label = "Stock Portfolio", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity public partial class MainActivity : Activity
{ {
public static string ClassName { get { return "monostockportfolio.MainActivity"; } }
[IoC] private IPortfolioService _svc; [IoC] private IPortfolioService _svc;
[IoC] private IPortfolioRepository _repo; [IoC] private IPortfolioRepository _repo;
@ -33,9 +31,6 @@ namespace MonoStockPortfolio
WireUpEvents(); WireUpEvents();
} }
private Button AddPortfolioButton { get { return FindViewById<Button>(Resource.id.btnAddPortfolio); } }
private ListView PortfolioListView { get { return FindViewById<ListView>(Resource.id.portfolioList); } }
private void RefreshList() private void RefreshList()
{ {
_portfolios = _svc.GetAllPortfolios(); _portfolios = _svc.GetAllPortfolios();

View file

@ -0,0 +1,12 @@
using Android.Widget;
namespace MonoStockPortfolio
{
public partial class MainActivity
{
public static string ClassName { get { return "monostockportfolio.MainActivity"; } }
protected Button AddPortfolioButton { get { return FindViewById<Button>(Resource.id.btnAddPortfolio); } }
protected ListView PortfolioListView { get { return FindViewById<ListView>(Resource.id.portfolioList); } }
}
}

View file

@ -48,11 +48,23 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AddPositionActivity.designer.cs">
<DependentUpon>AddPositionActivity.cs</DependentUpon>
</Compile>
<Compile Include="EditPortfolioActivity.cs" /> <Compile Include="EditPortfolioActivity.cs" />
<Compile Include="EditPortfolioActivity.designer.cs">
<DependentUpon>EditPortfolioActivity.cs</DependentUpon>
</Compile>
<Compile Include="IoCAttribute.cs" /> <Compile Include="IoCAttribute.cs" />
<Compile Include="MainActivity.designer.cs">
<DependentUpon>MainActivity.cs</DependentUpon>
</Compile>
<Compile Include="PortfolioActivity.cs" /> <Compile Include="PortfolioActivity.cs" />
<Compile Include="MainActivity.cs" /> <Compile Include="MainActivity.cs" />
<Compile Include="AddPositionActivity.cs" /> <Compile Include="AddPositionActivity.cs" />
<Compile Include="PortfolioActivity.designer.cs">
<DependentUpon>PortfolioActivity.cs</DependentUpon>
</Compile>
<Compile Include="Resources\Resource.Designer.cs" /> <Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringExtensions.cs" /> <Compile Include="StringExtensions.cs" />

View file

@ -13,23 +13,17 @@ using MonoStockPortfolio.Core.Services;
namespace MonoStockPortfolio namespace MonoStockPortfolio
{ {
[Activity(Label = "Portfolio")] [Activity(Label = "Portfolio")]
public class PortfolioActivity : Activity public partial class PortfolioActivity : Activity
{ {
public static string ClassName { get { return "monostockportfolio.PortfolioActivity"; } } [IoC] private IPortfolioService _svc;
public static string Extra_PortfolioID { get { return "monoStockPortfolio.PortfolioActivity.PortfolioID"; } }
private IPortfolioService _svc;
private IEnumerable<char>[] longClickOptions; private IEnumerable<char>[] longClickOptions;
private long ThisPortofolioId { get { return Intent.GetLongExtra(Extra_PortfolioID, -1); } }
protected override void OnCreate(Bundle bundle) protected override void OnCreate(Bundle bundle)
{ {
base.OnCreate(bundle); base.OnCreate(bundle);
SetContentView(Resource.layout.portfolio); SetContentView(Resource.layout.portfolio);
_svc = new PortfolioService(this);
Refresh(); Refresh();
WireUpEvents(); WireUpEvents();
@ -53,8 +47,7 @@ namespace MonoStockPortfolio
private void RefreshUI(IEnumerable<IDictionary<StockDataItem, string>> tickers) private void RefreshUI(IEnumerable<IDictionary<StockDataItem, string>> tickers)
{ {
var tableLayout = FindViewById<TableLayout>(Resource.id.quoteTable); QuoteTable.RemoveAllViews();
tableLayout.RemoveAllViews();
WriteTickerHeader(tickers.First()); WriteTickerHeader(tickers.First());
foreach (var ticker in tickers) foreach (var ticker in tickers)
@ -65,8 +58,7 @@ namespace MonoStockPortfolio
private void WireUpEvents() private void WireUpEvents()
{ {
var addPositionButton = FindViewById<Button>(Resource.id.btnAddPosition); AddPositionButton.Click += addPositionButton_Click;
addPositionButton.Click += addPositionButton_Click;
} }
private void SetTitle() private void SetTitle()
@ -119,8 +111,7 @@ namespace MonoStockPortfolio
tr.AddView(column); tr.AddView(column);
} }
var tableLayout = FindViewById<TableLayout>(Resource.id.quoteTable); QuoteTable.AddView(tr, new TableRow.LayoutParams(TableRow.LayoutParams.FillParent, TableRow.LayoutParams.WrapContent));
tableLayout.AddView(tr, new TableRow.LayoutParams(TableRow.LayoutParams.FillParent, TableRow.LayoutParams.WrapContent));
} }
private void WriteTickerRow(IDictionary<StockDataItem, string> ticker) private void WriteTickerRow(IDictionary<StockDataItem, string> ticker)
@ -140,8 +131,7 @@ namespace MonoStockPortfolio
tr.AddView(column); tr.AddView(column);
} }
var tableLayout = FindViewById<TableLayout>(Resource.id.quoteTable); QuoteTable.AddView(tr, new TableRow.LayoutParams(TableRow.LayoutParams.FillParent, TableRow.LayoutParams.WrapContent));
tableLayout.AddView(tr, new TableRow.LayoutParams(TableRow.LayoutParams.FillParent, TableRow.LayoutParams.WrapContent));
} }
void tr_LongClick(object sender, Android.Views.View.LongClickEventArgs e) void tr_LongClick(object sender, Android.Views.View.LongClickEventArgs e)

View file

@ -0,0 +1,15 @@
using Android.Widget;
namespace MonoStockPortfolio
{
public partial class PortfolioActivity
{
public static string ClassName { get { return "monostockportfolio.PortfolioActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.PortfolioActivity.PortfolioID"; } }
public long ThisPortofolioId { get { return Intent.GetLongExtra(Extra_PortfolioID, -1); } }
protected TableLayout QuoteTable { get { return FindViewById<TableLayout>(Resource.id.quoteTable); } }
protected Button AddPositionButton { get { return FindViewById<Button>(Resource.id.btnAddPosition); } }
}
}

View file

@ -15,7 +15,7 @@ namespace MonoStockPortfolio
public partial class Resource public partial class Resource
{ {
public class attr public partial class attr
{ {
private attr() private attr()
@ -23,7 +23,7 @@ namespace MonoStockPortfolio
} }
} }
public class drawable public partial class drawable
{ {
// aapt resource value: 0x7f020000 // aapt resource value: 0x7f020000
@ -34,7 +34,45 @@ namespace MonoStockPortfolio
} }
} }
public class layout public partial class id
{
// aapt resource value: 0x7f050004
public const int addPositionPrice = 2131034116;
// aapt resource value: 0x7f050005
public const int addPositionSaveButton = 2131034117;
// aapt resource value: 0x7f050003
public const int addPositionShares = 2131034115;
// aapt resource value: 0x7f050002
public const int addPositionTicker = 2131034114;
// aapt resource value: 0x7f050006
public const int btnAddPortfolio = 2131034118;
// aapt resource value: 0x7f050008
public const int btnAddPosition = 2131034120;
// aapt resource value: 0x7f050001
public const int btnSave = 2131034113;
// aapt resource value: 0x7f050007
public const int portfolioList = 2131034119;
// aapt resource value: 0x7f050000
public const int portfolioName = 2131034112;
// aapt resource value: 0x7f050009
public const int quoteTable = 2131034121;
private id()
{
}
}
public partial class layout
{ {
// aapt resource value: 0x7f030000 // aapt resource value: 0x7f030000
@ -54,56 +92,18 @@ namespace MonoStockPortfolio
} }
} }
public class @string public partial class @string
{ {
// aapt resource value: 0x7f040000
public const int hello = 2130968576;
// aapt resource value: 0x7f040001 // aapt resource value: 0x7f040001
public const int app_name = 2130968577; public const int app_name = 2130968577;
// aapt resource value: 0x7f040000
public const int hello = 2130968576;
private @string() private @string()
{ {
} }
} }
public class id
{
// aapt resource value: 0x7f050000
public const int portfolioName = 2131034112;
// aapt resource value: 0x7f050001
public const int btnSave = 2131034113;
// aapt resource value: 0x7f050002
public const int addPositionTicker = 2131034114;
// aapt resource value: 0x7f050003
public const int addPositionShares = 2131034115;
// aapt resource value: 0x7f050004
public const int addPositionPrice = 2131034116;
// aapt resource value: 0x7f050005
public const int addPositionSaveButton = 2131034117;
// aapt resource value: 0x7f050006
public const int btnAddPortfolio = 2131034118;
// aapt resource value: 0x7f050007
public const int portfolioList = 2131034119;
// aapt resource value: 0x7f050008
public const int btnAddPosition = 2131034120;
// aapt resource value: 0x7f050009
public const int quoteTable = 2131034121;
private id()
{
}
}
} }
} }