2019-01-08 03:07:50 +03:00
|
|
|
#if __ANDROID_28__
|
2018-11-23 20:40:15 +03:00
|
|
|
using System;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using Android.Content;
|
2019-01-08 03:07:50 +03:00
|
|
|
using Android.Support.V4.View;
|
|
|
|
using Android.Views;
|
2018-11-23 20:40:15 +03:00
|
|
|
using Xamarin.Forms;
|
2019-01-08 03:07:50 +03:00
|
|
|
using Xamarin.Forms.Platform.Android.FastRenderers;
|
2019-02-28 02:48:45 +03:00
|
|
|
using Xamarin.Forms.Material.Android;
|
2018-11-23 20:40:15 +03:00
|
|
|
using AProgressBar = Android.Widget.ProgressBar;
|
2019-01-08 03:07:50 +03:00
|
|
|
using AView = Android.Views.View;
|
2019-02-28 02:48:45 +03:00
|
|
|
using Xamarin.Forms.Platform.Android;
|
2018-11-23 20:40:15 +03:00
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
|
2019-02-28 02:48:45 +03:00
|
|
|
namespace Xamarin.Forms.Material.Android
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
public class MaterialProgressBarRenderer : AProgressBar,
|
|
|
|
IVisualElementRenderer, IViewRenderer, ITabStop
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
const int MaximumValue = 10000;
|
|
|
|
int? _defaultLabelFor;
|
|
|
|
bool _disposed;
|
|
|
|
ProgressBar _element;
|
|
|
|
VisualElementTracker _visualElementTracker;
|
|
|
|
VisualElementRenderer _visualElementRenderer;
|
|
|
|
MotionEventHelper _motionEventHelper;
|
2019-02-28 02:48:45 +03:00
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
public MaterialProgressBarRenderer(Context context)
|
2019-03-20 20:11:16 +03:00
|
|
|
: base(MaterialContextThemeWrapper.Create(context), null, Resource.Attribute.materialProgressBarHorizontalStyle)
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
Indeterminate = false;
|
|
|
|
Max = MaximumValue;
|
|
|
|
|
|
|
|
_visualElementRenderer = new VisualElementRenderer(this);
|
|
|
|
_motionEventHelper = new MotionEventHelper();
|
2018-11-23 20:40:15 +03:00
|
|
|
}
|
|
|
|
|
2019-02-28 02:48:45 +03:00
|
|
|
public override bool OnTouchEvent(MotionEvent e)
|
|
|
|
{
|
|
|
|
if (_visualElementRenderer.OnTouchEvent(e) || base.OnTouchEvent(e))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return _motionEventHelper.HandleMotionEvent(Parent, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
public event EventHandler<VisualElementChangedEventArgs> ElementChanged;
|
|
|
|
public event EventHandler<PropertyChangedEventArgs> ElementPropertyChanged;
|
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
protected AProgressBar Control => this;
|
|
|
|
|
|
|
|
protected ProgressBar Element
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
get { return _element; }
|
|
|
|
set
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
if (_element == value)
|
|
|
|
return;
|
2018-11-23 20:40:15 +03:00
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
var oldElement = _element;
|
|
|
|
_element = value;
|
|
|
|
|
|
|
|
OnElementChanged(new ElementChangedEventArgs<ProgressBar>(oldElement, _element));
|
2018-11-23 20:40:15 +03:00
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
_element?.SendViewInitialized(this);
|
2018-11-23 20:40:15 +03:00
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
_motionEventHelper.UpdateElement(_element);
|
|
|
|
}
|
2018-11-23 20:40:15 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
protected override void Dispose(bool disposing)
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
if (_disposed)
|
2018-11-23 20:40:15 +03:00
|
|
|
return;
|
2019-01-08 03:07:50 +03:00
|
|
|
_disposed = true;
|
|
|
|
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
_visualElementTracker?.Dispose();
|
|
|
|
_visualElementTracker = null;
|
|
|
|
|
|
|
|
_visualElementRenderer?.Dispose();
|
|
|
|
_visualElementRenderer = null;
|
|
|
|
|
|
|
|
if (Element != null)
|
|
|
|
{
|
|
|
|
Element.PropertyChanged -= OnElementPropertyChanged;
|
2018-11-23 20:40:15 +03:00
|
|
|
|
2019-02-28 02:48:45 +03:00
|
|
|
if (Platform.Android.Platform.GetRenderer(Element) == this)
|
|
|
|
Element.ClearValue(Platform.Android.Platform.RendererProperty);
|
2019-01-08 03:07:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
2018-11-23 20:40:15 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
protected virtual void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
ElementChanged?.Invoke(this, new VisualElementChangedEventArgs(e.OldElement, e.NewElement));
|
|
|
|
|
|
|
|
if (e.OldElement != null)
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
e.OldElement.PropertyChanged -= OnElementPropertyChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.NewElement != null)
|
|
|
|
{
|
|
|
|
this.EnsureId();
|
|
|
|
|
|
|
|
if (_visualElementTracker == null)
|
|
|
|
_visualElementTracker = new VisualElementTracker(this);
|
|
|
|
|
|
|
|
e.NewElement.PropertyChanged += OnElementPropertyChanged;
|
|
|
|
|
|
|
|
UpdateProgress();
|
|
|
|
UpdateColors();
|
|
|
|
|
|
|
|
ElevationHelper.SetElevation(this, e.NewElement);
|
2018-11-23 20:40:15 +03:00
|
|
|
}
|
2019-01-08 03:07:50 +03:00
|
|
|
}
|
2018-11-23 20:40:15 +03:00
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
protected virtual void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
{
|
|
|
|
ElementPropertyChanged?.Invoke(this, e);
|
|
|
|
if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName)
|
|
|
|
UpdateProgress();
|
|
|
|
else if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName || e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
|
|
|
|
UpdateColors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateColors()
|
|
|
|
{
|
2018-11-23 20:40:15 +03:00
|
|
|
if (Element == null || Control == null)
|
|
|
|
return;
|
|
|
|
|
2019-02-14 02:36:55 +03:00
|
|
|
this.ApplyProgressBarColors(Element.ProgressColor, Element.BackgroundColor);
|
2019-01-08 03:07:50 +03:00
|
|
|
}
|
|
|
|
|
2019-02-28 02:48:45 +03:00
|
|
|
void UpdateProgress() => Control.Progress = (int)(Element.Progress * MaximumValue);
|
2019-01-08 03:07:50 +03:00
|
|
|
|
|
|
|
// IVisualElementRenderer
|
|
|
|
VisualElement IVisualElementRenderer.Element => Element;
|
|
|
|
VisualElementTracker IVisualElementRenderer.Tracker => _visualElementTracker;
|
|
|
|
ViewGroup IVisualElementRenderer.ViewGroup => null;
|
|
|
|
AView IVisualElementRenderer.View => this;
|
2019-02-28 02:48:45 +03:00
|
|
|
void IVisualElementRenderer.SetElement(VisualElement element) =>
|
|
|
|
Element = (element as ProgressBar) ?? throw new ArgumentException("Element must be of type ProgressBar.");
|
|
|
|
void IVisualElementRenderer.UpdateLayout() =>
|
|
|
|
_visualElementTracker?.UpdateLayout();
|
2019-01-08 03:07:50 +03:00
|
|
|
|
|
|
|
SizeRequest IVisualElementRenderer.GetDesiredSize(int widthConstraint, int heightConstraint)
|
|
|
|
{
|
|
|
|
Measure(widthConstraint, heightConstraint);
|
2019-02-14 02:36:55 +03:00
|
|
|
return new SizeRequest(new Size(Control.MeasuredWidth, Context.ToPixels(4)), new Size(Context.ToPixels(4), Context.ToPixels(4)));
|
2018-11-23 20:40:15 +03:00
|
|
|
}
|
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
void IVisualElementRenderer.SetLabelFor(int? id)
|
2018-11-23 20:40:15 +03:00
|
|
|
{
|
2019-01-08 03:07:50 +03:00
|
|
|
if (_defaultLabelFor == null)
|
|
|
|
_defaultLabelFor = ViewCompat.GetLabelFor(this);
|
2018-11-23 20:40:15 +03:00
|
|
|
|
2019-01-08 03:07:50 +03:00
|
|
|
ViewCompat.SetLabelFor(this, (int)(id ?? _defaultLabelFor));
|
2018-11-23 20:40:15 +03:00
|
|
|
}
|
2019-01-08 03:07:50 +03:00
|
|
|
|
|
|
|
// IViewRenderer
|
|
|
|
void IViewRenderer.MeasureExactly() =>
|
|
|
|
ViewRenderer.MeasureExactly(this, Element, Context);
|
|
|
|
|
|
|
|
// ITabStop
|
|
|
|
AView ITabStop.TabStop => this;
|
2018-11-23 20:40:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|