Skip to content

Commit

Permalink
Merge pull request #36 from muak/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
muak authored Nov 1, 2018
2 parents 2274dca + e5e8987 commit fbeffa3
Show file tree
Hide file tree
Showing 65 changed files with 1,437 additions and 149 deletions.
5 changes: 2 additions & 3 deletions AiForms.Effects.Droid/AddCommandPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ protected override void OnAttached()

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

System.Diagnostics.Debug.WriteLine(Element.GetType().FullName);
if (!IsDisposed) {
_view.Touch -= _view_Touch;
}
Expand All @@ -92,6 +89,8 @@ protected override void OnDetached()
_gestureDetector = null;

_view = null;

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions AiForms.Effects.Droid/AddDatePickerPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected override void OnDetached()
_view = null;
_command = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions AiForms.Effects.Droid/AddNumberPickerPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ protected override void OnDetached()
_view = null;
_command = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
2 changes: 2 additions & 0 deletions AiForms.Effects.Droid/AddTextPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ protected override void OnDetached()
_fastListener?.Dispose();
_fastListener = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
Expand Down
2 changes: 2 additions & 0 deletions AiForms.Effects.Droid/AddTimePickerPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ protected override void OnDetached()
_view = null;
_command = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
Expand Down
4 changes: 3 additions & 1 deletion AiForms.Effects.Droid/AddTouchPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void _view_Touch(object sender, Android.Views.View.TouchEventArgs e)

protected override void OnDetached()
{
if(!IsDisposed)
if (!IsDisposed)
{
if (_viewRef.TryGetTarget(out var view))
{
Expand All @@ -78,6 +78,8 @@ protected override void OnDetached()
_recognizer = null;
_viewRef = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

}
Expand Down
133 changes: 111 additions & 22 deletions AiForms.Effects.Droid/AiEffectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Linq;
using Xamarin.Forms;
using Android.App;

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

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

protected bool IsDisposed {
get {
Expand All @@ -34,18 +34,45 @@ protected bool IsDisposed {
}
}

protected bool IsNullOrDisposed{
get{
if(Element.BindingContext == null){
return true;
}

return IsDisposed;
}
}

protected override void OnAttached()
{
var visual = Element as VisualElement;
var naviCandidate = visual.Navigation.NavigationStack.FirstOrDefault()?.Parent as NavigationPage;
if(naviCandidate != null)
var page = visual.Navigation.NavigationStack.LastOrDefault();
if(page == null)
{
naviCandidate.Popped += PagePopped;
_navigationRef = new WeakReference<NavigationPage>(naviCandidate);
page = visual.Navigation.ModalStack.LastOrDefault();
if (page == null) {
// In case the element in DataTemplate, NavigationProxycan't be got.
// Instead of it, the page dismissal is judged by whether the BindingContext is null.
Element.BindingContextChanged += BindingContextChanged;
return;
}
}

// Use not Popped but Popping because it is too late.
Xamarin.Forms.Application.Current.ModalPopping += ModalPopping;
// To call certainly a OnDetached method when the page is popped,
// it executes the process removing all the effects in the page at once with Attached bindable property.
if (!GetIsRegistered(page))
{
SetIsRegistered(page, true);
}
}

protected override void OnDetached()
{
System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}");
Element.BindingContextChanged -= BindingContextChanged;

_renderer = null;
}


Expand Down Expand Up @@ -102,32 +129,94 @@ Func<object, object> CreateGetField(Type t)
return lambda.Compile();
}

void PagePopped(object sender, NavigationEventArgs e)
void BindingContextChanged(object sender, EventArgs e)
{
if (Element.BindingContext != null)
return;

// For Android, when a page is popped, OnDetached is automatically not called. (when iOS, it is called)
// So, made the BindingContextChanged event subscribe in advance
// and make the effect manually removed when the BindingContext is null.
// However, there is the problem that it isn't called when the BindingContext remains null all along.
// The above solution is to use NavigationPage.Popped or Application.ModalPopping event.
// That's why the following code runs only when the element is in a DataTemplate.
if (IsAttached)
{
var toRemove = Element.Effects.OfType<AiRoutingEffectBase>().FirstOrDefault(x => x.EffectId == ResolveId);
Device.BeginInvokeOnMainThread(() => Element.Effects.Remove(toRemove));
}
}

internal static readonly BindableProperty IsRegisteredProperty =
BindableProperty.CreateAttached(
"IsRegistered",
typeof(bool),
typeof(AiEffectBase),
default(bool),
propertyChanged: IsRegisteredPropertyChanged
);

internal static void SetIsRegistered(BindableObject view, bool value)
{
Clear();
view.SetValue(IsRegisteredProperty, value);
}

void ModalPopping(object sender, ModalPoppingEventArgs e)
internal static bool GetIsRegistered(BindableObject view)
{
Clear();
return (bool)view.GetValue(IsRegisteredProperty);
}

void Clear()
static void IsRegisteredPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (_navigationRef != null && _navigationRef.TryGetTarget(out var navi))
if (!(bool)newValue) return;

var page = bindable as Page;

if (page.Parent is NavigationPage navi)
{
navi.Popped -= PagePopped;
navi.Popped += NaviPopped;
}
else
{
Xamarin.Forms.Application.Current.ModalPopping += ModalPopping;
}
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)
void NaviPopped(object sender, NavigationEventArgs e)
{
var toRemove = Element.Effects.OfType<AiRoutingEffectBase>().FirstOrDefault(x => x.EffectId == ResolveId);
Element.Effects.Remove(toRemove);
if (e.Page != page)
return;

navi.Popped -= NaviPopped;

RemoveEffects();
}

