Skip to content

Commit

Permalink
Added bool to double converter (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErlingMK authored Jun 28, 2022
1 parent e598628 commit 0582f30
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using DIPS.Xamarin.UI.Internal.Utilities;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace DIPS.Xamarin.UI.Converters.ValueConverters
{
/// <summary>
/// Converts a boolean input value to it's respective <see cref="TrueDouble"/> or <see cref="FalseDouble"/> depending on the <see cref="Inverted"/> value
/// </summary>
public class BoolToDoubleConverter : IValueConverter, IMarkupExtension
{
private IServiceProvider m_serviceProvider;

/// <summary>
/// The value that will return if the boolean input is true
/// <remarks>Will be the return value if <see cref="Inverted" /> is set to true</remarks>
/// </summary>
public double TrueDouble { get; set; }

/// <summary>
/// The value that will return if the boolean input is false
/// <remarks>Will be the return value if <see cref="Inverted" /> is set to false</remarks>
/// </summary>
public double FalseDouble { get; set; }

/// <summary>
/// A boolean value to set if the output value should be inverted
/// </summary>
public bool Inverted { get; set; }

/// <inheritdoc />
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not bool inputValue)
{
throw new XamlParseException($"Input value has to be of type {nameof(Boolean)}").WithXmlLineInfo(
m_serviceProvider);
}

return Inverted ? inputValue ? FalseDouble : TrueDouble : inputValue ? TrueDouble : FalseDouble;
}

/// <inheritdoc />
[ExcludeFromCodeCoverage]
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

/// <inheritdoc />
[ExcludeFromCodeCoverage]
public object ProvideValue(IServiceProvider serviceProvider)
{
m_serviceProvider = serviceProvider;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
mc:Ignorable="d">
<ContentPage.Content>
<StackLayout>
<Button Command="{Binding NavigateToCommand}"
CommandParameter="BoolToDouble"
Text="BoolToDoubleConverter" />
<Button Command="{Binding NavigateToCommand}"
CommandParameter="BoolToObject"
Text="BoolToObjectConverter" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ private void NavigateTo(string parameter)
{
switch (parameter)
{
case "BoolToDouble":
m_navigation.PushAsync(new BoolToDoubleConverterPage(){Title = parameter});
break;
case "BoolToObject":
m_navigation.PushAsync(new BoolToObjectConverterPage(){Title = parameter});
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:valueConverters="http://dips.xamarin.ui.com"
xmlns:valueConverters1="clr-namespace:DIPS.Xamarin.UI.Samples.Converters.ValueConverters;assembly=DIPS.Xamarin.UI.Samples"
x:Class="DIPS.Xamarin.UI.Samples.Converters.ValueConverters.BoolToDoubleConverterPage">
<ContentPage.BindingContext>
<valueConverters1:BoolToDoubleConverterViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>

<StackLayout Grid.Row="0"
Orientation="Horizontal">
<CheckBox IsChecked="{Binding SomeLogicalProperty}" />
<Label Text="Set me" VerticalOptions="Center" />
</StackLayout>

<StackLayout Grid.Row="1">
<Label
Opacity="{Binding SomeLogicalProperty, Converter={valueConverters:BoolToDoubleConverter TrueDouble=0.5, FalseDouble=1}}"
Text="My opacity changes" />
<Label
Opacity="{Binding SomeLogicalProperty, Converter={valueConverters:BoolToDoubleConverter TrueDouble=0.5, FalseDouble=1, Inverted=True}}"
Text="My opacity is inverted" />
</StackLayout>
</Grid>
</ContentPage.Content>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DIPS.Xamarin.UI.Extensions;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace DIPS.Xamarin.UI.Samples.Converters.ValueConverters
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BoolToDoubleConverterPage : ContentPage
{
public BoolToDoubleConverterPage()
{
InitializeComponent();
}
}

public class BoolToDoubleConverterViewModel : INotifyPropertyChanged
{
private bool m_someLogicalProperty;
public event PropertyChangedEventHandler PropertyChanged;

public bool SomeLogicalProperty
{
get => m_someLogicalProperty;
set => this.Set(ref m_someLogicalProperty, value, PropertyChanged);
}

}
}

0 comments on commit 0582f30

Please sign in to comment.