From 9e4c1987b9c45e8c3182abf1a730acccf81d00de Mon Sep 17 00:00:00 2001 From: Thomas Labrecque Date: Wed, 15 May 2024 14:12:04 -0400 Subject: [PATCH] fix: Workaround for Posts page --- CHANGELOG.md | 1 + .../ViewModels/Posts/PostItemViewModel.cs | 41 +++++++++++++++++++ .../ViewModels/Posts/PostsPageViewModel.cs | 40 ++++++------------ .../Content/Posts/PostsPage.xaml | 23 ++++------- .../Posts/EditPostPageViewModelShould.cs | 17 +++----- 5 files changed, 68 insertions(+), 54 deletions(-) create mode 100644 src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostItemViewModel.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ed3f6434..444792b6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Prefix your items with `(Template)` if the change is about the template and not - Added a kill switch feature to the app. - Bump Uno.WinUI, Uno.WinUI.DevServer, Uno.WinUI.Lottie and Uno.UI.Adapter.Microsoft.Extensions.Logging to 5.0.159 to fix backNavigation/CloseModal crash. - Fixed an issue with logging configuration not creating the directory before writing the log file in the case logging was disabled by default. +- Fixed Post commands binding issue on Android. ## 3.3.X - Added a forced update feature to the app. diff --git a/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostItemViewModel.cs b/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostItemViewModel.cs new file mode 100644 index 000000000..e2729caea --- /dev/null +++ b/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostItemViewModel.cs @@ -0,0 +1,41 @@ +using ApplicationTemplate.Business; +using Chinook.DynamicMvvm; +using Chinook.StackNavigation; + +namespace ApplicationTemplate.Presentation; + +/// +/// Post item view model. +/// +public sealed class PostItemViewModel : ViewModel +{ + /// + /// Initializes a new instance of the class. + /// + /// The post. + public PostItemViewModel(Post post) + { + Post = post; + } + + /// + /// The post. + /// + public Post Post { get; } + + /// + /// Deletes the post. + /// + public IDynamicCommand DeletePost => this.GetCommandFromTask(async (ct) => + { + await this.GetService().Delete(ct, Post.Id); + }); + + /// + /// Navigates to the edit post page. + /// + public IDynamicCommand EditPost => this.GetCommandFromTask(async (ct) => + { + await this.GetService().Navigate(ct, () => new EditPostPageViewModel(Post)); + }); +} diff --git a/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostsPageViewModel.cs b/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostsPageViewModel.cs index e6e2fe2c6..473a5ed4c 100644 --- a/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostsPageViewModel.cs +++ b/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostsPageViewModel.cs @@ -1,5 +1,7 @@ -using System; -using System.Collections.Immutable; +using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.Linq; using System.Threading; using System.Threading.Tasks; using ApplicationTemplate.Business; @@ -9,42 +11,24 @@ namespace ApplicationTemplate.Presentation; -public partial class PostsPageViewModel : ViewModel +public sealed partial class PostsPageViewModel : ViewModel { - private readonly Func _onGetPostsCalled; - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "It will be disposed by the DataLoader when passed via WithTrigger.")] + [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "It will be disposed by the DataLoader when passed via WithTrigger.")] private readonly ManualDataLoaderTrigger _deletePostTrigger = new(); - public PostsPageViewModel(Func onGetPostsCalled = null) - { - _onGetPostsCalled = onGetPostsCalled; - } - - public IDynamicCommand DeletePost => this.GetCommandFromTask(async (ct, post) => - { - await this.GetService().Delete(ct, post.Id); - }); - - public IDynamicCommand NavigateToNewPost => this.GetCommandFromTask(async ct => + public IDynamicCommand CreatePost => this.GetCommandFromTask(async ct => { await this.GetService().Navigate(ct, () => new EditPostPageViewModel()); }); - public IDynamicCommand NavigateToPost => this.GetCommandFromTask(async (ct, post) => - { - await this.GetService().Navigate(ct, () => new EditPostPageViewModel(post)); - }); - public IDataLoader Posts => this.GetDataLoader(GetPosts, d => d.WithTrigger(_deletePostTrigger)); - private async Task> GetPosts(CancellationToken ct) + private async Task> GetPosts(CancellationToken ct) { - if (_onGetPostsCalled != null) - { - await _onGetPostsCalled(); - } + var posts = await this.GetService().GetPosts(ct); - return await this.GetService().GetPosts(ct); + return posts + .Select(p => this.GetChild(() => new PostItemViewModel(p), p.Id.ToString(CultureInfo.InvariantCulture))) + .ToImmutableList(); } } diff --git a/src/app/ApplicationTemplate.Shared.Views/Content/Posts/PostsPage.xaml b/src/app/ApplicationTemplate.Shared.Views/Content/Posts/PostsPage.xaml index 7af3d814e..fd7a71f34 100644 --- a/src/app/ApplicationTemplate.Shared.Views/Content/Posts/PostsPage.xaml +++ b/src/app/ApplicationTemplate.Shared.Views/Content/Posts/PostsPage.xaml @@ -4,8 +4,7 @@ xmlns:u="using:Nventive.View.Controls" xmlns:dl="using:Chinook.DataLoader"> - + @@ -17,15 +16,13 @@ x:Uid="Posts_CommandBar" /> - - + - @@ -33,17 +30,16 @@ - - -