Skip to content

Commit 7a534a8

Browse files
committed
#40 Added support for NON-AppCompat android activity (actionbar)
Backwards compatibility to PCL
1 parent bb966ff commit 7a534a8

21 files changed

+651
-339
lines changed

.build/Plugin.Badge.nuspec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<group targetFramework="netstandard1.4">
2323
<dependency id="Xamarin.Forms" version="2.5.0.280555" />
2424
</group>
25+
<group targetFramework="portable-net45+win+wpa81+wp80">
26+
<dependency id="Xamarin.Forms" version="2.5.0.280555" />
27+
</group>
2528
<group targetFramework="MonoAndroid10">
2629
<dependency id="Xamarin.Forms" version="2.5.0.280555" />
2730
</group>
@@ -39,6 +42,8 @@
3942
<files>
4043
<!-- .net standard -->
4144
<file src="out\lib\netstandard1.4\Plugin.Badge.*" target="lib\netstandard1.4" />
45+
<!-- PCL -->
46+
<file src="out\lib\netstandard1.4\Plugin.Badge.*" target="lib\portable-net45+win8+wpa81+wp8" />
4247
<!-- droid -->
4348
<file src="out\lib\android\Plugin.Badge.*" target="lib\MonoAndroid10" />
4449
<!-- iOS -->
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System.ComponentModel;
2+
using Android.Views;
3+
using Plugin.Badge.Abstractions;
4+
using Xamarin.Forms;
5+
using Xamarin.Forms.Platform.Android;
6+
using View = Android.Views.View;
7+
8+
namespace Plugin.Badge.Droid
9+
{
10+
internal static class BadgeViewExtensions
11+
{
12+
public static void UpdateFromElement(this BadgeView badgeView, Page element)
13+
{
14+
//get text
15+
var badgeText = TabBadge.GetBadgeText(element);
16+
badgeView.Text = badgeText;
17+
18+
// set color if not default
19+
var tabColor = TabBadge.GetBadgeColor(element);
20+
if (tabColor != Color.Default)
21+
{
22+
badgeView.BadgeColor = tabColor.ToAndroid();
23+
}
24+
25+
// set text color if not default
26+
var tabTextColor = TabBadge.GetBadgeTextColor(element);
27+
if (tabTextColor != Color.Default)
28+
{
29+
badgeView.TextColor = tabTextColor.ToAndroid();
30+
}
31+
32+
// set font if not default
33+
var font = TabBadge.GetBadgeFont(element);
34+
if (font != Font.Default)
35+
{
36+
badgeView.Typeface = font.ToTypeface();
37+
}
38+
39+
var margin = TabBadge.GetBadgeMargin(element);
40+
badgeView.SetMargins((float)margin.Left, (float)margin.Top, (float)margin.Right, (float)margin.Bottom);
41+
42+
// set position
43+
badgeView.Postion = TabBadge.GetBadgePosition(element);
44+
}
45+
46+
public static void UpdateFromPropertyChangedEvent(this BadgeView badgeView, Element element, PropertyChangedEventArgs e)
47+
{
48+
if (e.PropertyName == TabBadge.BadgeTextProperty.PropertyName)
49+
{
50+
badgeView.Text = TabBadge.GetBadgeText(element);
51+
return;
52+
}
53+
54+
if (e.PropertyName == TabBadge.BadgeColorProperty.PropertyName)
55+
{
56+
badgeView.BadgeColor = TabBadge.GetBadgeColor(element).ToAndroid();
57+
return;
58+
}
59+
60+
if (e.PropertyName == TabBadge.BadgeTextColorProperty.PropertyName)
61+
{
62+
badgeView.TextColor = TabBadge.GetBadgeTextColor(element).ToAndroid();
63+
return;
64+
}
65+
66+
if (e.PropertyName == TabBadge.BadgeFontProperty.PropertyName)
67+
{
68+
badgeView.Typeface = TabBadge.GetBadgeFont(element).ToTypeface();
69+
return;
70+
}
71+
72+
if (e.PropertyName == TabBadge.BadgePositionProperty.PropertyName)
73+
{
74+
badgeView.Postion = TabBadge.GetBadgePosition(element);
75+
return;
76+
}
77+
78+
if (e.PropertyName == TabBadge.BadgeMarginProperty.PropertyName)
79+
{
80+
var margin = TabBadge.GetBadgeMargin(element);
81+
badgeView.SetMargins((float)margin.Left, (float)margin.Top, (float)margin.Right, (float)margin.Bottom);
82+
return;
83+
}
84+
}
85+
86+
public static T FindChildOfType<T>(this ViewGroup parent) where T : View
87+
{
88+
if (parent == null)
89+
return null;
90+
91+
if (parent.ChildCount == 0)
92+
return null;
93+
94+
for (var i = 0; i < parent.ChildCount; i++)
95+
{
96+
var child = parent.GetChildAt(i);
97+
98+
99+
var typedChild = child as T;
100+
if (typedChild != null)
101+
{
102+
return typedChild;
103+
}
104+
105+
if (!(child is ViewGroup))
106+
continue;
107+
108+
109+
var result = FindChildOfType<T>(child as ViewGroup);
110+
if (result != null)
111+
return result;
112+
}
113+
114+
return null;
115+
}
116+
}
117+
}

