Skip to content

Commit fbeffa3

Browse files
authored
Merge pull request #36 from muak/development
Development
2 parents 2274dca + e5e8987 commit fbeffa3

File tree

65 files changed

+1437
-149
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1437
-149
lines changed

AiForms.Effects.Droid/AddCommandPlatformEffect.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ protected override void OnAttached()
6868

6969
protected override void OnDetached()
7070
{
71-
base.OnDetached();
72-
73-
System.Diagnostics.Debug.WriteLine(Element.GetType().FullName);
7471
if (!IsDisposed) {
7572
_view.Touch -= _view_Touch;
7673
}
@@ -92,6 +89,8 @@ protected override void OnDetached()
9289
_gestureDetector = null;
9390

9491
_view = null;
92+
93+
base.OnDetached();
9594
}
9695

9796
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)

AiForms.Effects.Droid/AddDatePickerPlatformEffect.cs

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ protected override void OnDetached()
4141
_view = null;
4242
_command = null;
4343
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
44+
45+
base.OnDetached();
4446
}
4547

4648
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)

AiForms.Effects.Droid/AddNumberPickerPlatform.cs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ protected override void OnDetached()
5353
_view = null;
5454
_command = null;
5555
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
56+
57+
base.OnDetached();
5658
}
5759

5860
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)

AiForms.Effects.Droid/AddTextPlatformEffect.cs

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ protected override void OnDetached()
7474
_fastListener?.Dispose();
7575
_fastListener = null;
7676
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
77+
78+
base.OnDetached();
7779
}
7880

7981
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)

AiForms.Effects.Droid/AddTimePickerPlatformEffect.cs

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ protected override void OnDetached()
4343
_view = null;
4444
_command = null;
4545
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
46+
47+
base.OnDetached();
4648
}
4749

4850
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)

AiForms.Effects.Droid/AddTouchPlatformEffect.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void _view_Touch(object sender, Android.Views.View.TouchEventArgs e)
6565

