<commit_before>﻿using System;
using System.Collections.Generic;
using FFImageLoading.Forms;
using Xamarin.Forms;

namespace FFImageLoading.Svg.Forms
{
#if __IOS__
            [Foundation.Preserve(AllMembers = true)]
#elif __ANDROID__
            [Android.Runtime.Preserve(AllMembers = true)]
#endif
    [Preserve(AllMembers = true)]
	public class SvgCachedImage : CachedImage
    {
        static FFImageLoading.Forms.ImageSourceConverter _imageSourceConverter = new FFImageLoading.Forms.ImageSourceConverter();

        public SvgCachedImage() : base()
        {
            ReplaceStringMap = new Dictionary<string, string>();
        }

        /// <summary>
        /// The source property.
        /// </summary>
        public static new readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(Source), typeof(ImageSource), typeof(SvgCachedImage), default(ImageSource), BindingMode.OneWay, propertyChanging: OnSourcePropertyChanging);

        static void OnSourcePropertyChanging(BindableObject bindable, object oldValue, object newValue)
        {
            var element = (CachedImage)bindable;

            // HACK for the strange issue when TypeConverter is not respected (somehow FileImageSource is returned !?!?!?!?)
			var fileSource = newValue as FileImageSource;
            if (fileSource?.File != null && fileSource.File.StartsWith("<", StringComparison.OrdinalIgnoreCase))
            {
                element.Source = SvgImageSource.FromSvgString(fileSource.File, replaceStringMap: ((SvgCachedImage)element).ReplaceStringMap);
                return;
            }

            var uriSource = newValue as UriImageSource;
            if (uriSource?.Uri?.OriginalString != null && uriSource.Uri.OriginalString.IsSvgDataUrl())
			{
				element.Source = SvgImageSource.FromSvgString(uriSource.Uri.OriginalString, replaceStringMap: ((SvgCachedImage)element).ReplaceStringMap);
				return;
			}

            element.Source = newValue as ImageSource;
        }

        /// <summary>
        /// Gets or sets the source.
        /// </summary>
        /// <value>The source.</value>
        [TypeConverter(typeof(SvgImageSourceConverter))]
        public new ImageSource Source
        {
        	get
        	{
        		return (ImageSource)GetValue(SourceProperty);
        	}
        	set
        	{
        		SetValue(SourceProperty, value);
        	}
        }

        /// <summary>
        /// The loading placeholder property.
        /// </summary>
        public static new readonly BindableProperty LoadingPlaceholderProperty = BindableProperty.Create(nameof(LoadingPlaceholder), typeof(ImageSource), typeof(SvgCachedImage), default(ImageSource), propertyChanging: OnLoadingPlaceholderPropertyChanging);

        static void OnLoadingPlaceholderPropertyChanging(BindableObject bindable, object oldValue, object newValue)
        {
        	var element = (CachedImage)bindable;
            element.LoadingPlaceholder = newValue as ImageSource;
        }

        /// <summary>
        /// Gets or sets the loading placeholder image.
        /// </summary>
        [TypeConverter(typeof(SvgImageSourceConverter))]
        public new ImageSource LoadingPlaceholder
        {
        	get
        	{
        		return (ImageSource)GetValue(LoadingPlaceholderProperty);
        	}
        	set
        	{
        		SetValue(LoadingPlaceholderProperty, value);
        	}
        }

        /// <summary>
        /// The error placeholder property.
        /// </summary>
        public static new readonly BindableProperty ErrorPlaceholderProperty = BindableProperty.Create(nameof(ErrorPlaceholder), typeof(ImageSource), typeof(SvgCachedImage), default(ImageSource), propertyChanging: OnErrorPlaceholderPropertyChanging);

        static void OnErrorPlaceholderPropertyChanging(BindableObject bindable, object oldValue, object newValue)
        {
        	var element = (CachedImage)bindable;
        	element.ErrorPlaceholder = newValue as ImageSource;
        }

