using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Android.App; using Android.Content; using Android.Glide; using Android.Graphics; using Android.Graphics.Drawables; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Bumptech.Glide; using Google.Android.Material.TextField; using TcuMobile.Controls; using TcuMobile.Controls.Svg; using TcuMobile.Droid.Renderers; using TcuMobile.Droid.Renderers.Material; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; using static Bumptech.Glide.Glide; namespace TcuMobile.Droid { public static class Extensions { public static IImageSourceHandler GetHandler(this ImageSource imageSource) { if (imageSource is SvgImageSource) return new SvgImageSourceHandler(); if (imageSource is FontImageSource) return new FontImageSourceHandler(); if (imageSource is UriImageSource) return new ImageLoaderSourceHandler(); if (imageSource is FileImageSource) return new FileImageSourceHandler(); if (imageSource is StreamImageSource) return new StreamImagesourceHandler(); if (imageSource is BytesImageSource) return new BytesImageSourceHandler(); return null; } public static async Task GetBitmap(this ImageSource imageSource, Context context, CancellationToken cancellationToken = default) { if (imageSource == null) return null; IImageSourceHandler handler = imageSource.GetHandler(); Bitmap bitmap = await handler?.LoadImageAsync(imageSource, context, cancellationToken); return bitmap; } public static Task HandleStreamImageSource(RequestManager request, BytesImageSource source, CancellationToken token) { if (token.IsCancellationRequested) return Task.FromResult(null); return Task.FromResult(request.AsBitmap().Load(source.Buffer)); } public static async Task LoadViaGlide(this BytesImageSource source, Context context, CancellationToken token) { try { if (source is null) return null; RequestManager request = With(context); RequestBuilder builder = null; builder = await HandleStreamImageSource(request, source, token); if (builder is null) return null; Bumptech.Glide.Request.IFutureTarget future = builder.Submit(); Java.Lang.Object result = await Task.Run(() => future.Get(), token); if (result is Bitmap drawable) return drawable; } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } return null; } public static void SetHelperText(this TextInputLayout control, IMaterialInputHelper element) { control.HelperTextEnabled = element.IsHelperTextEnabled; control.HelperText = element.HelperText; } public static void SetError(this TextInputLayout control, IMaterialInputError element) { control.ErrorEnabled = element.IsErrorEnabled; if (control.ErrorEnabled) control.Error = element.ErrorText; } public static void SetCounter(this TextInputLayout control, IMaterialInputCounter element) { control.CounterEnabled = element.IsCounterEnabled; control.CounterMaxLength = element.CounterMaxLength; } public static void SetBackground(this Android.Views.View view, IBackground element) { GradientDrawable gd = new GradientDrawable(); gd.SetStroke(element.BorderWidth, element.BorderColor.ToAndroid()); gd.SetColor(element.BackgroundColor.ToAndroid()); gd.SetCornerRadius(element.CornerRadius); view.SetBackground(gd); } public static async Task SetDrawable(this TextInputLayout control, IMaterialInput element) { Drawable leadingDrawable = await control.CreateLeadingDrawable(element); Drawable trailingDrawable = await control.CreateTrailingDrawable(element); control.EditText.SetCompoundDrawablesWithIntrinsicBounds(leadingDrawable, null, trailingDrawable, null); control.EditText.CompoundDrawablePadding = 35; } private static async Task CreateLeadingDrawable(this TextInputLayout control, IMaterialInput element) => new BitmapDrawable(control.Context.Resources, await element.LeadingImageSource.GetBitmap(control.Context, new CancellationToken())); private static async Task CreateTrailingDrawable(this TextInputLayout control, IMaterialInput element) { Drawable rightDrawable = null; if (element.IsErrorEnabled) return new BitmapDrawable(control.Context.Resources, await element.ErrorImageSource.GetBitmap(control.Context, new CancellationToken())); if (element.TrailingImageSource != null) { rightDrawable = new BitmapDrawable(control.Context.Resources, await element.TrailingImageSource.GetBitmap(control.Context, new CancellationToken())); control.SetTrailingDrawableTouchListener(element); } return rightDrawable; } public static void SetTrailingDrawableTouchListener(this TextInputLayout control, IMaterialInput element) { OnTrailingDrawableTouchListener trailingDrawableTouchListener = new OnTrailingDrawableTouchListener(() => element.RaiseTrailingImageTapped()); control.EditText.SetOnTouchListener(trailingDrawableTouchListener); } } }