Closed Cfun1 closed 3 years ago
What about using a ConditionOperator enum?
@GUOLDEV Thanks I didn't know about ConditionOperator, it seems interesting, we'll check what better for this implementation a classic switch statement or ConditionOperator. Is it available out of the box in Xamarin.Forms ?
What about using a ConditionOperator enum?
This ConditionOoerator is part of the Microsoft.Xrm.Sdk namespace, which is used for developing with Microsoft Dynamics. Not sure if you would want to use that one in this scenario.
Actually, I was thinking about something very simple as the sample code below:
public class CompareConverter : ValueConverterExtension, IValueConverter
{
enum Conditional
{
Equal,
NotEqual,
LessThan,
GreaterThan,
LessThanOrEqual,
GreaterThanOrEqual,
}
public Conditional? Condition { get; set; }
public IList<Conditional> Conditions { get; set; } // If you prefer combining enums rather than specific single ones
public object? ValueToCompare { get; set; }
public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture)
{
if(value is not IComparable c1 || ValueToCompare is not IComparable c2)
throw new NotSupportedException();
var result = c1.CompareTo(c2);
switch (Condition)
{
case Conditional.NotEqual:
return result != 0;
case Conditional.Equal:
return result == 0;
case Conditional.GreaterThan:
return result > 0;
case Conditional.LessThan:
return result < 0;
default:
return false;
}
}
}
Thanks @GUOLDEV it seems a nice readable approach.
@AndreiMisiukevich what do you think about this feature request ?
@Cfun1 if the CompareConverter idea is ok and if you don't mind, I am happy to create a PR for this issue.
CompareConverter sounds good
@GUOLDEV I have already an implementation but I believe we can merge the best of both, I was just waiting for the approval process feedback and thoughts before sending a pr.
@GUOLDEV, @Cfun1 marked this issue as his want to work on it, so I ask you to let him do the implementation. Thanks
That's great @Cfun1 and thanks for considering merging the ideas, let me know if you need any help 👍
Also I think I can implement it in MathExpressionConverter (#1117)
@sattew I don't think MathExpressionConverter
serves the same purpose, this converter will returns a boolean or an object defined by the dev while MathExpressionConverter
calculate a math expression and returns the result, IMHO better we don't mix up those two converters.
@AndreiMisiukevich @pictos What are your thoughts?
@sattew I don't think
MathExpressionConverter
serves the same purpose, this converter will returns a boolean or an object defined by the dev whileMathExpressionConverter
calculate a math expression and returns the result, IMHO better we don't mix up those two converters. @AndreiMisiukevich @pictos What are your thoughts?
Yeah, that is right too. I think MathExpressionConverter can include this logic because the Math includes inequalities. [UP] Ah, MathExpression can compare just digits and it can't be used with IComparable. Maybe we need a new converter.
Implemented in https://github.com/xamarin/XamarinCommunityToolkit/pull/1172
Summary
Add
ConditionalDoubleConverter
which seem to have similar name as existingIntToBoolConverter
but the aim is totally different.Input is a double and output is a bool, the difference with
IntToBoolConverter
is that the value of this bool depends on a condition set by the dev, <, >, ==, <=, >=, !=.API Changes
or better we could extend to more generic than
bool
like FalseObject TrueObject thing like inBoolToObjectConverter
.if
TrueObject
andFalseObject
are not specified it will return the related bool.Intended Use Case
A real example could be used to implement a parallax effect, once the scroll value reach a certain threshold you want to set invisible a view.
Who Will Do The Work?