using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Android.App; using Android.Content; using Android.Graphics; using Android.Graphics.Drawables; using Android.OS; using Android.Runtime; using Android.Text; using Android.Text.Method; using Android.Views; using Android.Widget; using TcuMobile.Controls; using TcuMobile.Droid.Renderers.Material; using Xamarin.Forms; using Xamarin.Forms.Material.Android; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(MaterialEntry), typeof(ExtendedMaterialEntryRenderer), new[] { typeof(VisualMarker.MaterialVisual) })] namespace TcuMobile.Droid.Renderers.Material { public class ExtendedMaterialEntryRenderer : MaterialEntryRenderer { public OnTrailingDrawableTouchListener TrailingDrawableTouchListener { get; private set; } public new MaterialEntry Element => (MaterialEntry)base.Element; public ExtendedMaterialEntryRenderer(Context context) : base(context) { } protected override async void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); if (e.OldElement != null || e.NewElement == null) return; Control.SetCounter(Element); Control.SetHelperText(Element); Control.SetBackground(Element); await Control.SetDrawable(Element); Control.SetError(Element); this.Control.EditText.SetSelectAllOnFocus(this.Element.SelectAllOnFocus); this.SetIsDecimal(); this.SetIsFocusable(); this.SetIsNumeric(); } protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (e.PropertyName == nameof(MaterialEntry.LeadingImageSource) || e.PropertyName == nameof(MaterialEntry.TrailingImageSource)) await Control.SetDrawable(Element); if (e.PropertyName == nameof(Element.IsErrorEnabled) || e.PropertyName == nameof(Element.ErrorText)) Control.SetError(Element); if (e.PropertyName == nameof(Element.IsHelperTextEnabled) || e.PropertyName == nameof(Element.HelperText)) Control.SetHelperText(Element); if (e.PropertyName == nameof(Element.IsCounterEnabled) || e.PropertyName == nameof(Element.CounterMaxLength)) Control.SetCounter(Element); if (e.PropertyName == nameof(Element.CornerRadius) || e.PropertyName == nameof(Element.BackgroundColor) || e.PropertyName == nameof(Element.BorderWidth) || e.PropertyName == nameof(Element.BorderColor)) Control.SetBackground(Element); if (e.PropertyName == nameof(Element.SelectAllOnFocus)) this.Control.EditText.SetSelectAllOnFocus(this.Element.SelectAllOnFocus); if (e.PropertyName == nameof(Element.Focusable)) this.SetIsFocusable(); if (e.PropertyName == nameof(Element.IsDecimal)) this.SetIsDecimal(); if (e.PropertyName == nameof(Element.IsNumeric)) this.SetIsNumeric(); } private void SetIsDecimal() { if (this.Control == null) return; if (this.Element.IsDecimal) { if (Build.VERSION.SdkInt >= BuildVersionCodes.O) this.Control.EditText.KeyListener = DigitsKeyListener.GetInstance(null, true, true); else this.Control.EditText.KeyListener = DigitsKeyListener.GetInstance(true, true); this.Control.EditText.InputType = InputTypes.ClassNumber | InputTypes.NumberFlagDecimal; } } private void SetIsNumeric() { if (this.Control == null) return; if (this.Element.IsNumeric) { InputTypes its = InputTypes.ClassNumber | InputTypes.NumberVariationNormal; Control.EditText.SetRawInputType(its); } } private void SetIsFocusable() { if (this.Control == null) return; this.Control.EditText.Focusable = this.Element.Focusable; if (this.Element.Focusable) { this.Control.Click -= this.OnClick; this.Control.EditText.Click -= OnClick; } else { //todo: вернуть тип ввода назад после того, как присвоим IsFocusable=true this.Control.EditText.InputType = InputTypes.Null; this.Control.Click += this.OnClick; this.Control.EditText.Click += OnClick; } } private void OnClick(object sender, EventArgs e) { this.Element.InvokeTapped(); } } public class OnTrailingDrawableTouchListener : Java.Lang.Object, Android.Views.View.IOnTouchListener { public event EventHandler RightDrawableClicked; public Action OnTapped { get; } public OnTrailingDrawableTouchListener() { } public OnTrailingDrawableTouchListener(Action onTapped) { OnTapped = onTapped; } public bool OnTouch(Android.Views.View view, MotionEvent e) { if (view is EditText && e.Action == MotionEventActions.Up) { EditText editText = (EditText)view; if (editText.GetCompoundDrawables()[2] != null) { if (e.RawX >= (editText.Right - editText.GetCompoundDrawables()[2].Bounds.Width())) { this.RightDrawableClicked?.Invoke(this, new EventArgs()); OnTapped(); return true; } } } return false; } } }