Skip to content

Commit

Permalink
Merge pull request #251 from MartinZikmund/feature/blog-viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund authored Mar 13, 2024
2 parents 01a573a + d206659 commit 23d9789
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 43 deletions.
20 changes: 10 additions & 10 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,19 @@
<PackageVersion Include="Uno.Extensions.Serialization.Http" Version="4.0.0" />
<PackageVersion Include="Uno.Extensions.Serialization.Refit" Version="4.0.0" />
<PackageVersion Include="Uno.Resizetizer" Version="1.3.0" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="5.1.75" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="5.1.80" />
<PackageVersion Include="Uno.UniversalImageLoader" Version="1.9.36" />
<PackageVersion Include="Uno.Wasm.Bootstrap" Version="8.0.9" />
<PackageVersion Include="Uno.Wasm.Bootstrap.DevServer" Version="8.0.9" />
<PackageVersion Include="Uno.Wasm.Bootstrap.Server" Version="8.0.4" />
<PackageVersion Include="Uno.WinUI" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI.MSAL" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI.Lottie" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI.DevServer" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI.Skia.Gtk" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI.Skia.Linux.FrameBuffer" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI.Skia.Wpf" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="5.2.0-dev.768" />
<PackageVersion Include="Uno.WinUI" Version="5.2.0-dev.1159" />
<PackageVersion Include="Uno.WinUI.MSAL" Version="5.2.0-dev.1159" />
<PackageVersion Include="Uno.WinUI.Lottie" Version="5.2.0-dev.1159" />
<PackageVersion Include="Uno.WinUI.DevServer" Version="5.2.0-dev.1159" />
<PackageVersion Include="Uno.WinUI.Skia.Gtk" Version="5.2.0-dev.1159" />
<PackageVersion Include="Uno.WinUI.Skia.Linux.FrameBuffer" Version="5.2.0-dev.1159" />
<PackageVersion Include="Uno.WinUI.Skia.Wpf" Version="5.2.0-dev.1159" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="5.2.0-dev.1159" />
<PackageVersion Include="Xamarin.Google.Android.Material" Version="1.10.0.3" />
</ItemGroup>
</Project>
</Project>
3 changes: 2 additions & 1 deletion src/app/MZikmund/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
);
MainWindow = builder.Window;

#if DEBUG
#if DEBUG && HAS_UNO
MainWindow.EnableHotReload();
#endif

Expand Down Expand Up @@ -118,6 +118,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)

private static void ConfigureServices(IServiceCollection services)
{
services.AddScoped<BlogViewModel>();
services.AddScoped<SettingsViewModel>();
services.AddScoped<TagsManagerViewModel>();
services.AddScoped<CategoriesManagerViewModel>();
Expand Down
12 changes: 0 additions & 12 deletions src/app/MZikmund/MZikmund.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,6 @@
<UpToDateCheckInput Include="**\*.xaml" Exclude="bin\**\*.xaml;obj\**\*.xaml" />
</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" />
<UpToDateCheckInput Remove="Views\Admin\TagsManagerView.xaml" />
</ItemGroup>

<ItemGroup>
<Content Include="..\..\..\assets\fonts\generated\fonts\icomoon.ttf" Link="Assets\Fonts\icomoon.ttf" />
<Content Include="..\..\..\assets\fonts\CascadiaCode.ttf" Link="Assets\Fonts\CascadiaCode.ttf" />
Expand Down
4 changes: 2 additions & 2 deletions src/app/MZikmund/ViewModels/Admin/PostEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ private async void PreviewTimerOnTick(DispatcherQueueTimer sender, object args)
}
}

