/// <summary> /// Creates an <see cref="IAvatarProvider"/> based on the given /// <see cref="AvatarProvider"/> and <see cref="AvatarFallbackType"/> enum options. /// </summary> /// <returns>Returns an avatar provider that can be used to resolve user avatars.</returns> public static IAvatarProvider CreateAvatarProvider( AvatarProvider provider, AvatarFallbackType?fallbackType, IAvatarDownloader downloader = null) { // initialize download only if needed (some options, like local providers, don't need a downloader) // and use the downloader provided as parameter if possible. var lazyDownloader = new Lazy <IAvatarDownloader>(() => downloader ?? new AvatarDownloader()); // build collection of (non-null) providers var providers = new[] { BuildMainProvider(), BuildFallbackProvider() } .Where(p => p != null) .ToArray(); // only create chained avatar overhead if really needed return(providers.Length switch { 0 => null, 1 => providers[0], _ => new ChainedAvatarProvider(providers), });
public GravatarProvider( IAvatarDownloader downloader, AvatarFallbackType?fallback, bool forceFallback = false) { _downloader = downloader ?? throw new ArgumentNullException(nameof(downloader)); _fallback = fallback; _forceFallback = forceFallback; }
/// <summary> /// Parses a single template segment. /// </summary> private static IAvatarProvider?FromTemplateSegment(IAvatarDownloader downloader, string providerTemplate) { // if the segment is a tag like "<Demo>", we extract the name // and try to parse it as an AvatarProvider enum. if (providerTemplate.StartsWith("<") && providerTemplate.EndsWith(">")) { var providerName = providerTemplate.Substring(1, providerTemplate.Length - 2); if (Enum.TryParse <AvatarProvider>(providerName, true, out var provider) && provider != AvatarProvider.Custom) { return(AvatarService.CreateAvatarProvider(provider, null, downloader)); } return(null); } // in all other cases assume it's an UriTemplate return(new UriTemplateResolver(providerTemplate)); }
/// <summary> /// Parses a custom avatar template string and creates an <see cref="IAvatarProvider"/> from it. /// </summary> /// <param name="customProviderTemplates">The custom avatar provider template.</param> /// <param name="downloader">The downloader that is used to download avatar images.</param> /// <returns>Returns the <see cref="IAvatarProvider"/> described by the template.</returns> public static IAvatarProvider ParseTemplateString(string customProviderTemplates, IAvatarDownloader downloader) { if (downloader is null) { throw new ArgumentNullException(nameof(downloader)); } var providerParts = customProviderTemplates .Split(';') .Select(p => p.Trim()) .Select(p => FromTemplateSegment(downloader, p)) .WhereNotNull() .ToArray(); // We can't use the chain provider here, because some returned providers (namely UriTemplateResolvers) // don't actually fulfill the interface contract of IAvatarProvider. UriTemplateResolver is a special // case that exists to prevent variables like hashes from being evaluated/calculated multiple times. return(new CustomAvatarProvider(downloader, providerParts)); }
private CustomAvatarProvider(IAvatarDownloader downloader, IAvatarProvider[] subProvider) { _downloader = downloader ?? throw new ArgumentNullException(nameof(downloader)); _subProvider = subProvider ?? throw new ArgumentNullException(nameof(subProvider)); }
public GithubAvatarProvider([NotNull] IAvatarDownloader downloader, bool onlySupplyNoReply = false) { _downloader = downloader ?? throw new ArgumentNullException(nameof(downloader)); _onlySupplyNoReply = onlySupplyNoReply; }