refactor of intent stuff, added ManifestName class to framework

This commit is contained in:
mgroves 2011-02-20 22:40:43 -05:00
parent 48a6e6a318
commit 0a489ce6ac
10 changed files with 89 additions and 70 deletions

View file

@ -1,5 +1,6 @@
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories;
@ -8,14 +9,31 @@ using MonoStockPortfolio.Framework;
namespace MonoStockPortfolio.Activites
{
[Activity(Label = "Add Portfolio", MainLauncher = false)]
public partial class EditPortfolioActivity : Activity
[Activity(Label = "Add Portfolio", MainLauncher = false, Name = "monostockportfolio.activites.EditPortfolioActivity")]
public class EditPortfolioActivity : Activity
{
[IoC] private IPortfolioRepository _repo;
[LazyView(Resource.Id.btnSave)] protected Button SaveButton;
[LazyView(Resource.Id.portfolioName)] protected EditText PortfolioName;
private const string PORTFOLIOIDEXTRA = "monoStockPortfolio.EditPortfolioActivity.PortfolioID";
public static Intent AddIntent(Context context)
{
var intent = new Intent();
intent.SetClassName(context, ManifestNames.GetName<EditPortfolioActivity>());
return intent;
}
public static Intent EditIntent(Context context, long portfolioId)
{
var intent = new Intent();
intent.SetClassName(context, ManifestNames.GetName<EditPortfolioActivity>());
intent.PutExtra(PORTFOLIOIDEXTRA, portfolioId);
return intent;
}
public long ThisPortfolioId { get { return Intent.GetLongExtra(PORTFOLIOIDEXTRA, -1); } }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

View file

@ -1,11 +0,0 @@
using Android.Widget;
namespace MonoStockPortfolio.Activites
{
public partial class EditPortfolioActivity
{
public static string ClassName { get { return "monostockportfolio.activites.EditPortfolioActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.EditPortfolioActivity.PortfolioID"; } }
public long ThisPortfolioId { get { return Intent.GetLongExtra(Extra_PortfolioID, -1); } }
}
}

View file

@ -1,5 +1,6 @@
using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories;
@ -8,8 +9,8 @@ using MonoStockPortfolio.Framework;
namespace MonoStockPortfolio.Activites
{
[Activity(Label = "Add Position", MainLauncher = false)]
public partial class EditPositionActivity : Activity
[Activity(Label = "Add Position", MainLauncher = false, Name = "monostockportfolio.activites.EditPositionActivity")]
public class EditPositionActivity : Activity
{
[IoC] private IPortfolioRepository _repo;
@ -18,6 +19,27 @@ namespace MonoStockPortfolio.Activites
[LazyView(Resource.Id.addPositionShares)] protected EditText SharesTextBox;
[LazyView(Resource.Id.addPositionSaveButton)] protected Button SaveButton;
private const string POSITIONIDEXTRA = "monoStockPortfolio.EditPositionActivity.PositionID";
private const string PORTFOLIOIDEXTRA = "monoStockPortfolio.EditPositionActivity.PortfolioID";
public static Intent AddIntent(Context context, long portfolioId)
{
var intent = new Intent();
intent.SetClassName(context, ManifestNames.GetName<EditPositionActivity>());
intent.PutExtra(PORTFOLIOIDEXTRA, portfolioId);
return intent;
}
public static Intent EditIntent(Context context, long positionId, long portfolioId)
{
var intent = new Intent();
intent.SetClassName(context, ManifestNames.GetName<EditPositionActivity>());
intent.PutExtra(POSITIONIDEXTRA, positionId);
intent.PutExtra(PORTFOLIOIDEXTRA, portfolioId);
return intent;
}
public long ThisPortfolioId { get { return Intent.GetLongExtra(PORTFOLIOIDEXTRA, -1); } }
public long ThisPositionId { get { return Intent.GetLongExtra(POSITIONIDEXTRA, -1); } }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

View file

@ -1,13 +0,0 @@
using Android.Widget;
namespace MonoStockPortfolio.Activites
{
public partial class EditPositionActivity
{
public static string ClassName { get { return "monostockportfolio.activites.EditPositionActivity"; } }
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 long ThisPositionId { get { return Intent.GetLongExtra(Extra_PositionID, -1); } }
}
}

View file

@ -67,9 +67,7 @@ namespace MonoStockPortfolio.Activites
if (item.Title.ToS() == "Rename")
{
// Edit
var intent = new Intent();
intent.SetClassName(this, EditPortfolioActivity.ClassName);
intent.PutExtra(EditPortfolioActivity.Extra_PortfolioID, (long)item.ItemId);
var intent = EditPortfolioActivity.EditIntent(this, item.ItemId);
StartActivityForResult(intent, 0);
return true;
}
@ -111,16 +109,13 @@ namespace MonoStockPortfolio.Activites
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);
var intent = PortfolioActivity.ViewIntent(this, _portfolios[e.Position].ID ?? -1);
StartActivityForResult(intent, 0);
}
private void addPortfolioButton_Click(object sender, EventArgs e)
{
var intent = new Intent();
intent.SetClassName(this, EditPortfolioActivity.ClassName);
var intent = EditPortfolioActivity.AddIntent(this);
StartActivityForResult(intent, 0);
}

View file

@ -15,8 +15,8 @@ using MonoStockPortfolio.Framework;
namespace MonoStockPortfolio.Activites
{
[Activity(Label = "Portfolio")]
public partial class PortfolioActivity : Activity
[Activity(Label = "Portfolio", Name = "monostockportfolio.activites.PortfolioActivity")]
public class PortfolioActivity : Activity
{
[IoC] private IPortfolioService _svc;
[IoC] private IPortfolioRepository _repo;
@ -26,6 +26,17 @@ namespace MonoStockPortfolio.Activites
[LazyView(Resource.Id.btnAddPosition)] protected Button AddPositionButton;
[LazyView(Resource.Id.quoteHeaderLayout)] protected LinearLayout QuoteListviewHeader;
private const string PORTFOLIOIDEXTRA = "monoStockPortfolio.PortfolioActivity.PortfolioID";
public static Intent ViewIntent(Context context, long portfolioId)
{
var intent = new Intent();
intent.SetClassName(context, ManifestNames.GetName<PortfolioActivity>());
intent.PutExtra(PORTFOLIOIDEXTRA, portfolioId);
return intent;
}
public long ThisPortofolioId { get { return Intent.GetLongExtra(PORTFOLIOIDEXTRA, -1); } }
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
@ -74,10 +85,7 @@ namespace MonoStockPortfolio.Activites
if (item.Title.ToS() == "Edit")
{
// Edit
var intent = new Intent();
intent.SetClassName(this, EditPositionActivity.ClassName);
intent.PutExtra(EditPositionActivity.Extra_PositionID, (long)item.ItemId);
intent.PutExtra(EditPositionActivity.Extra_PortfolioID, ThisPortofolioId);
var intent = EditPositionActivity.EditIntent(this, item.ItemId, ThisPortofolioId);
StartActivityForResult(intent, 0);
return true;
}
@ -188,9 +196,7 @@ namespace MonoStockPortfolio.Activites
void addPositionButton_Click(object sender, EventArgs e)
{
var intent = new Intent();
intent.SetClassName(this, EditPositionActivity.ClassName);
intent.PutExtra(EditPositionActivity.Extra_PortfolioID, ThisPortofolioId);
var intent = EditPositionActivity.AddIntent(this, ThisPortofolioId);
StartActivityForResult(intent, 0);
}
@ -201,12 +207,9 @@ namespace MonoStockPortfolio.Activites
Refresh();
}
public IEnumerable<StockDataItem> GetStockItemsFromConfig()
{
return _config.GetStockItems();
}
}
}
}

