Skip to content

Commit

Permalink
Fix Border / AlterColor
Browse files Browse the repository at this point in the history
  • Loading branch information
kamu authored and kamu committed Nov 1, 2018
1 parent f490030 commit 27fd776
Show file tree
Hide file tree
Showing 36 changed files with 182 additions and 94 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
4 changes: 2 additions & 2 deletions AiForms.Effects.Droid/AddDatePickerPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ protected override void OnAttached()

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

var renderer = Container as IVisualElementRenderer;
if (!IsDisposed) {
_view.Touch -= _view_Touch;
Expand All @@ -43,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
4 changes: 2 additions & 2 deletions AiForms.Effects.Droid/AddNumberPickerPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ void _view_Touch(object sender, Android.Views.View.TouchEventArgs e)

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

if (!IsDisposed) {
_view.Touch -= _view_Touch;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
Expand All @@ -55,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
3 changes: 2 additions & 1 deletion AiForms.Effects.Droid/AddTextPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ protected override void OnAttached()

protected override void OnDetached()
{
base.OnDetached();
System.Diagnostics.Debug.WriteLine(Element.GetType().FullName);

if (!IsDisposed) {
Expand All @@ -75,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
4 changes: 2 additions & 2 deletions AiForms.Effects.Droid/AddTimePickerPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ protected override void OnAttached()

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

if (!IsDisposed) {
_view.Touch -= _view_Touch;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
Expand All @@ -45,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: 2 additions & 2 deletions AiForms.Effects.Droid/AddTouchPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ void _view_Touch(object sender, Android.Views.View.TouchEventArgs e)

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

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

base.OnDetached();
}

}
Expand Down
40 changes: 24 additions & 16 deletions AiForms.Effects.Droid/AiEffectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public abstract class AiEffectBase : PlatformEffect

IVisualElementRenderer _renderer;
bool _isDisposed = false;
WeakReference<Page> _pageRef;

protected bool IsDisposed {
get {
Expand Down Expand Up @@ -45,12 +46,29 @@ protected bool IsNullOrDisposed{

protected override void OnAttached()
{
Element.BindingContextChanged += BindingContextChanged;
var visual = Element as VisualElement;

var page = visual.Navigation.NavigationStack.LastOrDefault() ??
visual.Navigation.ModalStack.LastOrDefault();

if (page == null)
return;

page.Disappearing += Page_Disappearing;

_pageRef = new WeakReference<Page>(page);
}

protected override void OnDetached()
{
Element.BindingContextChanged -= BindingContextChanged;
System.Diagnostics.Debug.WriteLine($"Detached {GetType().Name} from {Element.GetType().FullName}");
if (_pageRef != null && _pageRef.TryGetTarget(out var page))
{
page.Disappearing -= Page_Disappearing;
}

_renderer = null;
_pageRef = null;
}


Expand Down Expand Up @@ -107,25 +125,15 @@ Func<object, object> CreateGetField(Type t)
return lambda.Compile();
}

void BindingContextChanged(object sender, EventArgs e)
{
if (Element.BindingContext == null)
{
Clear();
}
}

void Clear()
void Page_Disappearing(object sender, EventArgs e)
{
Element.BindingContextChanged -= BindingContextChanged;

// 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.
// So, made the Page.Disappearing 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);
Device.BeginInvokeOnMainThread(()=>Element.Effects.Remove(toRemove));
Device.BeginInvokeOnMainThread(() => Element.Effects.Remove(toRemove));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions AiForms.Effects.Droid/AlterColorPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ protected override void OnAttached()

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

if (!IsDisposed) {
_effect?.OnDetachedIfNotDisposed();
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}
_effect?.OnDetached();
_effect = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
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
10 changes: 0 additions & 10 deletions AiForms.Effects.Droid/AlterColorStatusbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,26 @@ public class AlterColorStatusbar : IAiEffectDroid

public AlterColorStatusbar(Element element,Context context)
{
System.Diagnostics.Debug.WriteLine("Constructor Start");

_window = (context as FormsAppCompatActivity).Window;
_element = element;
_orgColor = _window.StatusBarColor;

System.Diagnostics.Debug.WriteLine("Constructor Completed");
}

public void OnDetachedIfNotDisposed() { }

public void OnDetached()
{
System.Diagnostics.Debug.WriteLine("OnDetached Start");
var color = new Android.Graphics.Color(_orgColor);
_window.SetStatusBarColor(color);

_window = null;
_element = null;

System.Diagnostics.Debug.WriteLine("OnDetached Completed");
}

public void Update()
{
System.Diagnostics.Debug.WriteLine("Update Start");
var color = AlterColor.GetAccent(_element).ToAndroid();
_window.SetStatusBarColor(color);

System.Diagnostics.Debug.WriteLine("Update Completed");
}
}
}
4 changes: 2 additions & 2 deletions AiForms.Effects.Droid/AlterLineHeightPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ protected override void OnAttached()

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

if (!IsDisposed) {
_effect.OnDetachedIfNotDisposed();
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached Disposing");
}
_effect?.OnDetached();
_effect = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}


Expand Down
12 changes: 10 additions & 2 deletions AiForms.Effects.Droid/BorderPlatformEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ protected override void OnAttached()

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

if (!IsDisposed) { // Check disposed
_view.Background = _orgDrawable;

Expand All @@ -49,6 +47,8 @@ protected override void OnDetached()
_border = null;
_view = null;
System.Diagnostics.Debug.WriteLine($"{this.GetType().FullName} Detached completely");

base.OnDetached();
}

protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
Expand All @@ -71,6 +71,9 @@ protected override void OnElementPropertyChanged(System.ComponentModel.PropertyC
UpdateColor();
UpdateBorder();
}
else if (args.PropertyName == VisualElement.BackgroundColorProperty.PropertyName) {
UpdateBackgroundColor();
}
}

void UpdateRadius()
Expand Down Expand Up @@ -109,7 +112,12 @@ void UpdateBorder()
_view.ClipToOutline = true; //not to overflow children

_view.SetBackground(_border);
}

void UpdateBackgroundColor()
{
_orgDrawable = _view.Background;
UpdateBorder();
}
}
}
Loading

0 comments on commit 27fd776

Please sign in to comment.