-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from MartinZikmund/feature/blog-viewer
- Loading branch information
Showing
21 changed files
with
240 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } = ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.