mirror of
https://github.com/mgroves/MonodroidStockPortfolio.git
synced 2024-11-15 03:00:23 +00:00
Merge branch 'multithreading_with_postsharp' into develop
This commit is contained in:
commit
cc72b84404
4 changed files with 88 additions and 23 deletions
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
|
@ -90,41 +89,40 @@ namespace MonoStockPortfolio.Activites
|
|||
|
||||
private void Refresh()
|
||||
{
|
||||
var pd = new ProgressDialog(this);
|
||||
pd.SetMessage("Loading...Please wait...");
|
||||
pd.SetProgressStyle(ProgressDialogStyle.Spinner);
|
||||
pd.Show();
|
||||
RefreshWorker();
|
||||
|
||||
Action refresh = () =>
|
||||
{
|
||||
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
|
||||
if (tickers.Any())
|
||||
{
|
||||
RunOnUiThread(() => RefreshUI(tickers));
|
||||
}
|
||||
else
|
||||
{
|
||||
RunOnUiThread(() => ShowMessage("Please add positions!"));
|
||||
}
|
||||
RunOnUiThread(pd.Dismiss);
|
||||
};
|
||||
var background = new Thread(() => refresh());
|
||||
background.Start();
|
||||
UpdateHeader(GetStockItemsFromConfig());
|
||||
}
|
||||
|
||||
private void ShowMessage(string message)
|
||||
[OnWorkerThread]
|
||||
private void RefreshWorker()
|
||||
{
|
||||
var listAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, new[] { message });
|
||||
QuoteListview.Adapter = listAdapter;
|
||||
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
|
||||
if (tickers.Any())
|
||||
{
|
||||
RefreshUI(tickers);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowMessage("Please add positions!");
|
||||
}
|
||||
}
|
||||
|
||||
[OnGuiThread]
|
||||
private void RefreshUI(IEnumerable<PositionResultsViewModel> tickers)
|
||||
{
|
||||
var listAdapter = new PositionArrayAdapter(this, tickers.ToArray());
|
||||
QuoteListview.Adapter = listAdapter;
|
||||
}
|
||||
|
||||
[OnGuiThread]
|
||||
private void ShowMessage(string message)
|
||||
{
|
||||
var listAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, new[] { message });
|
||||
QuoteListview.Adapter = listAdapter;
|
||||
}
|
||||
|
||||
[OnGuiThread]
|
||||
private void UpdateHeader(IEnumerable<StockDataItem> items)
|
||||
{
|
||||
QuoteListviewHeader.RemoveAllViews();
|
||||
|
|
23
MonoStockPortfolio/Framework/OnGuiThreadAttribute.cs
Normal file
23
MonoStockPortfolio/Framework/OnGuiThreadAttribute.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using Android.App;
|
||||
using Android.Util;
|
||||
using PostSharp.Aspects;
|
||||
|
||||
namespace MonoStockPortfolio.Framework
|
||||
{
|
||||
public class OnGuiThreadAttribute : MethodInterceptionAspect
|
||||
{
|
||||
public override void OnInvoke(MethodInterceptionArgs args)
|
||||
{
|
||||
var activity = args.Instance as Activity;
|
||||
if (activity == null)
|
||||
{
|
||||
Log.Error("OnGuiThreadAttribute", "OnGuiThreadAttribute can only be used on methods within an Activity");
|
||||
args.Proceed();
|
||||
}
|
||||
else
|
||||
{
|
||||
activity.RunOnUiThread(args.Proceed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
42
MonoStockPortfolio/Framework/OnWorkerThreadAttribute.cs
Normal file
42
MonoStockPortfolio/Framework/OnWorkerThreadAttribute.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using Android.App;
|
||||
using PostSharp.Aspects;
|
||||
|
||||
namespace MonoStockPortfolio.Framework
|
||||
{
|
||||
public class OnWorkerThreadAttribute : MethodInterceptionAspect
|
||||
{
|
||||
private ProgressDialog _progressDialog;
|
||||
|
||||
public override void OnInvoke(MethodInterceptionArgs args)
|
||||
{
|
||||
var activity = args.Instance as Activity;
|
||||
if(activity == null) throw new Exception("OnWorkerThread can only be used on methods in Activity classes");
|
||||
|
||||
ShowProgressDialog(activity);
|
||||
ThreadPool.QueueUserWorkItem(delegate
|
||||
{
|
||||
args.Proceed();
|
||||
activity.RunOnUiThread(DismissProgressDialog);
|
||||
});
|
||||
}
|
||||
|
||||
private void ShowProgressDialog(Activity activity)
|
||||
{
|
||||
if (_progressDialog == null)
|
||||
{
|
||||
var pd = new ProgressDialog(activity);
|
||||
pd.SetMessage("Loading...Please wait...");
|
||||
pd.SetProgressStyle(ProgressDialogStyle.Spinner);
|
||||
_progressDialog = pd;
|
||||
}
|
||||
_progressDialog.Show();
|
||||
}
|
||||
|
||||
private void DismissProgressDialog()
|
||||
{
|
||||
_progressDialog.Dismiss();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -70,6 +70,8 @@
|
|||
</Compile>
|
||||
<Compile Include="Framework\IoCAttribute.cs" />
|
||||
<Compile Include="Framework\IttyBittyIoC.cs" />
|
||||
<Compile Include="Framework\OnGuiThreadAttribute.cs" />
|
||||
<Compile Include="Framework\OnWorkerThreadAttribute.cs" />
|
||||
<Compile Include="Framework\ServiceLocator.cs" />
|
||||
<Compile Include="Resources\Resource.Designer.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
|
Loading…
Reference in a new issue