From cf30d35aca287448f7f7b6ba5ae1dbe3ee4c285e Mon Sep 17 00:00:00 2001 From: akv3sic Date: Sat, 11 Nov 2023 22:40:31 +0100 Subject: [PATCH] Add feature to toggle password visibility --- .../Converters/BoolToPasswordIconConverter.cs | 26 +++++++++++++++++ MauiStoreApp/Resources/Images/eye_off.svg | 8 ++++++ MauiStoreApp/Resources/Images/eye_show.svg | 5 ++++ MauiStoreApp/ViewModels/LoginViewModel.cs | 15 ++++++++++ MauiStoreApp/Views/LoginPage.xaml | 28 +++++++++++++++---- 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 MauiStoreApp/Converters/BoolToPasswordIconConverter.cs create mode 100644 MauiStoreApp/Resources/Images/eye_off.svg create mode 100644 MauiStoreApp/Resources/Images/eye_show.svg diff --git a/MauiStoreApp/Converters/BoolToPasswordIconConverter.cs b/MauiStoreApp/Converters/BoolToPasswordIconConverter.cs new file mode 100644 index 0000000..1750ebf --- /dev/null +++ b/MauiStoreApp/Converters/BoolToPasswordIconConverter.cs @@ -0,0 +1,26 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Kvesic, Matkovic, FSRE. All rights reserved. +// +// ----------------------------------------------------------------------- + +using System.Globalization; + +namespace MauiStoreApp.Converters +{ + /// + /// Converts a boolean value to a password icon. + /// + 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(); + } + } +} diff --git a/MauiStoreApp/Resources/Images/eye_off.svg b/MauiStoreApp/Resources/Images/eye_off.svg new file mode 100644 index 0000000..5a0b98a --- /dev/null +++ b/MauiStoreApp/Resources/Images/eye_off.svg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/MauiStoreApp/Resources/Images/eye_show.svg b/MauiStoreApp/Resources/Images/eye_show.svg new file mode 100644 index 0000000..87a0df3 --- /dev/null +++ b/MauiStoreApp/Resources/Images/eye_show.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/MauiStoreApp/ViewModels/LoginViewModel.cs b/MauiStoreApp/ViewModels/LoginViewModel.cs index ed39d66..c62a584 100644 --- a/MauiStoreApp/ViewModels/LoginViewModel.cs +++ b/MauiStoreApp/ViewModels/LoginViewModel.cs @@ -41,6 +41,12 @@ public LoginViewModel() [ObservableProperty] string password; + /// + /// Gets or sets a value indicating whether the password is visible. + /// + [ObservableProperty] + bool isPasswordVisible; + private LoginResponse loginResponse = new LoginResponse(); /// @@ -92,5 +98,14 @@ public async Task Login() IsBusy = false; } } + + /// + /// Toggles the password visibility. + /// + [RelayCommand] + public void TogglePasswordVisibility() + { + this.IsPasswordVisible = !this.IsPasswordVisible; + } } } diff --git a/MauiStoreApp/Views/LoginPage.xaml b/MauiStoreApp/Views/LoginPage.xaml index b90fd19..72bfa2f 100644 --- a/MauiStoreApp/Views/LoginPage.xaml +++ b/MauiStoreApp/Views/LoginPage.xaml @@ -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"> @@ -29,6 +31,8 @@ + + @@ -60,11 +64,25 @@ Style="{StaticResource EntryStyle}" Text="{Binding Username}" /> - + + + + + + + + +