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 9e4c198
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using ApplicationTemplate.Business;
using Chinook.DynamicMvvm;
using Chinook.StackNavigation;

namespace ApplicationTemplate.Presentation;

/// <summary>
/// Post item view model.
/// </summary>
public sealed class PostItemViewModel : ViewModel
{
/// <summary>
/// Initializes a new instance of the <see cref="PostItemViewModel"/> class.
/// </summary>
/// <param name="post">The post.</param>
public PostItemViewModel(Post post)
{
Post = post;
}

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

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

/// <summary>
/// Navigates to the edit post page.
/// </summary>
public IDynamicCommand EditPost => this.GetCommandFromTask(async (ct) =>
{
await this.GetService<IStackNavigator>().Navigate(ct, () => new EditPostPageViewModel(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(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,33 +16,30 @@
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">

<!-- Title -->
<TextBlock Text="{Binding Title}"
<TextBlock Text="{Binding Post.Title}"
Style="{StaticResource TitleMedium}" />

<!-- Body -->
<TextBlock Text="{Binding Body}"
<TextBlock Text="{Binding Post.Body}"
Foreground="{ThemeResource OnSurfaceBrush}"
Style="{StaticResource BodyMedium}"
Opacity="{StaticResource MediumOpacity}"
Expand All @@ -55,14 +51,12 @@

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

<!-- Edit Button -->
<Button Content="Edit"
Command="{Binding ElementName=PostPage, Path=DataContext.NavigateToPost}"
CommandParameter="{Binding}"
Command="{Binding EditPost}"
Margin="4,0,0,0" />
</StackPanel>
</StackPanel>
Expand All @@ -74,8 +68,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
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApplicationTemplate.Business;
using ApplicationTemplate.Presentation;
using Chinook.DynamicMvvm;
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit;
using Xunit.Abstractions;

namespace ApplicationTemplate.Tests;
Expand All @@ -27,10 +20,12 @@ public async Task ShowPostDetails()
await Menu.ShowPostsSection.Execute();

var postsVM = GetAndAssertActiveViewModel<PostsPageViewModel>();
var posts = await postsVM.Posts.Load(CancellationToken.None) as ImmutableList<Post>;
var postItemViewModels = await postsVM.Posts.Load(CancellationToken.None) as ImmutableList<PostItemViewModel>;

var post = posts.First();
await postsVM.NavigateToPost.Execute(post);
var postItemViewModel = postItemViewModels.First();
var post = postItemViewModel.Post;

await postItemViewModel.EditPost.Execute();
var sut = GetAndAssertActiveViewModel<EditPostPageViewModel>();

// Assert
Expand Down

0 comments on commit 9e4c198

Please sign in to comment.