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

Add CommandFactory class #797

Merged
merged 21 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion samples/XCT.Sample/Pages/AboutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
<pages:BasePage.BindingContext>
<viewmodels:AboutViewModel />
</pages:BasePage.BindingContext>


<ContentPage.Behaviors>
<xct:EventToCommandBehavior EventName="Appearing" Command="{Binding PageAppearingCommand}"/>
</ContentPage.Behaviors>

<pages:BasePage.Content>
<StackLayout Padding="{StaticResource ContentPadding}">
<Label Text="Thank you Xamarin Community Toolkit contributors"
Expand Down
11 changes: 0 additions & 11 deletions samples/XCT.Sample/Pages/AboutPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Linq;
using Octokit;
using Xamarin.CommunityToolkit.Sample.ViewModels;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.Pages
{
Expand All @@ -12,12 +7,6 @@ public partial class AboutPage : BasePage
public AboutPage()
=> InitializeComponent();

protected override async void OnAppearing()
{
base.OnAppearing();
await ((AboutViewModel)BindingContext).OnAppearing();
}

async void OnCloseClicked(object sender, EventArgs e)
=> await Navigation.PopModalAsync();
}
Expand Down
6 changes: 3 additions & 3 deletions samples/XCT.Sample/Pages/Base/BasePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Xamarin.CommunityToolkit.Sample.Pages
{
public class BasePage : ContentPage
{
ICommand navigateCommand;
public BasePage() =>
NavigateCommand = CommandFactory.Create<SectionModel>(sectionModel => Navigation.PushAsync(PreparePage(sectionModel)));

public Color DetailColor { get; set; }

public ICommand NavigateCommand => navigateCommand ??= new AsyncCommand<SectionModel>(sectionModel
=> Navigation.PushAsync(PreparePage(sectionModel)));
public ICommand NavigateCommand { get; }

Page PreparePage(SectionModel model)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
</ResourceDictionary>
</ContentPage.Resources>

<ContentPage.Behaviors>
<xct:EventToCommandBehavior EventName="Appearing" Command="{Binding PageAppearingCommand}"/>
</ContentPage.Behaviors>

<pages:BasePage.Content>

<StackLayout Orientation="Vertical" VerticalOptions="Center">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
using Xamarin.CommunityToolkit.Sample.ViewModels.Converters;

namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
namespace Xamarin.CommunityToolkit.Sample.Pages.Converters
{
public partial class ByteArrayToImageSourcePage : BasePage
{
public ByteArrayToImageSourcePage()
=> InitializeComponent();

protected override async void OnAppearing()
{
base.OnAppearing();
await ((ByteArrayToImageSourceViewModel)BindingContext).OnAppearing();
}
}
}
8 changes: 4 additions & 4 deletions samples/XCT.Sample/Pages/Effects/TouchEffectPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.Forms.Xaml;

namespace Xamarin.CommunityToolkit.Sample.Pages.Effects
Expand All @@ -9,18 +9,18 @@ public partial class TouchEffectPage
{
public TouchEffectPage()
{
Command = new Command(() =>
Command = CommandFactory.Create(() =>
{
TouchCount++;
OnPropertyChanged(nameof(TouchCount));
});
LongPressCommand = new Command(() =>
LongPressCommand = CommandFactory.Create(() =>
{
LongPressCount++;
OnPropertyChanged(nameof(LongPressCount));
});
InitializeComponent();

InitializeComponent();
}

public ICommand Command { get; }
Expand Down
11 changes: 6 additions & 5 deletions samples/XCT.Sample/Pages/TestCases/TouchEffectButtonPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.Pages.TestCases
{
public partial class TouchEffectButtonPage
{
ICommand command;

public TouchEffectButtonPage()
=> InitializeComponent();
{
Command = CommandFactory.Create(() => DisplayAlert("Command executed ", null, "OK"));
InitializeComponent();
}

public ICommand Command => command ??= new Command(() => this.DisplayAlert("Command executed ", null, "OK"));
public ICommand Command { get; }
}
}
22 changes: 14 additions & 8 deletions samples/XCT.Sample/ViewModels/AboutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ public class AboutViewModel : BaseViewModel

string emptyViewText = "Loading data...";

ICommand selectedContributorCommand;
public AboutViewModel()
{
PageAppearingCommand = CommandFactory.Create(OnAppearing);
SelectedContributorCommand = CommandFactory.Create(async () =>
{
if (SelectedContributor == null)
return;

await Launcher.OpenAsync(SelectedContributor.HtmlUrl);
SelectedContributor = null;
});
}

public RepositoryContributor[] Contributors
{
Expand All @@ -38,14 +49,9 @@ public string EmptyViewText
set => SetProperty(ref emptyViewText, value);
}

public ICommand SelectedContributorCommand => selectedContributorCommand ??= new AsyncCommand(async () =>
{
if (SelectedContributor is null)
return;
public ICommand PageAppearingCommand { get; }

await Launcher.OpenAsync(SelectedContributor.HtmlUrl);
SelectedContributor = null;
});
public ICommand SelectedContributorCommand { get; }

public async Task OnAppearing()
{
Expand Down
11 changes: 6 additions & 5 deletions samples/XCT.Sample/ViewModels/Base/BaseGalleryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
using System.Windows.Input;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.CommunityToolkit.Sample.Models;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.ViewModels
{
public abstract class BaseGalleryViewModel : BaseViewModel
{
ICommand filterCommand;

public BaseGalleryViewModel() => Filter();
public BaseGalleryViewModel()
{
Filter();
FilterCommand = CommandFactory.Create(Filter);
}

public abstract IEnumerable<SectionModel> Items { get; }

public IEnumerable<SectionModel> FilteredItems { get; private set; }

public ICommand FilterCommand => filterCommand ??= new Command(Filter);
public ICommand FilterCommand { get; }

public string FilterValue { private get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Behaviors
{
public class EventToCommandBehaviorViewModel : BaseViewModel
{
ICommand incrementCommand;

int clickCount;

public ICommand IncrementCommand => incrementCommand ??= new Command(() => ClickCount++);
public EventToCommandBehaviorViewModel() =>
IncrementCommand = CommandFactory.Create(() => ClickCount++);

public int ClickCount
{
get => clickCount;
set => SetProperty(ref clickCount, value);
}

public ICommand IncrementCommand { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Behaviors
{
Expand All @@ -17,7 +17,7 @@ public string CommandExecutions
public ICommand MaxLengthReachedCommand { get; }

public MaxLengthReachedBehaviorViewModel()
=> MaxLengthReachedCommand = new Command<string>(OnCommandExecuted);
=> MaxLengthReachedCommand = CommandFactory.Create<string>(OnCommandExecuted);

void OnCommandExecuted(string text)
=> CommandExecutions += string.Format("MaxLength reached with value: '{0}'.", text) + Environment.NewLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Behaviors
{
Expand All @@ -21,7 +21,7 @@ public string PerformedSearches
#endregion Properties

public UserStoppedTypingBehaviorViewModel()
=> SearchCommand = new Command<string>(PerformSearch);
=> SearchCommand = CommandFactory.Create<string>(PerformSearch);

void PerformSearch(string searchTerms)
=> PerformedSearches += string.Format("Performed search for '{0}'.", searchTerms) + Environment.NewLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Input;
using Octokit;
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Converters
{
Expand All @@ -25,6 +27,11 @@ public bool IsBusy
set => SetProperty(ref isBusy, value);
}

public ICommand PageAppearingCommand { get; }

public ByteArrayToImageSourceViewModel() =>
PageAppearingCommand = CommandFactory.Create(OnAppearing);

public async Task OnAppearing()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ItemSelectedEventArgsViewModel
new Person() { Id = 3, Name = "Person 3" }
};

public ICommand ItemSelectedCommand { get; private set; } = new AsyncCommand<Person>(person
=> Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancelf"));
public ICommand ItemSelectedCommand { get; } =
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancelf"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class ItemTappedEventArgsViewModel
new Person() { Id = 3, Name = "Person 3" }
};

public ICommand ItemTappedCommand { get; private set; } = new AsyncCommand<Person>(person
=> Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancel"));
public ICommand ItemTappedCommand { get; } =
CommandFactory.Create<Person>(person => Application.Current.MainPage.DisplayAlert("Item Tapped: ", person.Name, "Cancel"));
}

public class Person
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.CommunityToolkit.Helpers;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.Sample.ViewModels.Converters
Expand All @@ -9,15 +10,15 @@ public class ListIsNullOrEmptyViewModel : BaseViewModel

public ListIsNullOrEmptyViewModel()
{
AddItemCommand = new Command(() =>
AddItemCommand = CommandFactory.Create(() =>
{
Items.Add(new Person
{
Id = Items.Count,
Name = $"Person {Items.Count}"
});
});
RemoveItemCommand = new Command(() => Items.RemoveAt(0));
RemoveItemCommand = CommandFactory.Create(() => Items.RemoveAt(0));

// ListIsNullOrEmptyConvertor needs to know that Items are updated
Items.CollectionChanged += (sender, e) => OnPropertyChanged(nameof(Items));
Expand Down
15 changes: 9 additions & 6 deletions samples/XCT.Sample/ViewModels/Markup/SearchViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace Xamarin.CommunityToolkit.Sample.ViewModels.Markup
{
public class SearchViewModel : BaseViewModel
{
ICommand backCommand, likeCommand, openTwitterSearchCommand, openHelpCommand;

public string SearchText { get; set; }

public List<Tweet> SearchResults { get; set; }
Expand Down Expand Up @@ -54,15 +52,20 @@ public SearchViewModel()
},
}
};

BackCommand = new RelayCommand(Back);
LikeCommand = new RelayCommand<Tweet>(Like);
OpenTwitterSearchCommand = new RelayCommandAsync(OpenTwitterSearch);
OpenHelpCommand = new RelayCommandAsync(OpenHelp);
}

public ICommand BackCommand => backCommand ??= new RelayCommand(Back);
public ICommand BackCommand { get; }

public ICommand LikeCommand => likeCommand ??= new RelayCommand<Tweet>(Like);
public ICommand LikeCommand { get; }

public ICommand OpenTwitterSearchCommand => openTwitterSearchCommand ??= new RelayCommandAsync(OpenTwitterSearch);
public ICommand OpenTwitterSearchCommand { get; }

public ICommand OpenHelpCommand => openHelpCommand ??= new RelayCommandAsync(OpenHelp);
public ICommand OpenHelpCommand { get; }

void Back() { }

Expand Down
Loading