Skip to content

Commit

Permalink
Merge pull request #211 from MartinZikmund/feature/post-editor-pt2
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund authored Dec 6, 2023
2 parents 55074ae + 05d6dd0 commit b8bf7e1
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/app/MZikmund/Dialogs/Admin/CategoryPickerDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<local:CategoryPickerDialogBase
x:Class="MZikmund.Dialogs.Admin.CategoryPickerDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MZikmund.Dialogs.Admin"
xmlns:xaml="using:MZikmund.Extensions.Xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:blog="using:MZikmund.DataContracts.Blog"
Style="{ThemeResource DefaultContentDialogStyle}"
Title="{xaml:Localize Key=PickCategories}"
CloseButtonText="{xaml:Localize Key=Cancel}"
DefaultButton="Primary"
PrimaryButtonText="{xaml:Localize Key=Save}">
<Grid>
<ListView x:Name="CategoriesListView" Height="240" SelectionMode="Multiple" SelectionChanged="ListView_SelectionChanged" ItemsSource="{x:Bind ViewModel.AllCategories, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="blog:Category">
<TextBlock Text="{x:Bind DisplayName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</local:CategoryPickerDialogBase>
34 changes: 34 additions & 0 deletions src/app/MZikmund/Dialogs/Admin/CategoryPickerDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MZikmund.DataContracts.Blog;
using MZikmund.ViewModels.Admin;

namespace MZikmund.Dialogs.Admin;

public sealed partial class CategoryPickerDialog : CategoryPickerDialogBase
{
public CategoryPickerDialog()
{
InitializeComponent();
}

internal void SetSelectedItems(IEnumerable<Category> itemsToSelect)
{
foreach (var category in itemsToSelect)
{
CategoriesListView.SelectedItems.Add(category);
}
}

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ViewModel is null)
{
return;
}

ViewModel.SelectedCategories = CategoriesListView.SelectedItems.Cast<Category>().ToArray();
}
}

public abstract partial class CategoryPickerDialogBase : DialogBase<CategoryPickerDialogViewModel>
{
}
4 changes: 4 additions & 0 deletions src/app/MZikmund/Dialogs/DialogBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ protected DialogBase()
PrimaryButtonClick += OnPrimaryButtonClick;
SecondaryButtonClick += OnSecondaryButtonClick;
CloseButtonClick += OnCloseButtonClick;
Opened += OnOpened;
}

public TDialogViewModel? ViewModel { get; private set; }

private void OnOpened(ContentDialog sender, ContentDialogOpenedEventArgs args) =>
ViewModel?.OnOpened(this);

private void OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) =>
ViewModel?.OnPrimaryButtonClick(sender, args);

Expand Down
5 changes: 5 additions & 0 deletions src/app/MZikmund/MZikmund.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
</ItemGroup>

<ItemGroup>
<None Remove="Dialogs\Admin\CategoryPickerDialog.xaml" />
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Remove="Dialogs\Admin\CategoryPickerDialog.xaml" />
<UpToDateCheckInput Remove="Views\Admin\CategoriesManagerView.xaml" />
<UpToDateCheckInput Remove="Views\Admin\PostEditorView.xaml" />
<UpToDateCheckInput Remove="Views\Admin\PostsManagerView.xaml" />
Expand Down
35 changes: 35 additions & 0 deletions src/app/MZikmund/ViewModels/Admin/CategoryPickerDialogViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using MZikmund.Api.Client;
using MZikmund.DataContracts.Blog;
using MZikmund.Dialogs.Admin;

namespace MZikmund.ViewModels.Admin;

public class CategoryPickerDialogViewModel : DialogViewModel
{
private readonly Guid[] _initialSelection;
private readonly IMZikmundApi _api;

public CategoryPickerDialogViewModel(Guid[] selectedCategoryIds, IMZikmundApi api)
{
_initialSelection = selectedCategoryIds;
_api = api;
}

public override async void OnOpened(ContentDialog contentDialog)
{
try
{
var dialog = (CategoryPickerDialog)contentDialog;
AllCategories = (await _api.GetCategoriesAsync()).Content!;
dialog.SetSelectedItems(AllCategories.Where(c => _initialSelection.Contains(c.Id)));
}
catch (Exception)
{
// TODO: Handle dialog exception
}
}

public Category[] AllCategories { get; private set; } = Array.Empty<Category>();

public Category[] SelectedCategories { get; set; } = Array.Empty<Category>();
}
17 changes: 16 additions & 1 deletion src/app/MZikmund/ViewModels/Admin/PostEditorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using MZikmund.Api.Client;
using MZikmund.DataContracts.Blog;
using MZikmund.Services.Dialogs;
using MZikmund.Services.Loading;

