1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-21 03:01:30 +02:00
bitwarden-mobile/src/App/Controls/CipherViewCell/CipherViewCell.xaml.cs

146 lines
4.7 KiB
C#
Raw Normal View History

2019-04-22 23:08:37 +02:00
using Bit.App.Pages;
using Bit.Core;
using Bit.Core.Enums;
using Bit.Core.Models.View;
using Bit.Core.Utilities;
using System;
2019-03-29 21:52:57 +01:00
using Xamarin.Forms;
namespace Bit.App.Controls
{
public partial class CipherViewCell : ViewCell
{
public static readonly BindableProperty CipherProperty = BindableProperty.Create(
nameof(Cipher), typeof(CipherView), typeof(CipherViewCell), default(CipherView), BindingMode.OneWay);
2019-04-30 17:27:53 +02:00
public static readonly BindableProperty ButtonCommandProperty = BindableProperty.Create(
nameof(ButtonCommand), typeof(Command<CipherView>), typeof(CipherViewCell));
2019-03-29 21:52:57 +01:00
private CipherViewCellViewModel _viewModel;
public CipherViewCell()
{
InitializeComponent();
2019-04-22 23:08:37 +02:00
_viewModel = _grid.BindingContext as CipherViewCellViewModel;
2019-03-29 21:52:57 +01:00
}
public CipherView Cipher
{
get => GetValue(CipherProperty) as CipherView;
set => SetValue(CipherProperty, value);
}
2019-04-30 17:27:53 +02:00
public Command<CipherView> ButtonCommand
{
get => GetValue(ButtonCommandProperty) as Command<CipherView>;
set => SetValue(ButtonCommandProperty, value);
}
2019-03-29 21:52:57 +01:00
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if(propertyName == CipherProperty.PropertyName)
{
_viewModel.Cipher = Cipher;
}
}
2019-04-22 23:08:37 +02:00
protected override void OnBindingContextChanged()
{
string icon = null;
string image = null;
_image.Source = null;
CipherView cipher = null;
if(BindingContext is GroupingsPageListItem groupingsPageListItem)
{
cipher = groupingsPageListItem.Cipher;
}
else if(BindingContext is CipherView cv)
{
cipher = cv;
}
if(cipher != null)
2019-04-22 23:08:37 +02:00
{
switch(cipher.Type)
2019-04-22 23:08:37 +02:00
{
case CipherType.Login:
var loginIconImage = GetLoginIconImage(cipher);
2019-04-22 23:08:37 +02:00
icon = loginIconImage.Item1;
image = loginIconImage.Item2;
break;
case CipherType.SecureNote:
2019-04-25 05:53:36 +02:00
icon = "";
2019-04-22 23:08:37 +02:00
break;
case CipherType.Card:
2019-04-25 05:53:36 +02:00
icon = "";
2019-04-22 23:08:37 +02:00
break;
case CipherType.Identity:
2019-04-25 05:53:36 +02:00
icon = "";
2019-04-22 23:08:37 +02:00
break;
default:
break;
}
if(image != null)
{
_image.IsVisible = true;
_icon.IsVisible = false;
_image.Source = image;
_image.LoadingPlaceholder = "login.png";
}
else
{
_image.IsVisible = false;
_icon.IsVisible = true;
_icon.Text = icon;
}
}
base.OnBindingContextChanged();
}
private Tuple<string, string> GetLoginIconImage(CipherView cipher)
{
2019-04-25 05:53:36 +02:00
string icon = "";
2019-04-22 23:08:37 +02:00
string image = null;
var imageEnabled = true;
if(cipher.Login.Uri != null)
{
var hostnameUri = cipher.Login.Uri;
var isWebsite = false;
if(hostnameUri.StartsWith(Constants.AndroidAppProtocol))
{
2019-04-25 05:53:36 +02:00
icon = "";
2019-04-22 23:08:37 +02:00
}
else if(hostnameUri.StartsWith(Constants.iOSAppProtocol))
{
2019-04-25 05:53:36 +02:00
icon = "";
2019-04-22 23:08:37 +02:00
}
else if(imageEnabled && !hostnameUri.Contains("://") && hostnameUri.Contains("."))
{
hostnameUri = string.Concat("http://", hostnameUri);
isWebsite = true;
}
else if(imageEnabled)
{
isWebsite = hostnameUri.StartsWith("http") && hostnameUri.Contains(".");
}
if(imageEnabled && isWebsite)
{
var hostname = CoreHelpers.GetHostname(hostnameUri);
var iconsUrl = "https://icons.bitwarden.net";
image = string.Format("{0}/{1}/icon.png", iconsUrl, hostname);
}
}
return new Tuple<string, string>(icon, image);
}
2019-04-30 17:27:53 +02:00
private void ImageButton_Clicked(object sender, EventArgs e)
{
ButtonCommand?.Execute(Cipher);
}
2019-03-29 21:52:57 +01:00
}
}