Source/Plugin.Badge.Droid/BadgedTabbedPageRenderer.cs

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Android.Widget;
99
using Plugin.Badge.Abstractions;
1010
using Xamarin.Forms.Platform.Android;
11+
using Android.Content;
1112

1213
namespace Plugin.Badge.Droid
1314
{
@@ -18,6 +19,10 @@ public class BadgedTabbedPageRenderer : TabbedPageRenderer
1819
private TabLayout _tabLayout;
1920
private LinearLayout _tabStrip;
2021

22+
public BadgedTabbedPageRenderer(Context context) : base(context)
23+
{
24+
}
25+
2126
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
2227
{
2328
base.OnElementChanged(e);
@@ -66,88 +71,20 @@ private void AddTabBadge(int tabIndex)
6671

6772
BadgeViews[element] = badgeView;
6873

69-
//get text
70-
var badgeText = TabBadge.GetBadgeText(element);
71-
badgeView.Text = badgeText;
72-
73-
// set color if not default
74-
var tabColor = TabBadge.GetBadgeColor(element);
75-
if (tabColor != Color.Default)
76-
{
77-
badgeView.BadgeColor = tabColor.ToAndroid();
78-
}
79-
80-
// set text color if not default
81-
var tabTextColor = TabBadge.GetBadgeTextColor(element);
82-
if (tabTextColor != Color.Default)
83-
{
84-
badgeView.TextColor = tabTextColor.ToAndroid();
85-
}
86-
87-
// set font if not default
88-
var font = TabBadge.GetBadgeFont(element);
89-
if (font != Font.Default)
90-
{
91-
badgeView.Typeface = font.ToTypeface();
92-
}
93-
94-
var margin = TabBadge.GetBadgeMargin(element);
95-
badgeView.SetMargins((float)margin.Left, (float)margin.Top, (float)margin.Right, (float)margin.Bottom);
96-
97-
// set position
98-
badgeView.Postion = TabBadge.GetBadgePosition(element);
74+
badgeView.UpdateFromElement(element);
9975

76+
element.PropertyChanged -= OnTabbedPagePropertyChanged;
10077
element.PropertyChanged += OnTabbedPagePropertyChanged;
10178
}
10279

103-
104-
10580
protected virtual void OnTabbedPagePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
10681
{
107-
var element = sender as Element;
108-
if (element == null)
109-
return;
110-
111-
if (!BadgeViews.TryGetValue(element, out BadgeView badgeView))
112-
{
113-
return;
114-
}
115-
116-
if (e.PropertyName == TabBadge.BadgeTextProperty.PropertyName)
117-
{
118-
badgeView.Text = TabBadge.GetBadgeText(element);
82+
if (!(sender is Element element))
11983
return;
120-
}
12184

122-
if (e.PropertyName == TabBadge.BadgeColorProperty.PropertyName)
85+
if (BadgeViews.TryGetValue(element, out var badgeView))
12386
{
124-
badgeView.BadgeColor = TabBadge.GetBadgeColor(element).ToAndroid();
125-
return;
126-
}
127-
128-
if (e.PropertyName == TabBadge.BadgeTextColorProperty.PropertyName)
129-
{
130-
badgeView.TextColor = TabBadge.GetBadgeTextColor(element).ToAndroid();
131-
return;
132-
}
133-
134-
if (e.PropertyName == TabBadge.BadgeFontProperty.PropertyName)
135-
{
136-
badgeView.Typeface = TabBadge.GetBadgeFont(element).ToTypeface();
137-
return;
138-
}
139-
140-
if (e.PropertyName == TabBadge.BadgePositionProperty.PropertyName)
141-
{
142-
badgeView.Postion = TabBadge.GetBadgePosition(element);
143-
return;
144-
}
145-
146-
if (e.PropertyName == TabBadge.BadgeMarginProperty.PropertyName)
147-
{
148-
var margin = TabBadge.GetBadgeMargin(element);
149-
badgeView.SetMargins((float)margin.Left, (float)margin.Top, (float)margin.Right, (float)margin.Bottom);
150-
return;
87+
badgeView.UpdateFromPropertyChangedEvent(element, e);
15188
}
15289
}
15390

@@ -161,12 +98,10 @@ private async void OnTabAdded(object sender, ElementEventArgs e)
16198
{
16299
await Task.Delay(DeleayBeforeTabAdded);
163100

164-
var page = e.Element as Page;
165-
if (page == null)
101+
if (!(e.Element is Page page))
166102
return;
167103

168-
var tabIndex = Element.Children.IndexOf(page);
169-
AddTabBadge(tabIndex);
104+
AddTabBadge(Element.Children.IndexOf(page));
170105
}
171106

172107
protected override void Dispose(bool disposing)
@@ -192,6 +127,8 @@ private void Cleanup(TabbedPage page)
192127
page.ChildAdded -= OnTabAdded;
193128

194129
BadgeViews.Clear();
130+
_tabLayout = null;
131+
_tabStrip = null;
195132
}
196133
}
197134
}

0 commit comments

Comments
 (0)