diff --git a/MonoStockPortfolio/Activites/PortfolioActivity.cs b/MonoStockPortfolio/Activites/PortfolioActivity.cs index d8fa02f..8059a61 100644 --- a/MonoStockPortfolio/Activites/PortfolioActivity.cs +++ b/MonoStockPortfolio/Activites/PortfolioActivity.cs @@ -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(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 tickers) { var listAdapter = new PositionArrayAdapter(this, tickers.ToArray()); QuoteListview.Adapter = listAdapter; } + [OnGuiThread] + private void ShowMessage(string message) + { + var listAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, new[] { message }); + QuoteListview.Adapter = listAdapter; + } + + [OnGuiThread] private void UpdateHeader(IEnumerable items) { QuoteListviewHeader.RemoveAllViews(); diff --git a/MonoStockPortfolio/Framework/OnGuiThreadAttribute.cs b/MonoStockPortfolio/Framework/OnGuiThreadAttribute.cs new file mode 100644 index 0000000..9e635d3 --- /dev/null +++ b/MonoStockPortfolio/Framework/OnGuiThreadAttribute.cs @@ -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); + } + } + } +} \ No newline at end of file diff --git a/MonoStockPortfolio/Framework/OnWorkerThreadAttribute.cs b/MonoStockPortfolio/Framework/OnWorkerThreadAttribute.cs new file mode 100644 index 0000000..df307e3 --- /dev/null +++ b/MonoStockPortfolio/Framework/OnWorkerThreadAttribute.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/MonoStockPortfolio/MonoStockPortfolio.csproj b/MonoStockPortfolio/MonoStockPortfolio.csproj index e7a37e3..bfee43a 100644 --- a/MonoStockPortfolio/MonoStockPortfolio.csproj +++ b/MonoStockPortfolio/MonoStockPortfolio.csproj @@ -70,6 +70,8 @@ + +