6666
protected override void OnDetached()
6767
{
68-
if(!IsDisposed)
68+
if (!IsDisposed)
6969
{
7070
if (_viewRef.TryGetTarget(out var view))
7171
{
@@ -78,6 +78,8 @@ protected override void OnDetached()
7878
_recognizer = null;
7979
_viewRef = null;
8080
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
81+
82+
base.OnDetached();
8183
}
8284

8385
}

AiForms.Effects.Droid/AiEffectBase.cs

+111-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using System.Linq;
66
using Xamarin.Forms;
7+
using Android.App;
78

89
namespace AiForms.Effects.Droid
910
{
@@ -14,7 +15,6 @@ public abstract class AiEffectBase : PlatformEffect
1415

1516
IVisualElementRenderer _renderer;
1617
bool _isDisposed = false;
17-
WeakReference<NavigationPage> _navigationRef;
1818

1919
protected bool IsDisposed {
2020
get {
@@ -34,18 +34,45 @@ protected bool IsDisposed {
3434
}
3535
}
3636

37+
protected bool IsNullOrDisposed{
38+
get{
39+
if(Element.BindingContext == null){
40+
return true;
41+
}
42+
43+
return IsDisposed;
44+
}
45+
}
46+
3747
protected override void OnAttached()
3848
{
3949
var visual = Element as VisualElement;
40-
var naviCandidate = visual.Navigation.NavigationStack.FirstOrDefault()?.Parent as NavigationPage;
41-
if(naviCandidate != null)
50+
var page = visual.Navigation.NavigationStack.LastOrDefault();
51+
if(page == null)
4252
{
43-
naviCandidate.Popped += PagePopped;
44-
_navigationRef = new WeakReference<NavigationPage>(naviCandidate);
53+
page = visual.Navigation.ModalStack.LastOrDefault();
54+
if (page == null) {
55+
// In case the element in DataTemplate, NavigationProxycan't be got.
56+
// Instead of it, the page dismissal is judged by whether the BindingContext is null.
57+
Element.BindingContextChanged += BindingContextChanged;
58+
return;
59+
}
4560
}
4661

47-
// Use not Popped but Popping because it is too late.
48-
Xamarin.Forms.Application.Current.ModalPopping += ModalPopping;
62+
// To call certainly a OnDetached method when the page is popped,
63+
// it executes the process removing all the effects in the page at once with Attached bindable property.
64+
if (!GetIsRegistered(page))
65+
{
66+
SetIsRegistered(page, true);
67+
}
68+
}
69+
70+
protected override void OnDetached()
71+
{
72+
System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}");
73+
Element.BindingContextChanged -= BindingContextChanged;
74+
75+
_renderer = null;
4976
}
5077

5178

@@ -102,32 +129,94 @@ Func<object, object> CreateGetField(Type t)
102129
return lambda.Compile();
103130
}
104131

105-
void PagePopped(object sender, NavigationEventArgs e)
132+
void BindingContextChanged(object sender, EventArgs e)
133+
{
134+
if (Element.BindingContext != null)
135+
return;
136+
137+
// For Android, when a page is popped, OnDetached is automatically not called. (when iOS, it is called)
138+
// So, made the BindingContextChanged event subscribe in advance
139+
// and make the effect manually removed when the BindingContext is null.
140+
// However, there is the problem that it isn't called when the BindingContext remains null all along.
141+
// The above solution is to use NavigationPage.Popped or Application.ModalPopping event.
142+
// That's why the following code runs only when the element is in a DataTemplate.
143+
if (IsAttached)
144+
{
145+
var toRemove = Element.Effects.OfType<AiRoutingEffectBase>().FirstOrDefault(x => x.EffectId == ResolveId);
146+
Device.BeginInvokeOnMainThread(() => Element.Effects.Remove(toRemove));
147+
}
148+
}
149+
150+
internal static readonly BindableProperty IsRegisteredProperty =
151+
BindableProperty.CreateAttached(
152+
"IsRegistered",
153+
typeof(bool),
154+
typeof(AiEffectBase),
155+
default(bool),
156+
propertyChanged: IsRegisteredPropertyChanged
157+
);
158+
159+
internal static void SetIsRegistered(BindableObject view, bool value)
106160
{
107-
Clear();
161+
view.SetValue(IsRegisteredProperty, value);
108162
}
109163

110-
void ModalPopping(object sender, ModalPoppingEventArgs e)
164+
internal static bool GetIsRegistered(BindableObject view)
111165
{
112-
Clear();
166+
return (bool)view.GetValue(IsRegisteredProperty);
113167
}
114168

115-
void Clear()
169+
static void IsRegisteredPropertyChanged(BindableObject bindable, object oldValue, object newValue)
116170
{
117-
if (_navigationRef != null && _navigationRef.TryGetTarget(out var navi))
171+
if (!(bool)newValue) return;
172+
173+
var page = bindable as Page;
174+
175+
if (page.Parent is NavigationPage navi)
118176
{
119-
navi.Popped -= PagePopped;
177+
navi.Popped += NaviPopped;
178+
}
179+
else
180+
{
181+
Xamarin.Forms.Application.Current.ModalPopping += ModalPopping;
120182
}
121-
Xamarin.Forms.Application.Current.ModalPopping -= ModalPopping;
122-
_navigationRef = null;
123183

124-
// For Android, when a page is popped, OnDetached is automatically not called. (when iOS, it is called)
125-
// So, made the Popped & ModalPopped event subscribe in advance
126-
// and make the effect manually removed when the page is popped.
127-
if (IsAttached && !IsDisposed)
184+
void NaviPopped(object sender, NavigationEventArgs e)
128185
{
129-
var toRemove = Element.Effects.OfType<AiRoutingEffectBase>().FirstOrDefault(x => x.EffectId == ResolveId);
130-
Element.Effects.Remove(toRemove);
186+
if (e.Page != page)
187+
return;
188+
189+
navi.Popped -= NaviPopped;
190+
191+
RemoveEffects();
192+
}
193+
194+
void ModalPopping(object sender, ModalPoppingEventArgs e)
195+
{
196+
if (e.Modal != page)
197+
return;
198+
199+
Xamarin.Forms.Application.Current.ModalPopping -= ModalPopping;
200+
201+
RemoveEffects();
202+
}
203+
204+
void RemoveEffects()
205+
{
206+
foreach (var child in page.Descendants())
207+
{
208+
foreach (var effect in child.Effects.OfType<AiRoutingEffectBase>())
209+
{
210+
Device.BeginInvokeOnMainThread(() => child.Effects.Remove(effect));
211+
}
212+
}
213+
214+
foreach(var effect in page.Effects.OfType<AiRoutingEffectBase>())
215+
{
216+
Device.BeginInvokeOnMainThread(() => page.Effects.Remove(effect));
217+
}
218+
219+
page.ClearValue(IsRegisteredProperty);
131220
}
132221
}
133222
}

AiForms.Effects.Droid/AiForms.Effects.Droid.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
<OutputPath>bin\Release</OutputPath>
3737
<ErrorReport>prompt</ErrorReport>
3838
<WarningLevel>4</WarningLevel>
39+
<DocumentationFile>bin\Release\AiForms.Effects.Droid.xml</DocumentationFile>
3940
<AndroidManagedSymbols>true</AndroidManagedSymbols>
4041
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
42+
<NoWarn>1591</NoWarn>
4143
</PropertyGroup>
4244
<ItemGroup>
4345
<Reference Include="System" />

