Skip to content

Commit

Permalink
fix: Workaround for Posts page
Browse files Browse the repository at this point in the history
  • Loading branch information
Soap-141 committed May 15, 2024
1 parent 549b28a commit 6801a91
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using ApplicationTemplate.Business;
using Chinook.DynamicMvvm;
using Chinook.StackNavigation;

namespace ApplicationTemplate.Presentation;

/// <summary>
/// Post item view model.
/// </summary>
/// <remarks>
/// This was created as a workaround for an Uno issue.
/// See https://github.com/unoplatform/uno/issues/13996 for more details.
/// </remarks>
public sealed class PostItemViewModel : ViewModel
{
/// <summary>
/// Initializes a new instance of the <see cref="PostItemViewModel"/> class.
/// </summary>
/// <param name="parent">The parent view model.</param>
/// <param name="post">The post.</param>
public PostItemViewModel(IViewModel parent, Post post)
{
Parent = parent;
Post = post;
}

/// <summary>
/// The parent view model.
/// </summary>
public IViewModel Parent { get; }

/// <summary>
/// The post.
/// </summary>
public Post Post { get; }

/// <summary>
/// Deletes the post.
/// </summary>
public IDynamicCommand DeletePost => this.GetCommandFromTask<PostItemViewModel>(async (ct, item) =>
{
await this.GetService<IPostService>().Delete(ct, item.Post.Id);
});

/// <summary>
/// Navigates to the edit post page.
/// </summary>
public IDynamicCommand EditPost => this.GetCommandFromTask<PostItemViewModel>(async (ct, item) =>
{
await this.GetService<IStackNavigator>().Navigate(ct, () => new EditPostPageViewModel(item.Post));
});
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,42 +11,24 @@

namespace ApplicationTemplate.Presentation;

public partial class PostsPageViewModel : ViewModel
public sealed partial class PostsPageViewModel : ViewModel
{
private readonly Func<Task> _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<Task> onGetPostsCalled = null)
{
_onGetPostsCalled = onGetPostsCalled;
}

public IDynamicCommand DeletePost => this.GetCommandFromTask<Post>(async (ct, post) =>
{
await this.GetService<IPostService>().Delete(ct, post.Id);
});

public IDynamicCommand NavigateToNewPost => this.GetCommandFromTask(async ct =>
public IDynamicCommand CreatePost => this.GetCommandFromTask(async ct =>
{
await this.GetService<IStackNavigator>().Navigate(ct, () => new EditPostPageViewModel());
});

public IDynamicCommand NavigateToPost => this.GetCommandFromTask<Post>(async (ct, post) =>
{
await this.GetService<IStackNavigator>().Navigate(ct, () => new EditPostPageViewModel(post));
});

public IDataLoader Posts => this.GetDataLoader(GetPosts, d => d.WithTrigger(_deletePostTrigger));

private async Task<ImmutableList<Post>> GetPosts(CancellationToken ct)
private async Task<ImmutableList<PostItemViewModel>> GetPosts(CancellationToken ct)
{
if (_onGetPostsCalled != null)
{
await _onGetPostsCalled();
}
var posts = await this.GetService<IPostService>().GetPosts(ct);

return await this.GetService<IPostService>().GetPosts(ct);
return posts
.Select(p => this.GetChild(() => new PostItemViewModel(this, p), p.Id.ToString(CultureInfo.InvariantCulture)))
.ToImmutableList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
xmlns:u="using:Nventive.View.Controls"
xmlns:dl="using:Chinook.DataLoader">

<Grid x:Name="PostPage"
Background="{ThemeResource BackgroundBrush}">
<Grid Background="{ThemeResource BackgroundBrush}">

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -17,23 +16,20 @@
x:Uid="Posts_CommandBar" />

<Grid Grid.Row="1">

<!-- DataLoader View -->
<dl:DataLoaderView x:Name="PostsDataLoaderView"
Source="{Binding Posts}">
<DataTemplate>

<DataTemplate>
<!-- Swipe to Refresh -->
<u:SwipeRefresh x:Name="RefreshContent"
RefreshCommand="{Binding ElementName=PostsDataLoaderView, Path=RefreshCommand}"
<u:SwipeRefresh RefreshCommand="{Binding ElementName=PostsDataLoaderView, Path=RefreshCommand}"
IsRefreshing="{Binding ElementName=PostsDataLoaderView, Path=RefreshCommand.IsExecuting}"
IndicatorColor="{ThemeResource PrimaryBrush}">

<!-- ListView -->
<ListView ItemsSource="{Binding Data}">
<ListView.ItemTemplate>
<DataTemplate>

<StackPanel Background="{ThemeResource SurfaceBrush}"
Margin="0,16,0,0"
Padding="16">
Expand All @@ -55,13 +51,13 @@

<!-- Delete Button -->
<Button Content="Delete"
Command="{Binding ElementName=PostPage, Path=DataContext.DeletePost}"
Command="{Binding DeletePost}"
CommandParameter="{Binding}"
Style="{StaticResource OutlinedButtonStyle}" />

<!-- Edit Button -->
<Button Content="Edit"
Command="{Binding ElementName=PostPage, Path=DataContext.NavigateToPost}"
Command="{Binding EditPost}"
CommandParameter="{Binding}"
Margin="4,0,0,0" />
</StackPanel>
Expand All @@ -74,8 +70,7 @@
</dl:DataLoaderView>

<!-- Create New Post Button -->
<Button Command="{Binding NavigateToNewPost}"
CommandParameter="{Binding}"
<Button Command="{Binding CreatePost}"
Style="{StaticResource IconButtonStyle}"
Background="Transparent"
HorizontalAlignment="Right"
Expand Down

0 comments on commit 6801a91

Please sign in to comment.