Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Refactoring: Remove redundant logic from "Not" converters (#935)
Browse files Browse the repository at this point in the history
* Convert "Not" converters to use existing logic

* Revert "Convert "Not" converters to use existing logic"

This reverts commit 62c43e1.

* Use ststic method instead of inheritance

* Commit to rerun tests

Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com>
Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
  • Loading branch information
3 people committed Feb 28, 2021
1 parent 58b4f04 commit 0db9352
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public class EqualConverter : ValueConverterExtension, IValueConverter
/// <param name="parameter">The second object to compare.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>True if <paramref name="value"/> and <paramref name="parameter"/> are equal, False if they are not equal.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> (value != null && value.Equals(parameter)) || (value == null && parameter == null);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => ConvertInternal(value, parameter);

internal static bool ConvertInternal(object value, object parameter) =>
(value != null && value.Equals(parameter)) || (value == null && parameter == null);

/// <summary>
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
Expand All @@ -29,7 +31,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
/// <param name="parameter">N/A</param>
/// <param name="culture">N/A</param>
/// <returns>N/A</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class IsNotNullOrEmptyConverter : ValueConverterExtension, IValueConverte
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>A <see cref="bool"/> indicating if the incoming value is not null and not empty.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> value is string str ? !string.IsNullOrWhiteSpace(str) : value != null;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
!IsNullOrEmptyConverter.ConvertInternal(value);

/// <summary>
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public class IsNullOrEmptyConverter : ValueConverterExtension, IValueConverter
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>A <see cref="bool"/> indicating if the incoming value is null or empty.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> value == null || (value is string str && string.IsNullOrWhiteSpace(str));
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => ConvertInternal(value);

internal static bool ConvertInternal(object value) =>
value == null || (value is string str && string.IsNullOrWhiteSpace(str));

/// <summary>
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Globalization;
using Xamarin.CommunityToolkit.Extensions.Internals;
using Xamarin.Forms;
Expand All @@ -19,16 +18,8 @@ public class ListIsNotNullOrEmptyConverter : ValueConverterExtension, IValueConv
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>A <see cref="bool"/> indicating if the incoming value is not null and not empty.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is null)
return false;

if (value is IEnumerable list)
return list.GetEnumerator().MoveNext();

throw new ArgumentException("Value is not a valid IEnumerable or null", nameof(value));
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
!ListIsNullOrEmptyConverter.ConvertInternal(value);

/// <summary>
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class ListIsNullOrEmptyConverter : ValueConverterExtension, IValueConvert
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>A <see cref="bool"/> indicating if the incoming value is null or empty.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => ConvertInternal(value);

internal static bool ConvertInternal(object value)
{
if (value is null)
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class NotEqualConverter : ValueConverterExtension, IValueConverter
/// <param name="parameter">The second object to compare.</param>
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>True if <paramref name="value"/> and <paramref name="parameter"/> are not equal, False if they are equal.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> !((value != null && value.Equals(parameter)) || (value == null && parameter == null));
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
!EqualConverter.ConvertInternal(value, parameter);

/// <summary>
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
Expand Down

0 comments on commit 0db9352

Please sign in to comment.