From 571c0f88bec9b6128b38c4a0f675df157f68e308 Mon Sep 17 00:00:00 2001 From: Nickolas Gupton Date: Thu, 9 Mar 2023 07:58:10 -0600 Subject: [PATCH] Add placeholder blog post to news panel (#187) * Add placeholder blog post to news * Fix Codacy issues * Fix compile warning --- .../Assets/transparentdark.png | Bin 0 -> 585 bytes .../ViewModels/BlogPostViewModel.cs | 42 ++++++++++ .../ViewModels/NewsPanelViewModel.cs | 79 +++++++++++++----- UnitystationLauncher/Views/BlogPostView.axaml | 18 ++++ .../Views/BlogPostView.axaml.cs | 17 ++++ UnitystationLauncher/Views/NewsPanelView.xaml | 7 +- 6 files changed, 138 insertions(+), 25 deletions(-) create mode 100644 UnitystationLauncher/Assets/transparentdark.png create mode 100644 UnitystationLauncher/ViewModels/BlogPostViewModel.cs create mode 100644 UnitystationLauncher/Views/BlogPostView.axaml create mode 100644 UnitystationLauncher/Views/BlogPostView.axaml.cs diff --git a/UnitystationLauncher/Assets/transparentdark.png b/UnitystationLauncher/Assets/transparentdark.png new file mode 100644 index 0000000000000000000000000000000000000000..c4b56c0c5442578831aacde43d8398a700cbb836 GIT binary patch literal 585 zcmV-P0=E5$P)EX>4Tx04R}tkv&MmKpe$iQ>7x63U&~2$WWc^VnrNl6^c+H)C#RSm|Xe=O$fqw6tAnc`2!4RLx;QDiNQwVT3N2zhIPS;0dyl(!fY7Wl&FV=4nr@rf zbV|$@R>aUNf(Rjk1O{bh8MBga#A3ONl`du#QzM=tj;WeX`9j`f zmGc&7tyX94d-4~Ci~8~s*J+L-jRh=1f(Qk5R8fPCIITJ<7P7RT^6`(jeu-QPxvF5~ zn8yY*$gUs!4}N!Rl@gO)QaAlHTZO zu_GX|4P0DzG-VIC+yRE44B3=jDM(W&m4Np%`lbRfbPM#adcC#xaryw{sH@BkaBv8W zmnnPA=iPnXz5RQp-QN#@^m43cxK6zQ000JJOGiWi{{a60|De66lK=n!32;bRa{vG? zBLDy{BLR4&KXw2B00(qQO+^Ri0|yT^23_pqO#lD@8FWQhbVF}#ZDnqB07G(RVRU6= zAa`kWXdp*PO;A^X4i^9b03u05K~y-)W0aDV+{{2lz{EghFlykafujbF8aQg;P%#hy X&xZjgNb&D^00000NkvXXu0mjfD() + ?? throw new NullReferenceException("No IAssetLoader found, check Autofac configuration."); + // Default image in case one is not provided + postImage ??= new(assets.Open(new("avares://StationHub/Assets/bgnews.png"))); + PostImage = postImage; + DarkenBg = new(assets.Open(new("avares://StationHub/Assets/transparentdark.png"))); + } + + private void OpenLink() + { + if (string.IsNullOrWhiteSpace(PostLink)) + { + return; + } + + ProcessStartInfo psi = new() + { + FileName = PostLink, + UseShellExecute = true + }; + Process.Start(psi); + } +} \ No newline at end of file diff --git a/UnitystationLauncher/ViewModels/NewsPanelViewModel.cs b/UnitystationLauncher/ViewModels/NewsPanelViewModel.cs index 1741eab6..691e4fb4 100644 --- a/UnitystationLauncher/ViewModels/NewsPanelViewModel.cs +++ b/UnitystationLauncher/ViewModels/NewsPanelViewModel.cs @@ -1,12 +1,8 @@ -using Avalonia; -using Avalonia.Platform; -using ReactiveUI; -using Avalonia.Media.Imaging; -using System; +using ReactiveUI; +using System.Collections.ObjectModel; using System.Reactive; using System.Diagnostics; using UnitystationLauncher.Constants; -using UnitystationLauncher.Models.ConfigFile; namespace UnitystationLauncher.ViewModels { @@ -16,28 +12,31 @@ public class NewsPanelViewModel : PanelBase public override bool IsEnabled => true; - ViewModelBase _changelog; - - Bitmap _backGroundImage; - - public ReactiveCommand OpenMainSite { get; } - public ReactiveCommand OpenPatreon { get; } - public ReactiveCommand OpenGameIssues { get; } - public ReactiveCommand OpenLauncherIssues { get; } - public ReactiveCommand OpenDiscordInvite { get; } - - public Bitmap BackGroundImage + private ViewModelBase? _currentBlogPost; + private ViewModelBase CurrentBlogPost { - get => _backGroundImage; - set => this.RaiseAndSetIfChanged(ref _backGroundImage, value); + get => _currentBlogPost ?? new BlogPostViewModel("Loading...", string.Empty, null); + set => this.RaiseAndSetIfChanged(ref _currentBlogPost, value); } + private ViewModelBase _changelog; public ViewModelBase Changelog { get => _changelog; set => this.RaiseAndSetIfChanged(ref _changelog, value); } + public ReactiveCommand OpenMainSite { get; } + public ReactiveCommand OpenPatreon { get; } + public ReactiveCommand OpenGameIssues { get; } + public ReactiveCommand OpenLauncherIssues { get; } + public ReactiveCommand OpenDiscordInvite { get; } + + private ObservableCollection BlogPosts { get; } + public ReactiveCommand NextBlog { get; } + public ReactiveCommand PreviousBlog { get; } + private int CurrentBlogPostIndex { get; set; } + public NewsPanelViewModel(ChangelogViewModel changelog) { _changelog = changelog; @@ -48,8 +47,46 @@ public NewsPanelViewModel(ChangelogViewModel changelog) OpenLauncherIssues = ReactiveCommand.Create(() => OpenLink(LinkUrls.LauncherIssuesUrl)); OpenDiscordInvite = ReactiveCommand.Create(() => OpenLink(LinkUrls.DiscordInviteUrl)); - var assets = AvaloniaLocator.Current.GetService(); - _backGroundImage = new Bitmap(assets?.Open(new Uri("avares://StationHub/Assets/bgnews.png"))); + BlogPosts = new(); + FetchBlogPosts(); + NextBlog = ReactiveCommand.Create(NextPost); + PreviousBlog = ReactiveCommand.Create(PreviousPost); + CurrentBlogPostIndex = 0; + + SetCurrentBlogPost(); + } + + private void FetchBlogPosts() + { + // TODO, fetch from blog post API, for now we can just hard code one. + BlogPosts.Add(new("Coming soon!", "https://www.unitystation.org/blog", null)); + } + + private void SetCurrentBlogPost() + { + CurrentBlogPost = BlogPosts[CurrentBlogPostIndex]; + } + + private void NextPost() + { + CurrentBlogPostIndex++; + if (CurrentBlogPostIndex >= BlogPosts.Count) + { + CurrentBlogPostIndex = 0; + } + + SetCurrentBlogPost(); + } + + private void PreviousPost() + { + CurrentBlogPostIndex--; + if (CurrentBlogPostIndex <= 0) + { + CurrentBlogPostIndex = BlogPosts.Count - 1; + } + + SetCurrentBlogPost(); } private static void OpenLink(string url) diff --git a/UnitystationLauncher/Views/BlogPostView.axaml b/UnitystationLauncher/Views/BlogPostView.axaml new file mode 100644 index 00000000..48c49c2c --- /dev/null +++ b/UnitystationLauncher/Views/BlogPostView.axaml @@ -0,0 +1,18 @@ + + + + diff --git a/UnitystationLauncher/Views/BlogPostView.axaml.cs b/UnitystationLauncher/Views/BlogPostView.axaml.cs new file mode 100644 index 00000000..ff08427b --- /dev/null +++ b/UnitystationLauncher/Views/BlogPostView.axaml.cs @@ -0,0 +1,17 @@ +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace UnitystationLauncher.Views; + +public class BlogPostView : UserControl +{ + public BlogPostView() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } +} \ No newline at end of file diff --git a/UnitystationLauncher/Views/NewsPanelView.xaml b/UnitystationLauncher/Views/NewsPanelView.xaml index f4a7d2f0..a528c9fe 100644 --- a/UnitystationLauncher/Views/NewsPanelView.xaml +++ b/UnitystationLauncher/Views/NewsPanelView.xaml @@ -42,7 +42,7 @@ - - -