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
This commit is contained in:
Jake Fink 2021-10-12 11:00:33 -04:00 committed by GitHub
parent c9ce7256e5
commit fc1000acc1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 35 deletions

View file

@ -46,7 +46,7 @@
WidthRequest="22" WidthRequest="22"
HeightRequest="22" HeightRequest="22"
IsVisible="{Binding ShowIconImage}" IsVisible="{Binding ShowIconImage}"
Source="{Binding Cipher, Converter={StaticResource iconImageConverter}}" Source="{Binding IconImageSource, Mode=OneTime}"
AutomationProperties.IsInAccessibleTree="False" /> AutomationProperties.IsInAccessibleTree="False" />
<Grid RowSpacing="0" ColumnSpacing="0" Grid.Row="0" Grid.Column="1" VerticalOptions="Center" Padding="0, 7"> <Grid RowSpacing="0" ColumnSpacing="0" Grid.Row="0" Grid.Column="1" VerticalOptions="Center" Padding="0, 7">

View file

@ -1,4 +1,5 @@
using Bit.Core.Models.View; using Bit.App.Utilities;
using Bit.Core.Models.View;
using Bit.Core.Utilities; using Bit.Core.Utilities;
namespace Bit.App.Controls namespace Bit.App.Controls
@ -7,6 +8,7 @@ namespace Bit.App.Controls
{ {
private CipherView _cipher; private CipherView _cipher;
private bool _websiteIconsEnabled; private bool _websiteIconsEnabled;
private string _iconImageSource = string.Empty;
public CipherViewCellViewModel(CipherView cipherView, bool websiteIconsEnabled) public CipherViewCellViewModel(CipherView cipherView, bool websiteIconsEnabled)
{ {
@ -28,8 +30,22 @@ namespace Bit.App.Controls
public bool ShowIconImage public bool ShowIconImage
{ {
get => WebsiteIconsEnabled && !string.IsNullOrWhiteSpace(Cipher.Login?.Uri) && get => WebsiteIconsEnabled
Cipher.Login.Uri.StartsWith("http"); && !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;
}
} }
} }
} }

View file

@ -10,8 +10,6 @@ namespace Bit.App.Utilities
{ {
public class IconImageConverter : IValueConverter public class IconImageConverter : IValueConverter
{ {
private readonly IEnvironmentService _environmentService = ServiceContainer.Resolve<IEnvironmentService>("environmentService");
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
var cipher = value as CipherView; var cipher = value as CipherView;
@ -29,34 +27,50 @@ namespace Bit.App.Utilities
switch (cipher.Type) switch (cipher.Type)
{ {
case CipherType.Login: case CipherType.Login:
icon = GetLoginIconImage(cipher); icon = IconImageHelper.GetLoginIconImage(cipher);
break; break;
default: default:
break; break;
} }
return icon; return icon;
} }
}
string GetLoginIconImage(CipherView cipher) public static class IconImageHelper
{
public static string GetLoginIconImage(CipherView cipher)
{ {
string image = null; string image = null;
if (cipher.Login.Uri != null) if (cipher.Login.HasUris)
{ {
var hostnameUri = cipher.Login.Uri; foreach (var uri in cipher.Login.Uris)
{
var hostnameUri = uri.Uri;
var isWebsite = false; var isWebsite = false;
if (!hostnameUri.Contains("."))
if (!hostnameUri.Contains("://") && hostnameUri.Contains(".")) {
continue;
}
if (!hostnameUri.Contains("://"))
{ {
hostnameUri = string.Concat("http://", hostnameUri); hostnameUri = string.Concat("http://", hostnameUri);
isWebsite = true;
}
else
{
isWebsite = hostnameUri.StartsWith("http") && hostnameUri.Contains(".");
} }
isWebsite = hostnameUri.StartsWith("http");
if (isWebsite) if (isWebsite)
{ {
image = GetIconUrl(hostnameUri);
break;
}
}
}
return image;
}
private static string GetIconUrl(string hostnameUri)
{
IEnvironmentService _environmentService = ServiceContainer.Resolve<IEnvironmentService>("environmentService");
var hostname = CoreHelpers.GetHostname(hostnameUri); var hostname = CoreHelpers.GetHostname(hostnameUri);
var iconsUrl = _environmentService.IconsUrl; var iconsUrl = _environmentService.IconsUrl;
if (string.IsNullOrWhiteSpace(iconsUrl)) if (string.IsNullOrWhiteSpace(iconsUrl))
@ -70,10 +84,8 @@ namespace Bit.App.Utilities
iconsUrl = "https://icons.bitwarden.net"; iconsUrl = "https://icons.bitwarden.net";
} }
} }
image = string.Format("{0}/{1}/icon.png", iconsUrl, hostname); return string.Format("{0}/{1}/icon.png", iconsUrl, hostname);
}
}
return image;
} }
} }
} }