QuickImage – Rev 11

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Xml.Serialization;

namespace Configuration
{
    [XmlRoot(ElementName = "SupportedFormats")]
    public class SupportedFormats : INotifyPropertyChanged
    {
        private Images _images = new Images(new[] { "image/jpeg", "image/png", "image/gif", "image/bmp", "image/webp", "image/tga" });
        private Videos _videos = new Videos(new[] { "video/mp4", "video/x-m4v", "video/webm" });

        [XmlElement(ElementName = "Images")]
        public Images Images
        {
            get => _images;
            set
            {
                if (Equals(value, _images))
                {
                    return;
                }

                _images = value;

                supportedImages.Clear();
                if (value is { Image: { } } && value.Image.Count != 0)
                {
                    supportedImages.UnionWith(value.Image);
                }
                OnPropertyChanged();
            }
        }

        [XmlElement(ElementName = "Videos")]
        public Videos Videos
        {
            get => _videos;
            set
            {
                if (Equals(value, _videos))
                {
                    return;
                }

                _videos = value;

                supportedVideos.Clear();
                if(value is { Video: { }} && value.Video.Count != 0)
                {
                    supportedVideos.UnionWith(value.Video);
                }
                OnPropertyChanged();
            }
        }

        public SupportedFormats()
        {
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private readonly HashSet<string> supportedImages = new HashSet<string>(new[] { "image/jpeg", "image/png", "image/gif", "image/bmp", "image/webp", "image/tga" }, StringComparer.OrdinalIgnoreCase);
        private readonly HashSet<string> supportedVideos = new HashSet<string>(new[] { "video/mp4", "video/x-m4v", "video/webm" }, StringComparer.OrdinalIgnoreCase);

        public bool IsSupported(string mime)
        {
            if (string.IsNullOrEmpty(mime))
            {
                return false;
            }

            return supportedImages.Contains(mime) || supportedVideos.Contains(mime);
        }

        public bool IsSupportedImage(string mime)
        {
            if(string.IsNullOrEmpty(mime))
            {
                return false;
            }

            return supportedImages.Contains(mime);
        }

        public bool IsSupportedVideo(string mime)
        {
            if (string.IsNullOrEmpty(mime))
            {
                return false;
            }

            return supportedVideos.Contains(mime);
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        public void Add(Images images)
        {
            foreach (var image in images.Image)
            {
                if (!_images.Image.Contains(image))
                {
                    _images.Image.Add(image);
                }
            }

            supportedImages.UnionWith(images.Image);
        }

        public void Add(Videos videos)
        {
            foreach (var video in videos.Video)
            {
                if (!_videos.Video.Contains(video))
                {
                    _videos.Video.Add(video);
                }
            }

            supportedVideos.UnionWith(videos.Video);
        }
    }
}