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..15847732c --- /dev/null +++ b/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostItemViewModel.cs @@ -0,0 +1,52 @@ +using ApplicationTemplate.Business; +using Chinook.DynamicMvvm; +using Chinook.StackNavigation; + +namespace ApplicationTemplate.Presentation; + +/// +/// Post item view model. +/// +/// +/// This was created as a workaround for an Uno issue. +/// See https://github.com/unoplatform/uno/issues/13996 for more details. +/// +public sealed class PostItemViewModel : ViewModel +{ + /// + /// Initializes a new instance of the class. + /// + /// The parent view model. + /// The post. + public PostItemViewModel(IViewModel parent, Post post) + { + Parent = parent; + Post = post; + } + + /// + /// The parent view model. + /// + public IViewModel Parent { get; } + + /// + /// The post. + /// + public Post Post { get; } + + /// + /// Deletes the post. + /// + public IDynamicCommand DeletePost => this.GetCommandFromTask(async (ct, item) => + { + await this.GetService().Delete(ct, item.Post.Id); + }); + + /// + /// Navigates to the edit post page. + /// + public IDynamicCommand EditPost => this.GetCommandFromTask(async (ct, item) => + { + await this.GetService().Navigate(ct, () => new EditPostPageViewModel(item.Post)); + }); +} diff --git a/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostsPageViewModel.cs b/src/app/ApplicationTemplate.Presentation/ViewModels/Posts/PostsPageViewModel.cs index e6e2fe2c6..1713ca038 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(this, 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..81e2b6c28 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,7 +30,6 @@ - @@ -55,13 +51,13 @@