Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

Commit

Permalink
Removed dependency of having to set CurrentActivity for LegacyBar.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheesebaron committed Apr 9, 2013
1 parent ed9beb4 commit 8ced531
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 43 deletions.
21 changes: 6 additions & 15 deletions LegacyBar.Library/Bar/LegacyBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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);
Expand All @@ -234,10 +229,10 @@ public LegacyBar(Context context, IAttributeSet attrs)
_progress = _barView.FindViewById<ProgressBar>(Resource.Id.actionbar_progress);
_titleLayout = _barView.FindViewById<RelativeLayout>(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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -554,7 +550,6 @@ private View InflateOverflowAction(LegacyBarAction legacyBarAction)
labelView.SetOnClickListener(this);
//view.SetOnLongClickListener(this);

_overflowLegacyBarAction.Activity = CurrentActivity;
return view;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -611,7 +603,6 @@ protected override void Dispose(bool disposing)
_homeLayout = null;
_progress = null;
_titleLayout = null;
_context = null;
_overflowLegacyBarAction = null;
}

Expand Down
10 changes: 3 additions & 7 deletions LegacyBar.Library/Bar/LegacyBarUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,17 @@ public class LegacyBarUtils
/// <param name="hasMenuButton"></param>
/// <param name="actionType"></param>
/// <returns>If it will fit :)</returns>
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;

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;
Expand Down
17 changes: 11 additions & 6 deletions LegacyBar.Library/BarActions/OverflowLegacyBarAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class OverflowLegacyBarAction : LegacyBarAction, AdapterView.IOnItemSelec
public int MenuItemId;
private readonly List<string> _stringIds;
private Spinner _overflowSpinner;
public Activity Activity { get; set; }
public int Index { get; set; }
public OverflowLegacyBarAction(Context context)
{
Expand Down Expand Up @@ -83,14 +82,15 @@ 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();

}
catch (Exception ex)
{
//Todo: do something about me being empty!
}
}

Expand All @@ -113,27 +113,32 @@ public void OnNothingSelected(AdapterView parent)

public class OverflowSpinnerAdapter : BaseAdapter
{
private readonly Activity _context;
private readonly Context _context;
private readonly IEnumerable<string> _items;

public OverflowSpinnerAdapter(Activity context, IEnumerable<string> items)
public OverflowSpinnerAdapter(Context context, IEnumerable<string> 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;
}

Expand Down
7 changes: 1 addition & 6 deletions LegacyBar.Library/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion LegacyBar.Sample/FragmentTabActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ protected override void OnCreate(Bundle bundle)

ActionBar = FindViewById<Library.Bar.LegacyBar>(Resource.Id.actionbar);
ActionBar.Title = "Look Fragments";
ActionBar.CurrentActivity = this;
AddHomeAction(typeof (HomeActivity), Resource.Drawable.icon);


Expand Down
1 change: 0 additions & 1 deletion LegacyBar.Sample/HomeActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ protected override void OnCreate(Bundle bundle)
SetContentView(Resource.Layout.main);

LegacyBar = FindViewById<Library.Bar.LegacyBar>(Resource.Id.actionbar);
LegacyBar.CurrentActivity = this;
LegacyBar.SetHomeLogo(Resource.Drawable.icon);

/*
Expand Down
1 change: 0 additions & 1 deletion LegacyBar.Sample/OtherActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 1 addition & 6 deletions LegacyBar.Sample/Resources/Resource.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ced531

Please sign in to comment.