<commit_before>using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using System;
using FFImageLoading.Config;
using FFImageLoading.Work;
using System.Net.Http;
using FFImageLoading.Helpers;
using FFImageLoading.Cache;
using FFImageLoading.Extensions;
using System.Threading;

namespace FFImageLoading
{
    public static class ImageService
    {
        private static volatile bool _initialized;
		private static object _initializeLock = new object();

        /// <summary>
        /// Gets FFImageLoading configuration
        /// </summary>
        /// <value>The configuration used by FFImageLoading.</value>
        public static Configuration Config { get; private set; }

        /// <summary>
        /// Initialize ImageService default values. This can only be done once: during app start.
        /// </summary>
        /// <param name="maxCacheSize">Max cache size. If zero then 20% of the memory will be used.</param>
        /// <param name="httpClient">.NET HttpClient to use. If null then a ModernHttpClient is instanciated.</param>
        /// <param name="scheduler">Work scheduler used to organize/schedule loading tasks.</param>
        /// <param name="logger">Basic logger. If null a very simple implementation that prints to console is used.</param>
        /// <param name="diskCache">Disk cache. If null a default disk cache is instanciated that uses a journal mechanism.</param>
        /// <param name="downloadCache">Download cache. If null a default download cache is instanciated, which relies on the DiskCache</param>
		/// <param name="loadWithTransparencyChannel">Gets a value indicating whether images should be loaded with transparency channel. On Android we save 50% of the memory without transparency since we use 2 bytes per pixel instead of 4.</param>
        public static void Initialize(int? maxCacheSize = null, HttpClient httpClient = null, IWorkScheduler scheduler = null, IMiniLogger logger = null,
			IDiskCache diskCache = null, IDownloadCache downloadCache = null, bool? loadWithTransparencyChannel = null)
        {
			lock (_initializeLock)
			{
				_initialized = false;

				if (Config != null)
				{
					// If DownloadCache is not updated but HttpClient is then we inform DownloadCache
					if (httpClient != null && downloadCache == null)
					{
						downloadCache = Config.DownloadCache;
						downloadCache.DownloadHttpClient = httpClient;
					}

					logger.Debug("Skip configuration for maxCacheSize and diskCache. They cannot be redefined.");
					maxCacheSize = Config.MaxCacheSize;
					diskCache = Config.DiskCache;

					// Redefine these if they were provided only
					httpClient = httpClient ?? Config.HttpClient;
					scheduler = scheduler ?? Config.Scheduler;
					logger = logger ?? Config.Logger;
					downloadCache = downloadCache ?? Config.DownloadCache;
					loadWithTransparencyChannel = loadWithTransparencyChannel ?? Config.LoadWithTransparencyChannel;
				}


				InitializeIfNeeded(maxCacheSize ?? 0, httpClient, scheduler, logger, diskCache, downloadCache, loadWithTransparencyChannel ?? false);
			}
        }

        private static void InitializeIfNeeded(int maxCacheSize = 0, HttpClient httpClient = null, IWorkScheduler scheduler = null, IMiniLogger logger = null,
			IDiskCache diskCache = null, IDownloadCache downloadCache = null, bool loadWithTransparencyChannel = false)
        {
			if (_initialized)
				return;

			lock (_initializeLock)
			{
				if (_initialized)
					return;
			
				var userDefinedConfig = new Configuration(maxCacheSize, httpClient, scheduler, logger, diskCache, downloadCache, loadWithTransparencyChannel);
				Config = GetDefaultConfiguration(userDefinedConfig);

				_initialized = true;
			}
        }