        /// <summary>
        /// Gets or sets the error placeholder image.
        /// </summary>
        [TypeConverter(typeof(SvgImageSourceConverter))]
        public new ImageSource ErrorPlaceholder
        {
        	get
        	{
        		return (ImageSource)GetValue(ErrorPlaceholderProperty);
        	}
        	set
        	{
        		SetValue(ErrorPlaceholderProperty, value);
        	}
        }

        /// <summary>
        /// The error placeholder property.
        /// </summary>
        public static readonly BindableProperty ReplaceStringMapProperty = BindableProperty.Create(nameof(ReplaceStringMap), typeof(Dictionary<string, string>), typeof(SvgCachedImage), default(Dictionary<string, string>));

        /// <summary>
        /// Used to define replacement map which will be used to
        /// replace text inside SVG file (eg. changing colors values)
        /// </summary>
        /// <value>The replace string map.</value>
        public Dictionary<string, string> ReplaceStringMap
        {
            get
            {
                return (Dictionary<string, string>)GetValue(ReplaceStringMapProperty);
            }
            set
            {

                SetValue(ReplaceStringMapProperty, value);
            }
        }

        /// <summary>
        /// Setups the on before image loading.
        /// You can add additional logic here to configure image loader settings before loading
        /// </summary>
        /// <param name="imageLoader">Image loader.</param>
        protected override void SetupOnBeforeImageLoading(Work.TaskParameter imageLoader)
        {<commit_msg>SvgCachedImage - setting ReplaceStringMap auto reloads svg file<commit_after>﻿using System;
using System.Collections.Generic;
using FFImageLoading.Forms;
using Xamarin.Forms;

namespace FFImageLoading.Svg.Forms
{
#if __IOS__
            [Foundation.Preserve(AllMembers = true)]
#elif __ANDROID__
            [Android.Runtime.Preserve(AllMembers = true)]
#endif
    [Preserve(AllMembers = true)]
	public class SvgCachedImage : CachedImage
    {
        static FFImageLoading.Forms.ImageSourceConverter _imageSourceConverter = new FFImageLoading.Forms.ImageSourceConverter();

        public SvgCachedImage() : base()
        {
            ReplaceStringMap = new Dictionary<string, string>();
        }

        /// <summary>
        /// The source property.
        /// </summary>
        public static new readonly BindableProperty SourceProperty = BindableProperty.Create(nameof(Source), typeof(ImageSource), typeof(SvgCachedImage), default(ImageSource), BindingMode.OneWay, propertyChanging: OnSourcePropertyChanging);

        static void OnSourcePropertyChanging(BindableObject bindable, object oldValue, object newValue)
        {
            var element = (CachedImage)bindable;

            // HACK for the strange issue when TypeConverter is not respected (somehow FileImageSource is returned !?!?!?!?)
			var fileSource = newValue as FileImageSource;
            if (fileSource?.File != null && fileSource.File.StartsWith("<", StringComparison.OrdinalIgnoreCase))
            {
                element.Source = SvgImageSource.FromSvgString(fileSource.File, replaceStringMap: ((SvgCachedImage)element).ReplaceStringMap);
                return;
            }

            var uriSource = newValue as UriImageSource;
            if (uriSource?.Uri?.OriginalString != null && uriSource.Uri.OriginalString.IsSvgDataUrl())
			{
				element.Source = SvgImageSource.FromSvgString(uriSource.Uri.OriginalString, replaceStringMap: ((SvgCachedImage)element).ReplaceStringMap);
				return;
			}

            element.Source = newValue as ImageSource;
        }

        /// <summary>
        /// Gets or sets the source.
        /// </summary>
        /// <value>The source.</value>
        [TypeConverter(typeof(SvgImageSourceConverter))]
        public new ImageSource Source
        {
        	get
        	{
        		return (ImageSource)GetValue(SourceProperty);
        	}
        	set
        	{
        		SetValue(SourceProperty, value);
        	}
        }

        /// <summary>
        /// The loading placeholder property.
        /// </summary>
        public static new readonly BindableProperty LoadingPlaceholderProperty = BindableProperty.Create(nameof(LoadingPlaceholder), typeof(ImageSource), typeof(SvgCachedImage), default(ImageSource), propertyChanging: OnLoadingPlaceholderPropertyChanging);

        static void OnLoadingPlaceholderPropertyChanging(BindableObject bindable, object oldValue, object newValue)
        {
        	var element = (CachedImage)bindable;
            element.LoadingPlaceholder = newValue as ImageSource;
        }

        /// <summary>
        /// Gets or sets the loading placeholder image.
        /// </summary>
        [TypeConverter(typeof(SvgImageSourceConverter))]
        public new ImageSource LoadingPlaceholder
        {
        	get
        	{
        		return (ImageSource)GetValue(LoadingPlaceholderProperty);
        	}
        	set
        	{
        		SetValue(LoadingPlaceholderProperty, value);
        	}
        }

        /// <summary>
        /// The error placeholder property.
        /// </summary>
        public static new readonly BindableProperty ErrorPlaceholderProperty = BindableProperty.Create(nameof(ErrorPlaceholder), typeof(ImageSource), typeof(SvgCachedImage), default(ImageSource), propertyChanging: OnErrorPlaceholderPropertyChanging);

        static void OnErrorPlaceholderPropertyChanging(BindableObject bindable, object oldValue, object newValue)
        {
        	var element = (CachedImage)bindable;
        	element.ErrorPlaceholder = newValue as ImageSource;
        }

        /// <summary>
        /// Gets or sets the error placeholder image.
        /// </summary>
        [TypeConverter(typeof(SvgImageSourceConverter))]
        public new ImageSource ErrorPlaceholder
        {
        	get
        	{
        		return (ImageSource)GetValue(ErrorPlaceholderProperty);
        	}
        	set
        	{
        		SetValue(ErrorPlaceholderProperty, value);
        	}
        }

        /// <summary>
        /// The error placeholder property.
        /// </summary>
        public static readonly BindableProperty ReplaceStringMapProperty = BindableProperty.Create(nameof(ReplaceStringMap), typeof(Dictionary<string, string>), typeof(SvgCachedImage), default(Dictionary<string, string>), propertyChanged: new BindableProperty.BindingPropertyChangedDelegate(HandleReplaceStringMapPropertyChangedDelegate));

        /// <summary>
        /// Used to define replacement map which will be used to
        /// replace text inside SVG file (eg. changing colors values)
        /// </summary>
        /// <value>The replace string map.</value>
        public Dictionary<string, string> ReplaceStringMap
        {
            get
            {
                return (Dictionary<string, string>)GetValue(ReplaceStringMapProperty);
            }
            set
            {

                SetValue(ReplaceStringMapProperty, value);
            }
        }

        static void HandleReplaceStringMapPropertyChangedDelegate(BindableObject bindable, object oldValue, object newValue)
        {
            if (oldValue != newValue)
            {
                var cachedImage = bindable as SvgCachedImage;
                if (cachedImage != null && cachedImage.Source != null)
                {
                    cachedImage.ReloadImage();
                }
            }
        }

        /// <summary>
        /// Setups the on before image loading.
        /// You can add additional logic here to configure image loader settings before loading
        /// </summary>
        /// <param name="imageLoader">Image loader.</param>
        protected override void SetupOnBeforeImageLoading(Work.TaskParameter imageLoader)
        {
{'fraction_non_alphanumeric': 0.09072529694212787, 'fraction_numerical': 0.0, 'mean_word_length': 2.339521800281294, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7484207', 'n_tokens_mistral': 3023, 'n_tokens_neox': 2929, 'n_words': 673}