View file

@ -1,12 +0,0 @@
using Android.Widget;
namespace MonoStockPortfolio.Activites
{
public partial class PortfolioActivity
{
public static string ClassName { get { return "monostockportfolio.activites.PortfolioActivity"; } }
public static string Extra_PortfolioID { get { return "monoStockPortfolio.PortfolioActivity.PortfolioID"; } }
public long ThisPortofolioId { get { return Intent.GetLongExtra(Extra_PortfolioID, -1); } }
}
}

View file

@ -1,3 +1,4 @@
using System.Reflection;
using Android.App;
using Android.Content;
using Android.Widget;
@ -17,5 +18,6 @@ namespace MonoStockPortfolio.Framework
{
Toast.MakeText(@this, message, ToastLength.Long).Show();
}
}
}

View file

@ -0,0 +1,23 @@
using System.Reflection;
using Android.App;
namespace MonoStockPortfolio.Framework
{
public class ManifestNames
{
public static string GetName<T>()
{
var attrs = typeof(T).GetCustomAttributes(typeof(ActivityAttribute), false);
if (attrs.Length == 0) throw new CustomAttributeFormatException("Activity attribute must specify name");
foreach (var attr in attrs)
{
var activityAttr = attr as ActivityAttribute;
if (activityAttr != null)
{
return activityAttr.Name;
}
}
throw new CustomAttributeFormatException("Activity attribute name not found");
}
}
}

View file

@ -49,25 +49,17 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Activites\ConfigActivity.cs" />
<Compile Include="Activites\EditPositionActivity.designer.cs">
<DependentUpon>EditPositionActivity.cs</DependentUpon>
</Compile>
<Compile Include="Activites\EditPortfolioActivity.cs" />
<Compile Include="Activites\EditPortfolioActivity.designer.cs">
<DependentUpon>EditPortfolioActivity.cs</DependentUpon>
</Compile>
<Compile Include="Framework\ActivityExtensions.cs" />
<Compile Include="Framework\ContextExtensions.cs" />
<Compile Include="Framework\GenericArrayAdapter.cs" />
<Compile Include="Activites\PortfolioActivity.cs" />
<Compile Include="Activites\MainActivity.cs" />
<Compile Include="Activites\EditPositionActivity.cs" />
<Compile Include="Activites\PortfolioActivity.designer.cs">
<DependentUpon>PortfolioActivity.cs</DependentUpon>
</Compile>
<Compile Include="Framework\IoCAttribute.cs" />
<Compile Include="Framework\IttyBittyIoC.cs" />
<Compile Include="Framework\LazyViewAttribute.cs" />
<Compile Include="Framework\ManifestNames.cs" />
<Compile Include="Framework\OnGuiThreadAttribute.cs" />
<Compile Include="Framework\OnWorkerThreadAttribute.cs" />
<Compile Include="Framework\ServiceLocator.cs" />