mirror of
https://github.com/bitwarden/mobile
synced 2026-01-04 17:43:17 +00:00
Change password generator to use ColoredPassword (#686)
* Change password generator to use ColoredPassword * Change ColoredPassword from FormattedString to HTML string for improved performance * PasswordFormatter fixes * Correct || to && condition * Apply password colouring to history pages
This commit is contained in:
committed by
Kyle Spearrin
parent
051e15215d
commit
a9dacd561c
28
src/App/Utilities/ColoredPasswordConverter.cs
Normal file
28
src/App/Utilities/ColoredPasswordConverter.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Bit.App.Utilities
|
||||
{
|
||||
public class ColoredPasswordConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter,
|
||||
System.Globalization.CultureInfo culture)
|
||||
{
|
||||
if(targetType != typeof(string))
|
||||
{
|
||||
throw new InvalidOperationException("The target must be a string.");
|
||||
}
|
||||
if(value == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return PasswordFormatter.FormatPassword((string)value);
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter,
|
||||
System.Globalization.CultureInfo culture)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,17 +21,19 @@ namespace Bit.App.Utilities
|
||||
Special
|
||||
}
|
||||
|
||||
public static FormattedString FormatPassword(string password)
|
||||
public static string FormatPassword(string password)
|
||||
{
|
||||
if(password == null)
|
||||
{
|
||||
return new FormattedString();
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var result = new FormattedString();
|
||||
// Start off with an empty span to prevent possible NPEs. Due to the way the state machine
|
||||
// works, this will actually always be replaced by a new span anyway.
|
||||
var currentSpan = new Span();
|
||||
// First two digits of returned hex code contains the alpha,
|
||||
// which is not supported in HTML color, so we need to cut those out.
|
||||
var numberColor = $"<span style=\"color:#{((Color)Application.Current.Resources["PasswordNumberColor"]).ToHex().Substring(2)}\">";
|
||||
var specialColor = $"<span style=\"color:#{((Color)Application.Current.Resources["PasswordSpecialColor"]).ToHex().Substring(2)}\">";
|
||||
var result = string.Empty;
|
||||
|
||||
// Start with an otherwise uncovered case so we will definitely enter the "something changed"
|
||||
// state.
|
||||
var currentType = CharType.None;
|
||||
@@ -56,24 +58,36 @@ namespace Bit.App.Utilities
|
||||
// If the char type changed, build a new span to append the text to.
|
||||
if(charType != currentType)
|
||||
{
|
||||
currentSpan = new Span();
|
||||
result.Spans.Add(currentSpan);
|
||||
// Close off previous span.
|
||||
if (currentType != CharType.None && currentType != CharType.Normal)
|
||||
{
|
||||
result += "</span>";
|
||||
}
|
||||
|
||||
currentType = charType;
|
||||
|
||||
// Switch the color if it is not a normal text. Otherwise leave the
|
||||
// default value.
|
||||
switch(currentType)
|
||||
{
|
||||
// Apply color style to span.
|
||||
case CharType.Number:
|
||||
currentSpan.TextColor = (Color)Application.Current.Resources["PasswordNumberColor"];
|
||||
result += numberColor;
|
||||
break;
|
||||
case CharType.Special:
|
||||
currentSpan.TextColor = (Color)Application.Current.Resources["PasswordSpecialColor"];
|
||||
result += specialColor;
|
||||
break;
|
||||
}
|
||||
}
|
||||
currentSpan.Text += c;
|
||||
result += c;
|
||||
}
|
||||
|
||||
// Close off last span.
|
||||
if (currentType != CharType.None && currentType != CharType.Normal)
|
||||
{
|
||||
result += "</span>";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user