AiForms.Effects.Droid/AlterColorPlatformEffect.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ protected override void OnDetached()
4545
_effect?.OnDetached();
4646
_effect = null;
4747
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
48+
49+
base.OnDetached();
4850
}
4951

5052
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
5153
{
5254
base.OnElementPropertyChanged(args);
5355

54-
if (IsDisposed) {
56+
if (IsNullOrDisposed) {
5557
return;
5658
}
5759

AiForms.Effects.Droid/AlterColorSlider.cs

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using Android.Graphics;
1+
using Android.Content.Res;
2+
using Android.Graphics;
23
using Android.Graphics.Drawables;
34
using Android.Widget;
45
using Xamarin.Forms;
56
using Xamarin.Forms.Platform.Android;
67

78
namespace AiForms.Effects.Droid
8-
{
9+
{
10+
// References:
11+
// http://www.zoftino.com/android-seekbar-and-custom-seekbar-examples
912
[Android.Runtime.Preserve(AllMembers = true)]
1013
public class AlterColorSlider : IAiEffectDroid
1114
{
@@ -16,9 +19,8 @@ public class AlterColorSlider : IAiEffectDroid
1619
Drawable _orgThumb;
1720

1821
LayerDrawable _progress;
19-
Drawable _minDrawable;
20-
Drawable _maxDrawable;
21-
Drawable _thumb;
22+
Drawable _thumb;
23+
ColorStateList _orgProgressBackground;
2224

2325
bool notSupported = false;
2426

@@ -34,22 +36,22 @@ public AlterColorSlider(SeekBar seekbar, Element element)
3436
}
3537

3638
_progress = (LayerDrawable)(_seekbar.ProgressDrawable.Current.GetConstantState().NewDrawable());
37-
38-
_minDrawable = _progress.GetDrawable(2);
39-
_maxDrawable = _progress.GetDrawable(0);
39+
40+
_orgProgressBackground = _seekbar.ProgressBackgroundTintList;
4041

4142
_orgThumb = _seekbar.Thumb;
4243
_thumb = _seekbar.Thumb.GetConstantState().NewDrawable();
4344

44-
_seekbar.ProgressDrawable = _progress;
45+
_seekbar.ProgressDrawable = _progress;
4546
_seekbar.SetThumb(_thumb);
4647
}
4748

4849
public void OnDetachedIfNotDisposed()
4950
{
5051
if (notSupported) {
5152
return;
52-
}
53+
}
54+
_seekbar.ProgressBackgroundTintList = _orgProgressBackground;
5355
_seekbar.ProgressDrawable = _orgProgress;
5456
_seekbar.SetThumb(_orgThumb);
5557
}
@@ -60,20 +62,14 @@ public void OnDetached()
6062
return;
6163
}
6264

63-
_minDrawable.ClearColorFilter();
64-
_maxDrawable.ClearColorFilter();
65-
66-
_minDrawable.Dispose();
67-
_maxDrawable.Dispose();
6865
_thumb.Dispose();
6966
_progress.Dispose();
7067

71-
_minDrawable = null;
72-
_maxDrawable = null;
7368
_thumb = null;
7469
_progress = null;
7570
_orgProgress = null;
76-
_orgThumb = null;
71+
_orgThumb = null;
72+
_orgProgressBackground = null;
7773
_seekbar = null;
7874
_element = null;
7975
}
@@ -83,12 +79,19 @@ public void Update()
8379
if (notSupported) {
8480
return;
8581
}
86-
var color = AlterColor.GetAccent(_element).ToAndroid();
87-
var altColor = Android.Graphics.Color.Argb(76, color.R, color.G, color.B);
82+
var color = AlterColor.GetAccent(_element).ToAndroid();
83+
84+
_progress.SetColorFilter(color, PorterDuff.Mode.SrcIn);
85+
_seekbar.ProgressBackgroundTintList = new ColorStateList(
86+
new int[][]
87+
{
88+
new int[]{}
89+
},
90+
new int[]
91+
{
92+
color,
93+
});
8894

89-
//if use SetTint,it cannot restore.
90-
_minDrawable.SetColorFilter(color, PorterDuff.Mode.SrcIn);
91-
_maxDrawable.SetColorFilter(altColor, PorterDuff.Mode.SrcIn);
9295
_thumb.SetTint(color);
9396
}
9497

AiForms.Effects.Droid/AlterLineHeightPlatformEffect.cs

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ protected override void OnDetached()
3434
_effect?.OnDetached();
3535
_effect = null;
3636
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
37+
38+
base.OnDetached();
3739
}
3840

3941

0 commit comments

Comments
 (0)