namespace MZikmund.ViewModels.Admin;

public class PostEditorViewModel : PageViewModel
{
private readonly IMZikmundApi _api;
private readonly IDialogService _dialogService;
private readonly ILoadingIndicator _loadingIndicator;

public PostEditorViewModel(IMZikmundApi api, ILoadingIndicator loadingIndicator)
public PostEditorViewModel(IMZikmundApi api, IDialogService dialogService, ILoadingIndicator loadingIndicator)
{
_api = api;
_dialogService = dialogService;
_loadingIndicator = loadingIndicator;
}

Expand All @@ -27,6 +30,18 @@ public PostEditorViewModel(IMZikmundApi api, ILoadingIndicator loadingIndicator)

public ICommand SaveCommand => GetOrCreateAsyncCommand(SaveAsync);

public ICommand PickCategoriesCommand => GetOrCreateCommand(PickCategories);

private async void PickCategories()
{
var categoyPickerDialogViewModel = new CategoryPickerDialogViewModel(Categories.Select(c => c.Id).ToArray(), _api);
var result = await _dialogService.ShowAsync(categoyPickerDialogViewModel);
if (result == ContentDialogResult.Primary)
{
Categories = categoyPickerDialogViewModel.SelectedCategories.ToArray();
}
}

private async Task SaveAsync()
{
if (Post is null)
Expand Down
4 changes: 4 additions & 0 deletions src/app/MZikmund/ViewModels/DialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ public abstract class DialogViewModel : ViewModelBase
{
public virtual string Title { get; } = "";

public virtual void OnOpened(ContentDialog contentDialog)
{
}

public virtual void OnPrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
Expand Down
8 changes: 5 additions & 3 deletions src/app/MZikmund/Views/Admin/PostEditorView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
<Grid RowDefinitions="Auto, 100, *, Auto" ColumnDefinitions="*, *" RowSpacing="8" ColumnSpacing="8" Padding="20">
<TextBox Header="Title" FontSize="20" Grid.ColumnSpan="2" Text="{x:Bind ViewModel.Post.Title, Mode=TwoWay}" />
<StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="1">
<Button Content="{x:Bind ViewModel.CategoriesText, Mode=OneWay}" HorizontalAlignment="Stretch" />
<TextBox Header="Tags" Grid.Row="1" Text="{x:Bind ViewModel.Tags, Mode=TwoWay}" />
<TextBlock Text="Categories" />
<Button Command="{x:Bind ViewModel.PickCategoriesCommand}" Content="{x:Bind ViewModel.CategoriesText, Mode=OneWay}" HorizontalAlignment="Stretch" Margin="0,0,0,4" />
<TextBlock Text="Tags" />
<TextBox Grid.Row="1" Text="{x:Bind ViewModel.Tags, Mode=TwoWay}" />
</StackPanel>
<TextBox Grid.Row="1" FontFamily="{StaticResource CodeFont}" Header="Abstract" AcceptsReturn="True" TextWrapping="Wrap" Text="{x:Bind ViewModel.Post.Abstract, Mode=TwoWay}" />
<TextBox Grid.Row="2" FontFamily="{StaticResource CodeFont}" Header="Content" AcceptsReturn="True" TextWrapping="Wrap" Text="{x:Bind ViewModel.Post.Content, Mode=TwoWay}" />
<Button Grid.Row="3">
<Button Grid.Row="3" Command="{x:Bind ViewModel.SaveCommand}">
<StackPanel Spacing="8" Orientation="Horizontal">
<SymbolIcon Symbol="Save" />
<TextBlock Text="Save" />
Expand Down

0 comments on commit b8bf7e1

Please sign in to comment.