Skip to content

Commit

Permalink
Merge pull request #33 from muak/fixCommand
Browse files Browse the repository at this point in the history
Fix command
  • Loading branch information
muak authored Oct 23, 2018
2 parents 31dc08c + ce6ebff commit 6714a2a
Show file tree
Hide file tree
Showing 94 changed files with 2,915 additions and 1,186 deletions.
333 changes: 23 additions & 310 deletions AiForms.Effects.Droid/AddCommandPlatformEffect.cs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions AiForms.Effects.Droid/AddDatePickerPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[assembly: ExportEffect(typeof(AddDatePickerPlatformEffect), nameof(AddDatePicker))]
namespace AiForms.Effects.Droid
{
[Android.Runtime.Preserve(AllMembers = true)]
public class AddDatePickerPlatformEffect : AiEffectBase
{
Android.Views.View _view;
Expand All @@ -17,6 +18,8 @@ public class AddDatePickerPlatformEffect : AiEffectBase

protected override void OnAttached()
{
base.OnAttached();

_view = Control ?? Container;

_view.Touch += _view_Touch;
Expand All @@ -29,13 +32,15 @@ protected override void OnDetached()
var renderer = Container as IVisualElementRenderer;
if (!IsDisposed) {
_view.Touch -= _view_Touch;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}
if (_dialog != null) {
_dialog.Dispose();
_dialog = null;
}
_view = null;
_command = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
5 changes: 5 additions & 0 deletions AiForms.Effects.Droid/AddNumberPickerPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[assembly: ExportEffect(typeof(AddNumberPickerPlatform), nameof(AddNumberPicker))]
namespace AiForms.Effects.Droid
{
[Android.Runtime.Preserve(AllMembers = true)]
public class AddNumberPickerPlatform : AiEffectBase
{
private AlertDialog _dialog;
Expand All @@ -22,6 +23,8 @@ public class AddNumberPickerPlatform : AiEffectBase

protected override void OnAttached()
{
base.OnAttached();

_view = Control ?? Container;

_view.Touch += _view_Touch;
Expand All @@ -41,13 +44,15 @@ protected override void OnDetached()
{
if (!IsDisposed) {
_view.Touch -= _view_Touch;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}
if (_dialog != null) {
_dialog.Dispose();
_dialog = null;
}
_view = null;
_command = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
5 changes: 5 additions & 0 deletions AiForms.Effects.Droid/AddTextPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[assembly: ExportEffect(typeof(AddTextPlatformEffect), nameof(AddText))]
namespace AiForms.Effects.Droid
{
[Android.Runtime.Preserve(AllMembers = true)]
public class AddTextPlatformEffect : AiEffectBase
{
private TextView _textView;
Expand All @@ -20,6 +21,8 @@ public class AddTextPlatformEffect : AiEffectBase

protected override void OnAttached()
{
base.OnAttached();

_container = Container;

_textView = new TextView(_context);
Expand Down Expand Up @@ -59,6 +62,7 @@ protected override void OnDetached()
Control.RemoveOnLayoutChangeListener(_fastListener);
_fastListener.CleanUp();
}
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}

_listener?.Dispose();
Expand All @@ -69,6 +73,7 @@ protected override void OnDetached()

_fastListener?.Dispose();
_fastListener = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
Expand Down
5 changes: 5 additions & 0 deletions AiForms.Effects.Droid/AddTimePickerPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[assembly: ExportEffect(typeof(AddTimePickerPlatformEffect), nameof(AddTimePicker))]
namespace AiForms.Effects.Droid
{
[Android.Runtime.Preserve(AllMembers = true)]
public class AddTimePickerPlatformEffect : AiEffectBase
{
Android.Views.View _view;
Expand All @@ -19,6 +20,8 @@ public class AddTimePickerPlatformEffect : AiEffectBase

protected override void OnAttached()
{
base.OnAttached();

_view = Control ?? Container;

_view.Touch += _view_Touch;
Expand All @@ -31,13 +34,15 @@ protected override void OnDetached()
{
if (!IsDisposed) {
_view.Touch -= _view_Touch;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}
if (_dialog != null) {
_dialog.Dispose();
_dialog = null;
}
_view = null;
_command = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
15 changes: 14 additions & 1 deletion AiForms.Effects.Droid/AddTouchPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
[assembly: ExportEffect(typeof(AddTouchPlatformEffect), nameof(AddTouch))]
namespace AiForms.Effects.Droid
{
public class AddTouchPlatformEffect:PlatformEffect
[Android.Runtime.Preserve(AllMembers = true)]
public class AddTouchPlatformEffect:AiEffectBase
{
WeakReference<Android.Views.View> _viewRef;
TouchRecognizer _recognizer;
Context _context;

protected override void OnAttached()
{
base.OnAttached();

_viewRef = new WeakReference<Android.Views.View>(Control ?? Container);


Expand Down Expand Up @@ -62,9 +65,19 @@ void _view_Touch(object sender, Android.Views.View.TouchEventArgs e)

protected override void OnDetached()
{
if(!IsDisposed)
{
if (_viewRef.TryGetTarget(out var view))
{
view.Touch -= _view_Touch;
}
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}
Element.ClearValue(AddTouch.RecognizerProperty);
_context = null;
_recognizer = null;
_viewRef = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");
}

}
Expand Down
48 changes: 48 additions & 0 deletions AiForms.Effects.Droid/AiEffectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Linq;
using Xamarin.Forms;

namespace AiForms.Effects.Droid
{
[Android.Runtime.Preserve(AllMembers = true)]
public abstract class AiEffectBase : PlatformEffect
{
public static bool IsFastRenderers = global::Xamarin.Forms.Forms.Flags.Any(x => x == "FastRenderers_Experimental");

IVisualElementRenderer _renderer;
bool _isDisposed = false;
WeakReference<NavigationPage> _navigationRef;

protected bool IsDisposed {
get {
if (_isDisposed) {
Expand All @@ -30,6 +34,21 @@ protected bool IsDisposed {
}
}

protected override void OnAttached()
{
var visual = Element as VisualElement;
var naviCandidate = visual.Navigation.NavigationStack.FirstOrDefault()?.Parent as NavigationPage;
if(naviCandidate != null)
{
naviCandidate.Popped += PagePopped;
_navigationRef = new WeakReference<NavigationPage>(naviCandidate);
}

// Use not Popped but Popping because it is too late.
Xamarin.Forms.Application.Current.ModalPopping += ModalPopping;
}


// whether Element is FastRenderer.(Exept Button)
protected bool IsFastRenderer{
get{
Expand Down Expand Up @@ -82,5 +101,34 @@ Func<object, object> CreateGetField(Type t)

return lambda.Compile();
}

void PagePopped(object sender, NavigationEventArgs e)
{
Clear();
}

void ModalPopping(object sender, ModalPoppingEventArgs e)
{
Clear();
}

void Clear()
{
if (_navigationRef != null && _navigationRef.TryGetTarget(out var navi))
{
navi.Popped -= PagePopped;
}
Xamarin.Forms.Application.Current.ModalPopping -= ModalPopping;
_navigationRef = null;

// For Android, when a page is popped, OnDetached is automatically not called. (when iOS, it is called)
// So, made the Popped & ModalPopped event subscribe in advance
// and make the effect manually removed when the page is popped.
if (IsAttached && !IsDisposed)
{
var toRemove = Element.Effects.OfType<AiRoutingEffectBase>().FirstOrDefault(x => x.EffectId == ResolveId);
Element.Effects.Remove(toRemove);
}
}
}
}
Loading

0 comments on commit 6714a2a

Please sign in to comment.