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

Commit 0db9352

Browse files
maxkoshevoipictosjsuarezruiz
authored
Refactoring: Remove redundant logic from "Not" converters (#935)
* 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 <[email protected]> Co-authored-by: Javier Suárez <[email protected]>
1 parent 58b4f04 commit 0db9352

File tree

6 files changed

+19
-22
lines changed

6 files changed

+19
-22
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Converters/EqualConverter.shared.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public class EqualConverter : ValueConverterExtension, IValueConverter
1818
/// <param name="parameter">The second object to compare.</param>
1919
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
2020
/// <returns>True if <paramref name="value"/> and <paramref name="parameter"/> are equal, False if they are not equal.</returns>
21-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
22-
=> (value != null && value.Equals(parameter)) || (value == null && parameter == null);
21+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => ConvertInternal(value, parameter);
22+
23+
internal static bool ConvertInternal(object value, object parameter) =>
24+
(value != null && value.Equals(parameter)) || (value == null && parameter == null);
2325

2426
/// <summary>
2527
/// This method is not implemented and will throw a <see cref="NotImplementedException"/>.
@@ -29,7 +31,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
2931
/// <param name="parameter">N/A</param>
3032
/// <param name="culture">N/A</param>
3133
/// <returns>N/A</returns>
32-
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
33-
=> throw new NotImplementedException();
34+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
35+
throw new NotImplementedException();
3436
}
3537
}

src/CommunityToolkit/Xamarin.CommunityToolkit/Converters/IsNotNullOrEmptyConverter.shared.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class IsNotNullOrEmptyConverter : ValueConverterExtension, IValueConverte
1818
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
1919
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
2020
/// <returns>A <see cref="bool"/> indicating if the incoming value is not null and not empty.</returns>
21-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
22-
=> value is string str ? !string.IsNullOrWhiteSpace(str) : value != null;
21+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
22+
!IsNullOrEmptyConverter.ConvertInternal(value);
2323

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

src/CommunityToolkit/Xamarin.CommunityToolkit/Converters/IsNullOrEmptyConverter.shared.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public class IsNullOrEmptyConverter : ValueConverterExtension, IValueConverter
1818
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
1919
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
2020
/// <returns>A <see cref="bool"/> indicating if the incoming value is null or empty.</returns>
21-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
22-
=> value == null || (value is string str && string.IsNullOrWhiteSpace(str));
21+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => ConvertInternal(value);
22+
23+
internal static bool ConvertInternal(object value) =>
24+
value == null || (value is string str && string.IsNullOrWhiteSpace(str));
2325

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

src/CommunityToolkit/Xamarin.CommunityToolkit/Converters/ListIsNotNullOrEmptyConverter.shared.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections;
32
using System.Globalization;
43
using Xamarin.CommunityToolkit.Extensions.Internals;
54
using Xamarin.Forms;
@@ -19,16 +18,8 @@ public class ListIsNotNullOrEmptyConverter : ValueConverterExtension, IValueConv
1918
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
2019
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
2120
/// <returns>A <see cref="bool"/> indicating if the incoming value is not null and not empty.</returns>
22-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
23-
{
24-
if (value is null)
25-
return false;
26-
27-
if (value is IEnumerable list)
28-
return list.GetEnumerator().MoveNext();
29-
30-
throw new ArgumentException("Value is not a valid IEnumerable or null", nameof(value));
31-
}
21+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
22+
!ListIsNullOrEmptyConverter.ConvertInternal(value);
3223

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

src/CommunityToolkit/Xamarin.CommunityToolkit/Converters/ListIsNullOrEmptyConverter.shared.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class ListIsNullOrEmptyConverter : ValueConverterExtension, IValueConvert
1919
/// <param name="parameter">Additional parameter for the converter to handle. This is not implemented.</param>
2020
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
2121
/// <returns>A <see cref="bool"/> indicating if the incoming value is null or empty.</returns>
22-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
22+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => ConvertInternal(value);
23+
24+
internal static bool ConvertInternal(object value)
2325
{
2426
if (value is null)
2527
return true;

src/CommunityToolkit/Xamarin.CommunityToolkit/Converters/NotEqualConverter.shared.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class NotEqualConverter : ValueConverterExtension, IValueConverter
1818
/// <param name="parameter">The second object to compare.</param>
1919
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
2020
/// <returns>True if <paramref name="value"/> and <paramref name="parameter"/> are not equal, False if they are equal.</returns>
21-
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
22-
=> !((value != null && value.Equals(parameter)) || (value == null && parameter == null));
21+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
22+
!EqualConverter.ConvertInternal(value, parameter);
2323

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

0 commit comments

Comments
 (0)