From 33c1f5083abb8998a621258e3075a71b1d3d17e8 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sat, 23 Jan 2021 11:57:59 +0200 Subject: [PATCH 01/21] Create CommandHelper.shared.cs --- .../Helpers/CommandHelper.shared.cs | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/CommunityToolkit/Xamarin.CommunityToolkit/Helpers/CommandHelper.shared.cs diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Helpers/CommandHelper.shared.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Helpers/CommandHelper.shared.cs new file mode 100644 index 000000000..94cfe4c65 --- /dev/null +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Helpers/CommandHelper.shared.cs @@ -0,0 +1,67 @@ +using System; +using System.Threading.Tasks; +using Xamarin.CommunityToolkit.ObjectModel; +using Xamarin.Forms; + +namespace Xamarin.CommunityToolkit.Helpers +{ + public static class CommandHelper + { + public static Command Create(Action execute, Func canExecute = null) => + new Command(execute, canExecute ?? (() => true)); + + public static Command Create(Action execute, Func canExecute = null) => + new Command(ConvertExecute(execute), ConvertCanExecute(canExecute)); + + public static Command Create(Action execute, Func canExecute) => + new Command(ConvertExecute(execute), ConvertCanExecute(canExecute)); + + public static IAsyncCommand Create( + Func execute, + Func canExecute = null, + Action onException = null, + bool continueOnCapturedContext = false, + bool allowsMultipleExecutions = true) => + new AsyncCommand(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions); + + public static IAsyncCommand Create( + Func execute, + Func canExecute = null, + Action onException = null, + bool continueOnCapturedContext = false, + bool allowsMultipleExecutions = true) => + new AsyncCommand(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions); + + public static IAsyncCommand Create( + Func execute, + Func canExecute = null, + Action onException = null, + bool continueOnCapturedContext = false, + bool allowsMultipleExecutions = true) => + new AsyncCommand(execute, canExecute, onException, continueOnCapturedContext, allowsMultipleExecutions); + + static Action ConvertExecute(Action execute) + { + if (execute is null) + return null; + + return e => execute((T)e); + } + + static Func ConvertCanExecute(Func canExecute) + { + if (canExecute is null) + return _ => true; + + return _ => canExecute(); + } + + static Func ConvertCanExecute(Func canExecute) + { + if (canExecute is null) + return _ => true; + + return e => canExecute((T)e); + } + } +} From 4a12da211bc48a771b347af8bd264fd6e1485b16 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Sat, 23 Jan 2021 12:40:06 +0200 Subject: [PATCH 02/21] Use CommandHelper in Sample app --- samples/XCT.Sample/Pages/AboutPage.xaml | 6 ++++- samples/XCT.Sample/Pages/AboutPage.xaml.cs | 11 --------- samples/XCT.Sample/Pages/Base/BasePage.cs | 8 +++---- .../ByteArrayToImageSourcePage.xaml | 4 ++++ .../ByteArrayToImageSourcePage.xaml.cs | 10 +------- .../Pages/Effects/TouchEffectPage.xaml.cs | 8 +++---- .../TestCases/TouchEffectButtonPage.xaml.cs | 11 +++++---- .../XCT.Sample/ViewModels/AboutViewModel.cs | 24 ++++++++++++------- .../ViewModels/Base/BaseGalleryViewModel.cs | 13 +++++----- .../EventToCommandBehaviorViewModel.cs | 9 +++---- .../MaxLengthReachedBehaviorViewModel.cs | 4 ++-- .../UserStoppedTypingBehaviorViewModel.cs | 4 ++-- .../ByteArrayToImageSourceViewModel.cs | 7 ++++++ .../ItemSelectedEventArgsViewModel.cs | 6 ++--- .../ItemTappedEventArgsViewModel.cs | 6 ++--- .../Converters/ListIsNullOrEmptyViewModel.cs | 7 +++--- .../ViewModels/Markup/SearchViewModel.cs | 19 +++++++++------ .../XCT.Sample/ViewModels/SettingViewModel.cs | 16 +++++++------ .../ViewModels/Views/BadgeViewViewModel.cs | 15 ++++++++---- .../ViewModels/Views/ExpanderViewModel.cs | 22 +++++++++-------- .../Views/GravatarImageViewModel.cs | 16 ++++--------- .../ViewModels/Views/StateLayoutViewModel.cs | 21 ++++------------ .../Views/TabItemsSourceViewModel.cs | 8 +++++-- 23 files changed, 130 insertions(+), 125 deletions(-) diff --git a/samples/XCT.Sample/Pages/AboutPage.xaml b/samples/XCT.Sample/Pages/AboutPage.xaml index 8fbf440c9..9abebc964 100644 --- a/samples/XCT.Sample/Pages/AboutPage.xaml +++ b/samples/XCT.Sample/Pages/AboutPage.xaml @@ -14,7 +14,11 @@ - + + + + +