From 8ced531a7da219047c0053243bdb81b77176ae25 Mon Sep 17 00:00:00 2001 From: Tomasz Cielecki Date: Tue, 9 Apr 2013 14:40:58 +0200 Subject: [PATCH] Removed dependency of having to set CurrentActivity for LegacyBar. --- LegacyBar.Library/Bar/LegacyBar.cs | 21 ++++++------------- LegacyBar.Library/Bar/LegacyBarUtils.cs | 10 +++------ .../BarActions/OverflowLegacyBarAction.cs | 17 +++++++++------ .../Resources/Resource.designer.cs | 7 +------ LegacyBar.Sample/FragmentTabActivity.cs | 1 - LegacyBar.Sample/HomeActivity.cs | 1 - LegacyBar.Sample/OtherActivity.cs | 1 - .../Resources/Resource.designer.cs | 7 +------ 8 files changed, 22 insertions(+), 43 deletions(-) diff --git a/LegacyBar.Library/Bar/LegacyBar.cs b/LegacyBar.Library/Bar/LegacyBar.cs index 44d1252..cfc866e 100644 --- a/LegacyBar.Library/Bar/LegacyBar.cs +++ b/LegacyBar.Library/Bar/LegacyBar.cs @@ -21,7 +21,6 @@ using System; using System.Collections.Generic; using System.Linq; -using Android.App; using Android.Content; using Android.Graphics; using Android.Graphics.Drawables; @@ -47,7 +46,6 @@ public sealed class LegacyBar : RelativeLayout, View.IOnClickListener, View.IOnL private RelativeLayout _homeLayout; private ProgressBar _progress; private RelativeLayout _titleLayout; - private Context _context; private OverflowLegacyBarAction _overflowLegacyBarAction; //Used to track what we need to hide in the pop up menu. @@ -75,8 +73,6 @@ public bool HasMenuButton } } - public Activity CurrentActivity { get; set; } - public LegacyBarTheme Theme { get; set; } public bool LightIcons { get; set; } public bool IsBottom { get; set; } @@ -214,10 +210,9 @@ public ViewStates ProgressBarVisibility public LegacyBar(Context context, IAttributeSet attrs) : base(context, attrs) { - _context = context; ResourceIdManager.UpdateIdValues(); - _inflater = LayoutInflater.From(context); + _inflater = LayoutInflater.From(Context); //_inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService); _barView = (RelativeLayout)_inflater.Inflate(Resource.Layout.actionbar, null); @@ -234,10 +229,10 @@ public LegacyBar(Context context, IAttributeSet attrs) _progress = _barView.FindViewById(Resource.Id.actionbar_progress); _titleLayout = _barView.FindViewById(Resource.Id.actionbar_title_layout); - _overflowLegacyBarAction = new OverflowLegacyBarAction(context); + _overflowLegacyBarAction = new OverflowLegacyBarAction(Context); //Custom Attributes (defined in Attrs.xml) - var a = context.ObtainStyledAttributes(attrs, + var a = Context.ObtainStyledAttributes(attrs, Resource.Styleable.actionbar); //grab theme attributes @@ -394,7 +389,8 @@ public void AddAction(LegacyBarAction legacyBarAction, int index) var addActionBar = false; var hideAction = false; - if (!LegacyBarUtils.ActionFits(CurrentActivity, index, HasMenuButton, legacyBarAction.ActionType)) + + if (!LegacyBarUtils.ActionFits(Context.Resources.DisplayMetrics.WidthPixels, Context.Resources.DisplayMetrics.Density, index, HasMenuButton, legacyBarAction.ActionType)) { if (!HasMenuButton) { @@ -554,7 +550,6 @@ private View InflateOverflowAction(LegacyBarAction legacyBarAction) labelView.SetOnClickListener(this); //view.SetOnLongClickListener(this); - _overflowLegacyBarAction.Activity = CurrentActivity; return view; } @@ -578,10 +573,7 @@ public bool OnLongClick(View v) if (action.PopUpMessage == 0) return true; - if (CurrentActivity == null) - return false; - - Toast.MakeText(_context, action.PopUpMessage, ToastLength.Short).Show(); + Toast.MakeText(Context, action.PopUpMessage, ToastLength.Short).Show(); return false; } @@ -611,7 +603,6 @@ protected override void Dispose(bool disposing) _homeLayout = null; _progress = null; _titleLayout = null; - _context = null; _overflowLegacyBarAction = null; } diff --git a/LegacyBar.Library/Bar/LegacyBarUtils.cs b/LegacyBar.Library/Bar/LegacyBarUtils.cs index cfa7d01..fdeb318 100644 --- a/LegacyBar.Library/Bar/LegacyBarUtils.cs +++ b/LegacyBar.Library/Bar/LegacyBarUtils.cs @@ -31,7 +31,7 @@ public class LegacyBarUtils /// /// /// If it will fit :) - public static bool ActionFits(Activity activity, int currentNumber, bool hasMenuButton, ActionType actionType) + public static bool ActionFits(int width, float density, int currentNumber, bool hasMenuButton, ActionType actionType) { if (actionType == ActionType.Always) return true; @@ -39,13 +39,9 @@ public static bool ActionFits(Activity activity, int currentNumber, bool hasMenu if (actionType == ActionType.Never) return false; - if (activity == null) + if (density == 0.0) return true; - - var density = activity.Resources.DisplayMetrics.Density; - if (density == 0) - return true; - density = (int)(activity.Resources.DisplayMetrics.WidthPixels / density);//calculator DP of width. + density = (int)(width / density);//calculator DP of width. var max = 5; diff --git a/LegacyBar.Library/BarActions/OverflowLegacyBarAction.cs b/LegacyBar.Library/BarActions/OverflowLegacyBarAction.cs index 4667593..3297e55 100644 --- a/LegacyBar.Library/BarActions/OverflowLegacyBarAction.cs +++ b/LegacyBar.Library/BarActions/OverflowLegacyBarAction.cs @@ -29,7 +29,6 @@ public class OverflowLegacyBarAction : LegacyBarAction, AdapterView.IOnItemSelec public int MenuItemId; private readonly List _stringIds; private Spinner _overflowSpinner; - public Activity Activity { get; set; } public int Index { get; set; } public OverflowLegacyBarAction(Context context) { @@ -83,7 +82,7 @@ public override void PerformAction(View view) if(_overflowSpinner == null) return; - _overflowSpinner.Adapter = new OverflowSpinnerAdapter(Activity, _stringIds); + _overflowSpinner.Adapter = new OverflowSpinnerAdapter(Context, _stringIds); _firstClick = true; _overflowSpinner.SetSelection(0); _overflowSpinner.PerformClick(); @@ -91,6 +90,7 @@ public override void PerformAction(View view) } catch (Exception ex) { + //Todo: do something about me being empty! } } @@ -113,27 +113,32 @@ public void OnNothingSelected(AdapterView parent) public class OverflowSpinnerAdapter : BaseAdapter { - private readonly Activity _context; + private readonly Context _context; private readonly IEnumerable _items; - public OverflowSpinnerAdapter(Activity context, IEnumerable items) + public OverflowSpinnerAdapter(Context context, IEnumerable items) { + ResourceIdManager.UpdateIdValues(); _context = context; _items = items; } public override View GetView(int position, View convertView, ViewGroup parent) { + if (position < 0) return null; View view; var item = _items.ElementAt(position); + + var layoutInflater = LayoutInflater.From(_context); + if (!string.IsNullOrEmpty(item)) - view = _context.LayoutInflater.Inflate(Resource.Layout.spinneritem, parent, false); + view = layoutInflater.Inflate(Resource.Layout.spinneritem, parent, false); else { - view = _context.LayoutInflater.Inflate(Resource.Layout.blankspinner, parent, false);//hack to get first item blank. + view = layoutInflater.Inflate(Resource.Layout.blankspinner, parent, false);//hack to get first item blank. return view; } diff --git a/LegacyBar.Library/Resources/Resource.designer.cs b/LegacyBar.Library/Resources/Resource.designer.cs index 74f9413..65a44f3 100644 --- a/LegacyBar.Library/Resources/Resource.designer.cs +++ b/LegacyBar.Library/Resources/Resource.designer.cs @@ -15,15 +15,10 @@ namespace LegacyBar.Library { - [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")] + [System.CodeDom.Compiler.GeneratedCodeAttribute("Novell.MonoDroid.Build.Tasks", "1.0.0.0")] public partial class Resource { - Resource() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues (); - } - public partial class Attribute { diff --git a/LegacyBar.Sample/FragmentTabActivity.cs b/LegacyBar.Sample/FragmentTabActivity.cs index e497d4b..cee60b3 100644 --- a/LegacyBar.Sample/FragmentTabActivity.cs +++ b/LegacyBar.Sample/FragmentTabActivity.cs @@ -55,7 +55,6 @@ protected override void OnCreate(Bundle bundle) ActionBar = FindViewById(Resource.Id.actionbar); ActionBar.Title = "Look Fragments"; - ActionBar.CurrentActivity = this; AddHomeAction(typeof (HomeActivity), Resource.Drawable.icon); diff --git a/LegacyBar.Sample/HomeActivity.cs b/LegacyBar.Sample/HomeActivity.cs index ffd68d1..a6efcd3 100644 --- a/LegacyBar.Sample/HomeActivity.cs +++ b/LegacyBar.Sample/HomeActivity.cs @@ -38,7 +38,6 @@ protected override void OnCreate(Bundle bundle) SetContentView(Resource.Layout.main); LegacyBar = FindViewById(Resource.Id.actionbar); - LegacyBar.CurrentActivity = this; LegacyBar.SetHomeLogo(Resource.Drawable.icon); /* diff --git a/LegacyBar.Sample/OtherActivity.cs b/LegacyBar.Sample/OtherActivity.cs index 9c44cc3..e3394c0 100644 --- a/LegacyBar.Sample/OtherActivity.cs +++ b/LegacyBar.Sample/OtherActivity.cs @@ -62,7 +62,6 @@ protected override void OnCreate(Bundle savedInstanceState) //Set the Up button to go home, also much set current activity on the Legacy Bar AddHomeAction(typeof (HomeActivity), Resource.Drawable.icon); - LegacyBar.CurrentActivity = this; //always show the search icon no matter what. var itemActionBarAction = new MenuItemLegacyBarAction( diff --git a/LegacyBar.Sample/Resources/Resource.designer.cs b/LegacyBar.Sample/Resources/Resource.designer.cs index 823d248..bcf15e9 100644 --- a/LegacyBar.Sample/Resources/Resource.designer.cs +++ b/LegacyBar.Sample/Resources/Resource.designer.cs @@ -15,15 +15,10 @@ namespace LegacyBar.Sample { - [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")] + [System.CodeDom.Compiler.GeneratedCodeAttribute("Novell.MonoDroid.Build.Tasks", "1.0.0.0")] public partial class Resource { - Resource() - { - global::Android.Runtime.ResourceIdManager.UpdateIdValues (); - } - public static void UpdateIdValues() { LegacyBar.Library.Resource.Attribute.background = LegacyBar.Sample.Resource.Attribute.background;