From fc1000acc11e8b45f428d5e8012d5a5804c74c9d Mon Sep 17 00:00:00 2001 From: Jake Fink Date: Tue, 12 Oct 2021 11:00:33 -0400 Subject: [PATCH] loop through URIs to find website before showing default icons (#1572) - create static class for uri logic (keeping converter for future) - create new property in viewmodel for icon source - check if icons are enabled and source for icon before showing default glyph --- .../CipherViewCell/CipherViewCell.xaml | 2 +- .../CipherViewCell/CipherViewCellViewModel.cs | 22 +++++- src/App/Utilities/IconImageConverter.cs | 74 +++++++++++-------- 3 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/App/Controls/CipherViewCell/CipherViewCell.xaml b/src/App/Controls/CipherViewCell/CipherViewCell.xaml index 905424ccb..433a8cef6 100644 --- a/src/App/Controls/CipherViewCell/CipherViewCell.xaml +++ b/src/App/Controls/CipherViewCell/CipherViewCell.xaml @@ -46,7 +46,7 @@ WidthRequest="22" HeightRequest="22" IsVisible="{Binding ShowIconImage}" - Source="{Binding Cipher, Converter={StaticResource iconImageConverter}}" + Source="{Binding IconImageSource, Mode=OneTime}" AutomationProperties.IsInAccessibleTree="False" /> diff --git a/src/App/Controls/CipherViewCell/CipherViewCellViewModel.cs b/src/App/Controls/CipherViewCell/CipherViewCellViewModel.cs index a3342b6ce..ea21df7dc 100644 --- a/src/App/Controls/CipherViewCell/CipherViewCellViewModel.cs +++ b/src/App/Controls/CipherViewCell/CipherViewCellViewModel.cs @@ -1,4 +1,5 @@ -using Bit.Core.Models.View; +using Bit.App.Utilities; +using Bit.Core.Models.View; using Bit.Core.Utilities; namespace Bit.App.Controls @@ -7,6 +8,7 @@ namespace Bit.App.Controls { private CipherView _cipher; private bool _websiteIconsEnabled; + private string _iconImageSource = string.Empty; public CipherViewCellViewModel(CipherView cipherView, bool websiteIconsEnabled) { @@ -28,8 +30,22 @@ namespace Bit.App.Controls public bool ShowIconImage { - get => WebsiteIconsEnabled && !string.IsNullOrWhiteSpace(Cipher.Login?.Uri) && - Cipher.Login.Uri.StartsWith("http"); + get => WebsiteIconsEnabled + && !string.IsNullOrWhiteSpace(Cipher.Login?.Uri) + && IconImageSource != null; + } + + public string IconImageSource + { + get + { + if (_iconImageSource == string.Empty) // default value since icon source can return null + { + _iconImageSource = IconImageHelper.GetLoginIconImage(Cipher); + } + return _iconImageSource; + } + } } } diff --git a/src/App/Utilities/IconImageConverter.cs b/src/App/Utilities/IconImageConverter.cs index c5020fb29..e81ec7959 100644 --- a/src/App/Utilities/IconImageConverter.cs +++ b/src/App/Utilities/IconImageConverter.cs @@ -10,8 +10,6 @@ namespace Bit.App.Utilities { public class IconImageConverter : IValueConverter { - private readonly IEnvironmentService _environmentService = ServiceContainer.Resolve("environmentService"); - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var cipher = value as CipherView; @@ -29,51 +27,65 @@ namespace Bit.App.Utilities switch (cipher.Type) { case CipherType.Login: - icon = GetLoginIconImage(cipher); + icon = IconImageHelper.GetLoginIconImage(cipher); break; default: break; } return icon; } + } - string GetLoginIconImage(CipherView cipher) + public static class IconImageHelper + { + public static string GetLoginIconImage(CipherView cipher) { string image = null; - if (cipher.Login.Uri != null) + if (cipher.Login.HasUris) { - var hostnameUri = cipher.Login.Uri; - var isWebsite = false; - - if (!hostnameUri.Contains("://") && hostnameUri.Contains(".")) + foreach (var uri in cipher.Login.Uris) { - hostnameUri = string.Concat("http://", hostnameUri); - isWebsite = true; - } - else - { - isWebsite = hostnameUri.StartsWith("http") && hostnameUri.Contains("."); - } - - if (isWebsite) - { - var hostname = CoreHelpers.GetHostname(hostnameUri); - var iconsUrl = _environmentService.IconsUrl; - if (string.IsNullOrWhiteSpace(iconsUrl)) + var hostnameUri = uri.Uri; + var isWebsite = false; + if (!hostnameUri.Contains(".")) { - if (!string.IsNullOrWhiteSpace(_environmentService.BaseUrl)) - { - iconsUrl = string.Format("{0}/icons", _environmentService.BaseUrl); - } - else - { - iconsUrl = "https://icons.bitwarden.net"; - } + continue; + } + if (!hostnameUri.Contains("://")) + { + hostnameUri = string.Concat("http://", hostnameUri); + } + isWebsite = hostnameUri.StartsWith("http"); + + if (isWebsite) + { + image = GetIconUrl(hostnameUri); + break; } - image = string.Format("{0}/{1}/icon.png", iconsUrl, hostname); } } return image; } + + private static string GetIconUrl(string hostnameUri) + { + IEnvironmentService _environmentService = ServiceContainer.Resolve("environmentService"); + + var hostname = CoreHelpers.GetHostname(hostnameUri); + var iconsUrl = _environmentService.IconsUrl; + if (string.IsNullOrWhiteSpace(iconsUrl)) + { + if (!string.IsNullOrWhiteSpace(_environmentService.BaseUrl)) + { + iconsUrl = string.Format("{0}/icons", _environmentService.BaseUrl); + } + else + { + iconsUrl = "https://icons.bitwarden.net"; + } + } + return string.Format("{0}/{1}/icon.png", iconsUrl, hostname); + + } } }