        private static Configuration GetDefaultConfiguration(Configuration userDefinedConfig)
        {
            var httpClient = userDefinedConfig.HttpClient ?? new HttpClient(new ModernHttpClient.NativeMessageHandler(true, false));

            var logger = userDefinedConfig.Logger ?? new MiniLogger();
            var scheduler = userDefinedConfig.Scheduler ?? new WorkScheduler(logger);
            var diskCache = userDefinedConfig.DiskCache ?? DiskCache.CreateCache(typeof(ImageService).Name);
            var downloadCache = userDefinedConfig.DownloadCache ?? new DownloadCache(httpClient, diskCache);

            return new Configuration(
                userDefinedConfig.MaxCacheSize,
                httpClient,
                scheduler,
                logger,
                diskCache,
                downloadCache,
				userDefinedConfig.LoadWithTransparencyChannel
            );<commit_msg>Allow globally disabling fade via ImageService.Initialize()<commit_after>using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using System;
using FFImageLoading.Config;
using FFImageLoading.Work;
using System.Net.Http;
using FFImageLoading.Helpers;
using FFImageLoading.Cache;
using FFImageLoading.Extensions;
using System.Threading;

namespace FFImageLoading
{
    public static class ImageService
    {
        private static volatile bool _initialized;
		private static object _initializeLock = new object();

        /// <summary>
        /// Gets FFImageLoading configuration
        /// </summary>
        /// <value>The configuration used by FFImageLoading.</value>
        public static Configuration Config { get; private set; }

        /// <summary>
        /// Initialize ImageService default values. This can only be done once: during app start.
        /// </summary>
        /// <param name="maxCacheSize">Max cache size. If zero then 20% of the memory will be used.</param>
        /// <param name="httpClient">.NET HttpClient to use. If null then a ModernHttpClient is instanciated.</param>
        /// <param name="scheduler">Work scheduler used to organize/schedule loading tasks.</param>
        /// <param name="logger">Basic logger. If null a very simple implementation that prints to console is used.</param>
        /// <param name="diskCache">Disk cache. If null a default disk cache is instanciated that uses a journal mechanism.</param>
        /// <param name="downloadCache">Download cache. If null a default download cache is instanciated, which relies on the DiskCache</param>
		/// <param name="loadWithTransparencyChannel">Gets a value indicating whether images should be loaded with transparency channel. On Android we save 50% of the memory without transparency since we use 2 bytes per pixel instead of 4.</param>
		/// <param name="fadeAnimationEnabled">Defines if fading should be performed while loading images.</param>
		public static void Initialize(int? maxCacheSize = null, HttpClient httpClient = null, IWorkScheduler scheduler = null, IMiniLogger logger = null,
			IDiskCache diskCache = null, IDownloadCache downloadCache = null, bool? loadWithTransparencyChannel = null, bool? fadeAnimationEnabled = null)
        {
			lock (_initializeLock)
			{
				_initialized = false;

				if (Config != null)
				{
					// If DownloadCache is not updated but HttpClient is then we inform DownloadCache
					if (httpClient != null && downloadCache == null)
					{
						downloadCache = Config.DownloadCache;
						downloadCache.DownloadHttpClient = httpClient;
					}

					logger.Debug("Skip configuration for maxCacheSize and diskCache. They cannot be redefined.");
					maxCacheSize = Config.MaxCacheSize;
					diskCache = Config.DiskCache;

					// Redefine these if they were provided only
					httpClient = httpClient ?? Config.HttpClient;
					scheduler = scheduler ?? Config.Scheduler;
					logger = logger ?? Config.Logger;
					downloadCache = downloadCache ?? Config.DownloadCache;
					loadWithTransparencyChannel = loadWithTransparencyChannel ?? Config.LoadWithTransparencyChannel;
					fadeAnimationEnabled = fadeAnimationEnabled ?? Config.FadeAnimationEnabled;
				}


				InitializeIfNeeded(maxCacheSize ?? 0, httpClient, scheduler, logger, diskCache, downloadCache, loadWithTransparencyChannel ?? false, fadeAnimationEnabled ?? true);
			}
        }

        private static void InitializeIfNeeded(int maxCacheSize = 0, HttpClient httpClient = null, IWorkScheduler scheduler = null, IMiniLogger logger = null,
			IDiskCache diskCache = null, IDownloadCache downloadCache = null, bool loadWithTransparencyChannel = false, bool fadeAnimationEnabled = true)
        {
			if (_initialized)
				return;

			lock (_initializeLock)
			{
				if (_initialized)
					return;
			
				var userDefinedConfig = new Configuration(maxCacheSize, httpClient, scheduler, logger, diskCache, downloadCache, loadWithTransparencyChannel, fadeAnimationEnabled);
				Config = GetDefaultConfiguration(userDefinedConfig);

				_initialized = true;
			}
        }

        private static Configuration GetDefaultConfiguration(Configuration userDefinedConfig)
        {
            var httpClient = userDefinedConfig.HttpClient ?? new HttpClient(new ModernHttpClient.NativeMessageHandler(true, false));

            var logger = userDefinedConfig.Logger ?? new MiniLogger();
            var scheduler = userDefinedConfig.Scheduler ?? new WorkScheduler(logger);
            var diskCache = userDefinedConfig.DiskCache ?? DiskCache.CreateCache(typeof(ImageService).Name);
            var downloadCache = userDefinedConfig.DownloadCache ?? new DownloadCache(httpClient, diskCache);

            return new Configuration(
                userDefinedConfig.MaxCacheSize,
                httpClient,
                scheduler,
                logger,
                diskCache,
                downloadCache,
				userDefinedConfig.LoadWithTransparencyChannel,
				userDefinedConfig.FadeAnimationEnabled
            );
{'fraction_non_alphanumeric': 0.08039114770972723, 'fraction_numerical': 0.0016469377251672672, 'mean_word_length': 3.3029229406554474, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5698832', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2336, 'n_words': 766}