Ejemplo n.º 1
0
        /// <summary>
        /// Gets the cached bitmap image representing the card object. Will query the API if it has not been loaded, otherwise gets the cached version.
        /// </summary>
        public static async Task <Bitmap> GetImageAsync(string imageUri, IProgress <string> reporter)
        {
            if (string.IsNullOrWhiteSpace(imageUri))
            {
                throw new ArgumentNullException(nameof(imageUri), @"Image request URI cannot be null or empty/whitespace. Consumer must check before using this method.");
            }

            while (IsCacheBeingAccessed)
            {
                await Task.Delay(1);
            }

            IsCacheBeingAccessed = true;

            if (ImageCache.ContainsKey(imageUri))
            {
                await Task.Delay(1);

                IsCacheBeingAccessed = false;
                return(ImageCache[imageUri]);
            }

            Bitmap bitmap = await ScryfallService.GetImageAsync(imageUri, reporter);

            ImageCache.Add(imageUri, bitmap);

            if (ImageCache.Count > 100)
            {
                ImageCache.Remove(ImageCache.First().Key);
            }

            IsCacheBeingAccessed = false;
            return(bitmap);
        }