public override async void ViewNavigatedTo(object parameter)
public override async void ViewNavigatedTo(object? parameter)
{
using var _ = _loadingIndicator.BeginLoading();
var postId = (Guid)parameter;
var postId = (Guid)parameter!;
if (postId == Guid.Empty)
{
Post = new Post();
Expand Down
35 changes: 35 additions & 0 deletions src/app/MZikmund/ViewModels/BlogViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.ObjectModel;
using MZikmund.Api.Client;
using MZikmund.DataContracts.Blog;
using MZikmund.Services.Loading;
using MZikmund.Services.Localization;

namespace MZikmund.ViewModels;

public class BlogViewModel : PageViewModel
{
private readonly IMZikmundApi _api;
private readonly ILoadingIndicator _loadingIndicator;

public BlogViewModel(IMZikmundApi api, ILoadingIndicator loadingIndicator)
{
_api = api ?? throw new ArgumentNullException(nameof(api));
_loadingIndicator = loadingIndicator;
}

public override string Title => Localizer.Instance.GetString("Blog");

public ObservableCollection<PostListItem> Posts { get; } = new ObservableCollection<PostListItem>();

public override async void ViewNavigatedTo(object? parameter)
{
base.ViewNavigatedTo(parameter);
using var _ = _loadingIndicator.BeginLoading();
Posts.Clear();
var posts = await _api.GetPostsAsync();
foreach (var post in posts.Content!.Data)
{
Posts.Add(post);
}
}
}
2 changes: 1 addition & 1 deletion src/app/MZikmund/ViewModels/PageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public virtual void ViewLoaded() { }

public virtual void ViewUnloaded() { }

public virtual void ViewNavigatedTo(object parameter) { }
public virtual void ViewNavigatedTo(object? parameter) { }
}
11 changes: 11 additions & 0 deletions src/app/MZikmund/ViewModels/PostViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MZikmund.ViewModels;

public class PostViewModel : PageViewModel
{
public PostViewModel()
{

}

public string HtmlPreview { get; private set; } = "";
}
4 changes: 3 additions & 1 deletion src/app/MZikmund/ViewModels/WindowShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MZikmund.ViewModels;

public class WindowShellViewModel : PageViewModel
public class WindowShellViewModel : ViewModelBase
{
private readonly DispatcherQueue _dispatcher;
private RefCountDisposable? _refCountDisposable;
Expand All @@ -14,6 +14,8 @@ public WindowShellViewModel(DispatcherQueue dispatcher)
_dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
}

public string Title { get; set; } = "Martin Zikmund";

public IDisposable BeginLoading()
{
LoadingStatusMessage = "";
Expand Down
29 changes: 29 additions & 0 deletions src/app/MZikmund/Views/BlogView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<local:BlogViewBase
x:Class="MZikmund.Views.BlogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MZikmund.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="using:MZikmund.DataContracts.Blog"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<ListView ItemsSource="{x:Bind ViewModel.Posts}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="models:PostListItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{x:Bind Title}" />
<TextBlock Text="{x:Bind Abstract}" Grid.Row="1" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</local:BlogViewBase>
15 changes: 15 additions & 0 deletions src/app/MZikmund/Views/BlogView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MZikmund.ViewModels;

namespace MZikmund.Views;

public sealed partial class BlogView : BlogViewBase
{
public BlogView()
{
this.InitializeComponent();
}
}

public partial class BlogViewBase : PageBase<BlogViewModel>
{
}
4 changes: 3 additions & 1 deletion src/app/MZikmund/Views/PageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public abstract partial class PageBase<TViewModel> : Page
where TViewModel : PageViewModel
{
private object? _pendingParameter;
private bool _isNavigationDelayed;

protected PageBase()
{
Expand All @@ -24,7 +25,7 @@ private void PageLoading(object sender, object args)
{
EnsureViewModel();

if (_pendingParameter is not null)
if (_isNavigationDelayed)
{
ViewModel?.ViewNavigatedTo(_pendingParameter);
_pendingParameter = null;
Expand All @@ -48,6 +49,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
}
else
{
_isNavigationDelayed = true;
_pendingParameter = e.Parameter;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/app/MZikmund/Views/PostView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<local:PostViewBase
x:Class="MZikmund.Views.PostView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MZikmund.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<ContentControl x:Name="PreviewWebViewContainer" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" />
</Grid>
</local:PostViewBase>
73 changes: 73 additions & 0 deletions src/app/MZikmund/Views/PostView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using MZikmund.ViewModels;
using Windows.Foundation;
using Windows.Foundation.Collections;

namespace MZikmund.Views;

public sealed partial class PostView : PostViewBase
{
private readonly WebView2 _previewWebView;

private string _postPreviewTemplate;

public PostView()
{
InitializeComponent();
// Read the template from the embedded resource
_postPreviewTemplate = typeof(PostView).GetAssembly().GetManifestResourceStream("MZikmund.Assets.PostPreviewTemplate.html")?.ReadToEnd()!;
PreviewWebViewContainer.Content = _previewWebView = new WebView2();
this.Loaded += PostEditorView_Loaded;
this.Unloaded += PostEditorView_Unloaded;
}

private void PostEditorView_Unloaded(object sender, RoutedEventArgs e)
{
ViewModel!.PropertyChanged -= ViewModel_PropertyChanged;
}

private async void PostEditorView_Loaded(object sender, RoutedEventArgs e)
{
try
{
_previewWebView.CoreWebView2Initialized += OnWebViewInitialized;
await _previewWebView.EnsureCoreWebView2Async();
ViewModel!.PropertyChanged += ViewModel_PropertyChanged;
}
catch (Exception)
{
// TODO: Log error
}
}

private void OnWebViewInitialized(WebView2 sender, CoreWebView2InitializedEventArgs args) => UpdatePreview();

private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ViewModel.HtmlPreview))
{
UpdatePreview();
}
}

private void UpdatePreview()
{
_previewWebView.NavigateToString(string.Format(CultureInfo.InvariantCulture, _postPreviewTemplate, ViewModel!.HtmlPreview ?? ""));
}
}

public partial class PostViewBase : PageBase<PostViewModel>
{
}
2 changes: 1 addition & 1 deletion src/app/MZikmund/WindowShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<BitmapIcon ShowAsMonochrome="False" Width="16" UriSource="ms-appx:///MZikmund/Assets/NavigationViewLogo.png" />
<TextBlock Grid.Column="1" VerticalAlignment="Center" Text="Martin Zikmund" Style="{ThemeResource CaptionTextBlockStyle}" />
<TextBlock Grid.Column="1" VerticalAlignment="Center" Text="{x:Bind ViewModel.Title}" Style="{ThemeResource CaptionTextBlockStyle}" />
</Grid>
<mux:NavigationView
x:Name="AppNavigation"
Expand Down
2 changes: 1 addition & 1 deletion src/app/MZikmund/WindowShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void MenuItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sender, M
}
else if (args.InvokedItemContainer == BlogNavigationViewItem)
{

navigationService.Navigate<BlogViewModel>();
}
else if (args.InvokedItemContainer == ContactNavigationViewItem)
{
Expand Down
5 changes: 0 additions & 5 deletions src/web/MZikmund.Web.Core/Blog/ListPostsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ public async Task<PagedResponse<PostListItem>> Handle(ListPostsQuery request, Ca
Categories = _mapper.Map<Category[]>(post.Categories),
});

foreach (var post in posts)
{
post.Abstract = _markdownConverter.ToHtml(post.Abstract);
}

return new(posts, request.Page, request.PageSize, postCount);
}
}
7 changes: 7 additions & 0 deletions src/web/MZikmund.Web.Core/Services/MetaWeblogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ public class MetaWeblogProvider : IMetaWeblogProvider
private readonly IBlobStorage _blobStorage;
private readonly ILogger<MetaWeblogProvider> _logger;
private readonly IMediator _mediator;
private readonly IPostContentProcessor _postContentProcessor;

public MetaWeblogProvider(
ISiteConfiguration blogConfig,
IMediaBlobPathGenerator mediaBlobPathGenerator,
IBlobStorage blobStorage,
IMediator mediator,
IPostContentProcessor postContentProcessor,
ILogger<MetaWeblogProvider> logger)
{
_siteConfiguration = blogConfig;
_mediaBlobPathGenerator = mediaBlobPathGenerator;
_blobStorage = blobStorage;
_logger = logger;
_mediator = mediator;
_postContentProcessor = postContentProcessor;
}

public Task<int> AddCategoryAsync(string key, string username, string password, NewCategory newCategory) => TryExecuteAsync(async () =>
Expand Down Expand Up @@ -186,6 +189,10 @@ public Task<WeblogPost[]> GetRecentPostsAsync(string blogid, string username, st
ValidateUser(username, password);

var posts = await _mediator.Send(new ListPostsQuery(1, numberOfPosts));
foreach (var post in posts.Data)
{
post.Abstract = await _postContentProcessor.ProcessAsync(post.Abstract);
}
return posts.Data.Select(p => ToWeblogPost(p)).Cast<WeblogPost>().ToArray();
});

Expand Down
Loading

0 comments on commit 23d9789

Please sign in to comment.