Skip to content

Commit

Permalink
Add placeholder blog post to news panel (#187)
Browse files Browse the repository at this point in the history
* Add placeholder blog post to news

* Fix Codacy issues

* Fix compile warning
  • Loading branch information
CorruptComputer authored Mar 9, 2023
1 parent 56ac85c commit 571c0f8
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 25 deletions.
Binary file added UnitystationLauncher/Assets/transparentdark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions UnitystationLauncher/ViewModels/BlogPostViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;

namespace UnitystationLauncher.ViewModels;

public class BlogPostViewModel : ViewModelBase
{
private string Title { get; }
private string PostLink { get; }
private Bitmap PostImage { get; }
private Bitmap DarkenBg { get; }

public BlogPostViewModel(string title, string postLink, Bitmap? postImage)
{
Title = title;
PostLink = postLink;
IAssetLoader assets = AvaloniaLocator.Current.GetService<IAssetLoader>()
?? 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);
}
}
79 changes: 58 additions & 21 deletions UnitystationLauncher/ViewModels/NewsPanelViewModel.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -16,28 +12,31 @@ public class NewsPanelViewModel : PanelBase

public override bool IsEnabled => true;

ViewModelBase _changelog;

Bitmap _backGroundImage;

public ReactiveCommand<Unit, Unit> OpenMainSite { get; }
public ReactiveCommand<Unit, Unit> OpenPatreon { get; }
public ReactiveCommand<Unit, Unit> OpenGameIssues { get; }
public ReactiveCommand<Unit, Unit> OpenLauncherIssues { get; }
public ReactiveCommand<Unit, Unit> 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<Unit, Unit> OpenMainSite { get; }
public ReactiveCommand<Unit, Unit> OpenPatreon { get; }
public ReactiveCommand<Unit, Unit> OpenGameIssues { get; }
public ReactiveCommand<Unit, Unit> OpenLauncherIssues { get; }
public ReactiveCommand<Unit, Unit> OpenDiscordInvite { get; }

private ObservableCollection<BlogPostViewModel> BlogPosts { get; }
public ReactiveCommand<Unit, Unit> NextBlog { get; }
public ReactiveCommand<Unit, Unit> PreviousBlog { get; }
private int CurrentBlogPostIndex { get; set; }

public NewsPanelViewModel(ChangelogViewModel changelog)
{
_changelog = changelog;
Expand All @@ -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<IAssetLoader>();
_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)
Expand Down
18 changes: 18 additions & 0 deletions UnitystationLauncher/Views/BlogPostView.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="UnitystationLauncher.Views.BlogPostView">
<Button Margin="0 0.6 0 0" Command="{Binding OpenLink}">
<Panel>
<Image Margin="0.5" Source="{Binding PostImage}" Stretch="UniformToFill" Opacity="0.459" />

<Panel VerticalAlignment="Bottom" MaxHeight="50">
<Image Source="{Binding DarkenBg}" Stretch="UniformToFill" />
<TextBlock Text="{Binding Title}" VerticalAlignment="Bottom" HorizontalAlignment="Center" FontSize="35" Opacity="1" />
</Panel>
</Panel>
</Button>

</UserControl>
17 changes: 17 additions & 0 deletions UnitystationLauncher/Views/BlogPostView.axaml.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 3 additions & 4 deletions UnitystationLauncher/Views/NewsPanelView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ColumnDefinition Width="0.8*" />
<ColumnDefinition Width="0.1*" />
</Grid.ColumnDefinitions>
<Button Margin="0.6 0.6 0 0" Grid.Column="0" Command="{Binding PastVideo}">
<Button Margin="0.6 0.6 0 0" Grid.Column="0" Command="{Binding PreviousBlog}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.25*" />
Expand All @@ -58,9 +58,8 @@
Source="/Assets/leftbutton.png" Stretch="Uniform" />
</Grid>
</Button>
<Image Grid.Column="0" Grid.ColumnSpan="3" Margin="0.5" Source="{Binding BackGroundImage}"
Stretch="UniformToFill" Opacity="0.459" />
<Button Margin="0 0.6 0.6 0" Grid.Column="2" Command="{Binding NextVideo}">
<ContentControl Grid.Column="1" Content="{Binding CurrentBlogPost}" />
<Button Margin="0 0.6 0.6 0" Grid.Column="2" Command="{Binding NextBlog}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.25*" />
Expand Down

0 comments on commit 571c0f8

Please sign in to comment.