-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added initial PopupService implementation * Tweaks to allowing parameters to be passed to the view model behind a popup * Tidy up xml docs * Some unit tests * Only create a view model instance if the BindingContext hasn't been set * Remove the reliance on IQueryAttributable in favour of our own interface * A better way to find the current Page * Readonly dictionary and some safety checking around expected BindingContext types. * A different attempt at passing parameters without an explicit interface * Update src/CommunityToolkit.Maui/PopupService.cs Co-authored-by: Pedro Jesus <[email protected]> * Now is a time for test * Remove unnecessary changes * Sample to perform a long running process * Provide ability to close popup from within popup view model * Prevent unnecessary instance being created * Refactor `CurrentPage`, Add Default Constructor, Refactor `ValidateBindingContext` * Update Unit Tests --------- Co-authored-by: Pedro Jesus <[email protected]> Co-authored-by: Brandon Minnick <[email protected]>
- Loading branch information
1 parent
9043e35
commit 2ec4be1
Showing
15 changed files
with
682 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 25 additions & 2 deletions
27
samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/MultiplePopupViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,28 @@ | ||
namespace CommunityToolkit.Maui.Sample.ViewModels.Views; | ||
using CommunityToolkit.Maui.Core; | ||
using CommunityToolkit.Mvvm.Input; | ||
|
||
public class MultiplePopupViewModel : BaseViewModel | ||
namespace CommunityToolkit.Maui.Sample.ViewModels.Views; | ||
|
||
public partial class MultiplePopupViewModel : BaseViewModel | ||
{ | ||
readonly IPopupService popupService; | ||
|
||
public MultiplePopupViewModel(IPopupService popupService) | ||
{ | ||
this.popupService = popupService; | ||
} | ||
|
||
[RelayCommand] | ||
Task OnCsharpBindingPopup() | ||
{ | ||
return popupService.ShowPopupAsync<CsharpBindingPopupViewModel>( | ||
onPresenting: viewModel => viewModel.Load("This is a platform specific popup with a .NET MAUI View being rendered. The behaviors of the popup will confirm to 100% this platform look and feel, but still allows you to use your .NET MAUI Controls.")); | ||
} | ||
|
||
[RelayCommand] | ||
Task OnUpdatingPopup() | ||
{ | ||
return popupService.ShowPopupAsync<UpdatingPopupViewModel>( | ||
onPresenting: viewModel => viewModel.PerformUpdates(10)); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/UpdatingPopupViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
|
||
namespace CommunityToolkit.Maui.Sample.ViewModels.Views; | ||
|
||
public partial class UpdatingPopupViewModel : BaseViewModel | ||
{ | ||
const double finalUpdateProgressValue = 1; | ||
|
||
readonly WeakEventManager finishedEventManager = new(); | ||
|
||
[ObservableProperty] | ||
string message = ""; | ||
|
||
[ObservableProperty] | ||
[NotifyCanExecuteChangedFor(nameof(FinishCommand))] | ||
double updateProgress; | ||
|
||
public event EventHandler<EventArgs> Finished | ||
{ | ||
add => finishedEventManager.AddEventHandler(value); | ||
remove => finishedEventManager.RemoveEventHandler(value); | ||
} | ||
|
||
internal async void PerformUpdates(int numberOfUpdates) | ||
{ | ||
double updateTotalForPercentage = numberOfUpdates + 1; | ||
|
||
for (var update = 1; update <= numberOfUpdates; update++) | ||
{ | ||
Message = $"Updating {update} of {numberOfUpdates}"; | ||
|
||
UpdateProgress = update / updateTotalForPercentage; | ||
|
||
await Task.Delay(TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
UpdateProgress = finalUpdateProgressValue; | ||
Message = "Updates complete"; | ||
} | ||
|
||
[RelayCommand(CanExecute = nameof(CanFinish))] | ||
void OnFinish() | ||
{ | ||
finishedEventManager.HandleEvent(this, EventArgs.Empty, nameof(Finished)); | ||
} | ||
|
||
bool CanFinish() => UpdateProgress is finalUpdateProgressValue; | ||
} |
55 changes: 55 additions & 0 deletions
55
samples/CommunityToolkit.Maui.Sample/Views/Popups/UpdatingPopup.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<toolkit:Popup | ||
x:Class="CommunityToolkit.Maui.Sample.Views.Popups.UpdatingPopup" | ||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" | ||
CanBeDismissedByTappingOutsideOfPopup="False"> | ||
|
||
<VerticalStackLayout Style="{StaticResource PopupLayout}" Spacing="12" > | ||
<VerticalStackLayout.Resources> | ||
<ResourceDictionary> | ||
<Style x:Key="Title" TargetType="Label"> | ||
<Setter Property="FontSize" Value="26" /> | ||
<Setter Property="FontAttributes" Value="Bold" /> | ||
<Setter Property="TextColor" Value="#000" /> | ||
<Setter Property="VerticalTextAlignment" Value="Center" /> | ||
<Setter Property="HorizontalTextAlignment" Value="Center" /> | ||
</Style> | ||
<Style x:Key="Divider" TargetType="BoxView"> | ||
<Setter Property="HeightRequest" Value="1" /> | ||
<Setter Property="Margin" Value="50, 25" /> | ||
<Setter Property="Color" Value="#c3c3c3" /> | ||
</Style> | ||
<Style x:Key="Content" TargetType="Label"> | ||
<Setter Property="HorizontalTextAlignment" Value="Center" /> | ||
<Setter Property="VerticalTextAlignment" Value="Center" /> | ||
</Style> | ||
<Style x:Key="PopupLayout" TargetType="StackLayout"> | ||
<Setter Property="Padding" Value="{OnPlatform Android=20, WinUI=20, iOS=12, MacCatalyst=5, Tizen=20}" /> | ||
</Style> | ||
<Style x:Key="ConfirmButton" TargetType="Button"> | ||
<Setter Property="VerticalOptions" Value="EndAndExpand" /> | ||
</Style> | ||
|
||
<x:Double x:Key="ComparingValue">1.0</x:Double> | ||
</ResourceDictionary> | ||
</VerticalStackLayout.Resources> | ||
|
||
<Label Style="{StaticResource Title}" Text="Updating" /> | ||
|
||
<BoxView Style="{StaticResource Divider}" /> | ||
|
||
<ActivityIndicator | ||
IsRunning="{Binding UpdateProgress, Converter={toolkit:CompareConverter ComparingValue={StaticResource ComparingValue}, ComparisonOperator=Smaller, FalseObject=False, TrueObject=True}}"/> | ||
|
||
<Label Style="{StaticResource Content}" Text="{Binding Message}" /> | ||
|
||
<ProgressBar Progress="{Binding UpdateProgress}" /> | ||
|
||
<Button | ||
Command="{Binding FinishCommand}" | ||
Style="{StaticResource ConfirmButton}" | ||
Text="Finish" /> | ||
</VerticalStackLayout> | ||
</toolkit:Popup> |
20 changes: 20 additions & 0 deletions
20
samples/CommunityToolkit.Maui.Sample/Views/Popups/UpdatingPopup.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using CommunityToolkit.Maui.Sample.ViewModels.Views; | ||
using CommunityToolkit.Maui.Views; | ||
|
||
namespace CommunityToolkit.Maui.Sample.Views.Popups; | ||
|
||
public partial class UpdatingPopup : Popup | ||
{ | ||
public UpdatingPopup(UpdatingPopupViewModel updatingPopupViewModel) | ||
{ | ||
InitializeComponent(); | ||
BindingContext = updatingPopupViewModel; | ||
|
||
updatingPopupViewModel.Finished += OnUpdatingPopupViewModelFinished; | ||
} | ||
|
||
void OnUpdatingPopupViewModelFinished(object? sender, EventArgs e) | ||
{ | ||
this.Close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.ComponentModel; | ||
|
||
namespace CommunityToolkit.Maui.Core; | ||
|
||
/// <summary> | ||
/// Provides a mechanism for displaying <see cref="CommunityToolkit.Maui.Core.IPopup"/>s based on the underlying view model. | ||
/// </summary> | ||
public interface IPopupService | ||
{ | ||
/// <summary> | ||
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam> | ||
void ShowPopup<TViewModel>() where TViewModel : INotifyPropertyChanged; | ||
|
||
/// <summary> | ||
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>. | ||
/// The supplied <paramref name="onPresenting"/> provides a mechanism to invoke any methods on your view model in order to load or pass data to it. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam> | ||
/// <param name="onPresenting">An <see cref="Action{TViewModel}"/> that will be performed before the popup is presented.</param> | ||
void ShowPopup<TViewModel>(Action<TViewModel> onPresenting) where TViewModel : INotifyPropertyChanged; | ||
|
||
/// <summary> | ||
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam> | ||
/// <param name="viewModel">The view model to use as the <c>BindingContext</c> for the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param> | ||
void ShowPopup<TViewModel>(TViewModel viewModel) where TViewModel : INotifyPropertyChanged; | ||
|
||
/// <summary> | ||
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam> | ||
/// <returns>A <see cref="Task"/> that can be awaited to return the result of the <see cref="CommunityToolkit.Maui.Core.IPopup"/> once it has been dismissed.</returns> | ||
Task<object?> ShowPopupAsync<TViewModel>() where TViewModel : INotifyPropertyChanged; | ||
|
||
/// <summary> | ||
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>. | ||
/// The supplied <paramref name="onPresenting"/> provides a mechanism to invoke any methods on your view model in order to load or pass data to it. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam> | ||
/// <returns>A <see cref="Task"/> that can be awaited to return the result of the <see cref="CommunityToolkit.Maui.Core.IPopup"/> once it has been dismissed.</returns> | ||
/// <param name="onPresenting">An <see cref="Action{TViewModel}"/> that will be performed before the popup is presented.</param> | ||
Task<object?> ShowPopupAsync<TViewModel>(Action<TViewModel> onPresenting) where TViewModel : INotifyPropertyChanged; | ||
|
||
/// <summary> | ||
/// Resolves and displays a <see cref="CommunityToolkit.Maui.Core.IPopup"/> and <typeparamref name="TViewModel"/> pair that was registered with <c>AddTransientPopup</c>. | ||
/// </summary> | ||
/// <typeparam name="TViewModel">The type of the view model registered with the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</typeparam> | ||
/// <param name="viewModel">The view model to use as the <c>BindingContext</c> for the <see cref="CommunityToolkit.Maui.Core.IPopup"/>.</param> | ||
/// <returns>A <see cref="Task"/> that can be awaited to return the result of the <see cref="CommunityToolkit.Maui.Core.IPopup"/> once it has been dismissed.</returns> | ||
Task<object?> ShowPopupAsync<TViewModel>(TViewModel viewModel) where TViewModel : INotifyPropertyChanged; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
public class MockPageViewModel : BindableObject | ||
{ | ||
public bool HasLoaded { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.