Skip to content

Commit

Permalink
Add feature to toggle password visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
akv3sic committed Nov 11, 2023
1 parent a44bc1f commit cf30d35
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
26 changes: 26 additions & 0 deletions MauiStoreApp/Converters/BoolToPasswordIconConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// -----------------------------------------------------------------------
// <copyright file="BoolToPasswordIconConverter.cs" company="Kvesic, Matkovic, FSRE">
// Copyright (c) Kvesic, Matkovic, FSRE. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System.Globalization;

namespace MauiStoreApp.Converters
{
/// <summary>
/// Converts a boolean value to a password icon.
/// </summary>
public class BoolToPasswordIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? "eye_off.svg" : "eye_show.svg";
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
8 changes: 8 additions & 0 deletions MauiStoreApp/Resources/Images/eye_off.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions MauiStoreApp/Resources/Images/eye_show.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions MauiStoreApp/ViewModels/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public LoginViewModel()
[ObservableProperty]
string password;

/// <summary>
/// Gets or sets a value indicating whether the password is visible.
/// </summary>
[ObservableProperty]
bool isPasswordVisible;

private LoginResponse loginResponse = new LoginResponse();

/// <summary>
Expand Down Expand Up @@ -92,5 +98,14 @@ public async Task Login()
IsBusy = false;
}
}

/// <summary>
/// Toggles the password visibility.
/// </summary>
[RelayCommand]
public void TogglePasswordVisibility()
{
this.IsPasswordVisible = !this.IsPasswordVisible;
}
}
}
28 changes: 23 additions & 5 deletions MauiStoreApp/Views/LoginPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
x:Class="MauiStoreApp.Views.LoginPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:MauiStoreApp.Converters"
xmlns:local="clr-namespace:MauiStoreApp.ViewModels"
xmlns:model="clr-namespace:MauiStoreApp.Models"
xmlns:modelroot="clr-namespace:MauiStoreApp.Models"
xmlns:toolkit="clr-namespace:CommunityToolkit.Maui.Converters;assembly=CommunityToolkit.Maui"
x:DataType="local:LoginViewModel"
BackgroundColor="#FFF"
Shell.NavBarIsVisible="True">
Expand All @@ -29,6 +31,8 @@
<Setter Property="CornerRadius" Value="20" />
<Setter Property="Margin" Value="20" />
</Style>
<converters:BoolToPasswordIconConverter x:Key="BoolToPasswordIconConverter" />
<toolkit:InvertedBoolConverter x:Key="InvertedBoolConverter" />
</ResourceDictionary>
</ContentPage.Resources>

Expand Down Expand Up @@ -60,11 +64,25 @@
Style="{StaticResource EntryStyle}"
Text="{Binding Username}" />

<Entry
IsPassword="True"
Placeholder="Zaporka"
Style="{StaticResource EntryStyle}"
Text="{Binding Password}" />
<AbsoluteLayout HeightRequest="50">
<Entry
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
IsPassword="{Binding IsPasswordVisible, Converter={StaticResource InvertedBoolConverter}}"
Placeholder="Zaporka"
Style="{StaticResource EntryStyle}"
Text="{Binding Password}" />

<Image
AbsoluteLayout.LayoutBounds="0.98,0.5,21,21"
AbsoluteLayout.LayoutFlags="PositionProportional"
BackgroundColor="Transparent"
Source="{Binding IsPasswordVisible, Converter={StaticResource BoolToPasswordIconConverter}}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding TogglePasswordVisibilityCommand}" />
</Image.GestureRecognizers>
</Image>
</AbsoluteLayout>

<Label
FontAttributes="Bold"
Expand Down

0 comments on commit cf30d35

Please sign in to comment.