Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[Proposal] Is In Range Converter

Open TheCodeTraveler opened this issue 4 years ago • 1 comments

IsInRangeConverter

  • [x] Proposed
  • [ ] Prototype: Not Started
  • [ ] Implementation: Not Started
    • [ ] iOS Support
    • [ ] Android Support
    • [ ] macOS Support
    • [ ] Windows Support
  • [ ] Unit Tests: Not Started
  • [ ] Sample: Not Started
  • [ ] Documentation: Not Started

Summary

The IsInRangeConverter is a converter that allows users to convert a object value into a boolean value, checking if the value is between MinValue and MaxValue, returning true if the value is within the range and false if the value is out of the range

Detailed Design

IsInRangeConverter.shared.cs

public class IsInRangeConverter : ValueConverterExtension, IValueConverter
{
  public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(object), typeof(IsInRangeConverter));

  public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(object), typeof(IsInRangeConverter));

  public object? MinValue
  {
	get => GetValue(MinValueProperty);
	set => SetValue(MinValueProperty, value);
  }

  public object? MaxValue
  {
	get => GetValue(MaxValueProperty);
	set => SetValue(MaxValueProperty, value);
  }

  public object Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture)
  {
	if (value is not IComparable comparable)
		throw new ArgumentException("is expected to implement IComparable interface.", nameof(value));

	if (MinValue is not IComparable)
		throw new ArgumentException("is expected to implement IComparable interface.", nameof(MinValue));

	if (MaxValue is not IComparable)
		throw new ArgumentException("is expected to implement IComparable interface.", nameof(MaxValue));

	return comparable.CompareTo(MinValue) >= 0 && comparable.CompareTo(MaxValue) <= 0;
  }

  public object? ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture) => throw new NotImplementedException();
}

Usage Syntax

XAML Usage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             xmlns:myEnum="MyLittleApp.Models"
             x:Class="MyLittleApp.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
          <xct:IsInRangeConverter
                          x:Key="IsInRangeConverter"
                          MaxValue= "1"
                          MinValue= "3" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>
        <Label IsVisible="{Binding Number, Converter={StaticResource IssueStateConverter } }" />

    </StackLayout>
</ContentPage>

C# Usage

class MyPage : ContentPage
{
  public MyPage()
  {
    Content = new StackLayout
    {
      Children = 
      {
          new Label().Bind(Label.IsVisibleProperty, nameof(ViewModel.Number), converter: new IsInRangeConverter { MinValue = 1, MaxValue = 3 }),
      };
    }
  }
}

TheCodeTraveler avatar Sep 25 '21 20:09 TheCodeTraveler

I have a working prototype here: 65-IsInRangeConverter. I have samples for most of the IComparable derived types and will work on the others for completeness. NOTE: I have left in GreaterThan and LessThan capabilities as these are easy in the converter (I do know that CompareConverter already does this). image

GeorgeLeithead avatar Aug 18 '22 15:08 GeorgeLeithead

Bump

GeorgeLeithead avatar Oct 19 '22 16:10 GeorgeLeithead

Hey George! I've added the https://github.com/CommunityToolkit/Maui/labels/needs%20discussion label, which means we'll discuss this in our next standup.

In other words, I'll bring raise this to the team to see if any of the maintainers have the bandwidth to Champion this proposal.

TheCodeTraveler avatar Oct 19 '22 20:10 TheCodeTraveler

Hi George! Thanks for your work on this proposal. I will be championing this proposal. I will bring this to a vote to be approved so you can create a pull request. Thanks.

JoonghyunCho avatar Oct 31 '22 20:10 JoonghyunCho

Reopening Proposal.

Only Proposals moved to the Closed Project Column and Completed Project Column can be closed.

ghost avatar Dec 01 '22 00:12 ghost

@brminnick I think that this proposal an be moved to the Closed project column and Completed project column to close.

GeorgeLeithead avatar Apr 24 '23 10:04 GeorgeLeithead