void ModalPopping(object sender, ModalPoppingEventArgs e)
{
if (e.Modal != page)
return;

Xamarin.Forms.Application.Current.ModalPopping -= ModalPopping;

RemoveEffects();
}

void RemoveEffects()
{
foreach (var child in page.Descendants())
{
foreach (var effect in child.Effects.OfType<AiRoutingEffectBase>())
{
Device.BeginInvokeOnMainThread(() => child.Effects.Remove(effect));
}
}

foreach(var effect in page.Effects.OfType<AiRoutingEffectBase>())
{
Device.BeginInvokeOnMainThread(() => page.Effects.Remove(effect));
}

page.ClearValue(IsRegisteredProperty);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions AiForms.Effects.Droid/AiForms.Effects.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\AiForms.Effects.Droid.xml</DocumentationFile>
<AndroidManagedSymbols>true</AndroidManagedSymbols>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down
4 changes: 3 additions & 1 deletion AiForms.Effects.Droid/AlterColorPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ protected override void OnDetached()
_effect?.OnDetached();
_effect = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
{
base.OnElementPropertyChanged(args);

if (IsDisposed) {
if (IsNullOrDisposed) {
return;
}

Expand Down
49 changes: 26 additions & 23 deletions AiForms.Effects.Droid/AlterColorSlider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Android.Graphics;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

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

LayerDrawable _progress;
Drawable _minDrawable;
Drawable _maxDrawable;
Drawable _thumb;
Drawable _thumb;
ColorStateList _orgProgressBackground;

bool notSupported = false;

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

_progress = (LayerDrawable)(_seekbar.ProgressDrawable.Current.GetConstantState().NewDrawable());

_minDrawable = _progress.GetDrawable(2);
_maxDrawable = _progress.GetDrawable(0);

_orgProgressBackground = _seekbar.ProgressBackgroundTintList;

_orgThumb = _seekbar.Thumb;
_thumb = _seekbar.Thumb.GetConstantState().NewDrawable();

_seekbar.ProgressDrawable = _progress;
_seekbar.ProgressDrawable = _progress;
_seekbar.SetThumb(_thumb);
}

public void OnDetachedIfNotDisposed()
{
if (notSupported) {
return;
}
}
_seekbar.ProgressBackgroundTintList = _orgProgressBackground;
_seekbar.ProgressDrawable = _orgProgress;
_seekbar.SetThumb(_orgThumb);
}
Expand All @@ -60,20 +62,14 @@ public void OnDetached()
return;
}

_minDrawable.ClearColorFilter();
_maxDrawable.ClearColorFilter();

_minDrawable.Dispose();
_maxDrawable.Dispose();
_thumb.Dispose();
_progress.Dispose();

_minDrawable = null;
_maxDrawable = null;
_thumb = null;
_progress = null;
_orgProgress = null;
_orgThumb = null;
_orgThumb = null;
_orgProgressBackground = null;
_seekbar = null;
_element = null;
}
Expand All @@ -83,12 +79,19 @@ public void Update()
if (notSupported) {
return;
}
var color = AlterColor.GetAccent(_element).ToAndroid();
var altColor = Android.Graphics.Color.Argb(76, color.R, color.G, color.B);
var color = AlterColor.GetAccent(_element).ToAndroid();

_progress.SetColorFilter(color, PorterDuff.Mode.SrcIn);
_seekbar.ProgressBackgroundTintList = new ColorStateList(
new int[][]
{
new int[]{}
},
new int[]
{
color,
});

//if use SetTint,it cannot restore.
_minDrawable.SetColorFilter(color, PorterDuff.Mode.SrcIn);
_maxDrawable.SetColorFilter(altColor, PorterDuff.Mode.SrcIn);
_thumb.SetTint(color);
}

Expand Down
2 changes: 2 additions & 0 deletions AiForms.Effects.Droid/AlterLineHeightPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ protected override void OnDetached()
_effect?.OnDetached();
_effect = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}


Expand Down
Loading

0 comments on commit fbeffa3

Please sign in to comment.