2
0
Эх сурвалжийг харах

Basic news fetching works

Krzysztof Krysiński 2 жил өмнө
parent
commit
bb6e39a94a

+ 1 - 0
src/PixiEditor.Platform.MSStore/MicrosoftStorePlatform.cs

@@ -2,6 +2,7 @@
 
 
 public sealed class MicrosoftStorePlatform : IPlatform
 public sealed class MicrosoftStorePlatform : IPlatform
 {
 {
+    public string Id { get; } = "ms-store";
     public string Name => "Microsoft Store";
     public string Name => "Microsoft Store";
 
 
     public bool PerformHandshake()
     public bool PerformHandshake()

+ 1 - 0
src/PixiEditor.Platform.Standalone/StandalonePlatform.cs

@@ -2,6 +2,7 @@
 
 
 public sealed class StandalonePlatform : IPlatform
 public sealed class StandalonePlatform : IPlatform
 {
 {
+    public string Id { get; } = "standalone";
     public string Name => "Standalone";
     public string Name => "Standalone";
 
 
     public bool PerformHandshake()
     public bool PerformHandshake()

+ 1 - 0
src/PixiEditor.Platform.Steam/SteamPlatform.cs

@@ -4,6 +4,7 @@ namespace PixiEditor.Platform.Steam;
 
 
 public class SteamPlatform : IPlatform
 public class SteamPlatform : IPlatform
 {
 {
+    public string Id { get; } = "steam";
     public string Name => "Steam";
     public string Name => "Steam";
 
 
     public bool PerformHandshake()
     public bool PerformHandshake()

+ 1 - 0
src/PixiEditor.Platform/IPlatform.cs

@@ -3,6 +3,7 @@
 public interface IPlatform
 public interface IPlatform
 {
 {
     public static IPlatform Current { get; private set; }
     public static IPlatform Current { get; private set; }
+    public abstract string Id { get; }
     public abstract string Name { get; }
     public abstract string Name { get; }
     public bool PerformHandshake();
     public bool PerformHandshake();
     public IAdditionalContentProvider? AdditionalContentProvider { get; }
     public IAdditionalContentProvider? AdditionalContentProvider { get; }

+ 2 - 1
src/PixiEditor/Models/Services/NewsFeed/News.cs

@@ -13,6 +13,7 @@ internal record News
 {
 {
     public string Title { get; set; }
     public string Title { get; set; }
     public string ShortDescription { get; set; }
     public string ShortDescription { get; set; }
-    public NewsType Type { get; set; }
+    public NewsType NewsType { get; set; }
     public string Url { get; set; }
     public string Url { get; set; }
+    public DateTime Date { get; set; }
 }
 }

+ 17 - 3
src/PixiEditor/Models/Services/NewsFeed/NewsProvider.cs

@@ -1,11 +1,25 @@
-namespace PixiEditor.Models.Services.NewsFeed;
+using System.Net;
+using System.Net.Http;
+using Newtonsoft.Json;
+using PixiEditor.UpdateModule;
+
+namespace PixiEditor.Models.Services.NewsFeed;
 
 
 internal class NewsProvider
 internal class NewsProvider
 {
 {
     private const int ProtocolVersion = 1;
     private const int ProtocolVersion = 1;
-    private const string FeedUrl = "";
-    public async Task<News> FetchNews()
+    private const string FeedUrl = "https://raw.githubusercontent.com/PixiEditor/news-feed/main/";
+    public async Task<List<News>?> FetchNewsAsync()
     {
     {
+        using HttpClient client = new HttpClient();
+        client.DefaultRequestHeaders.Add("User-Agent", "PixiEditor");
+        HttpResponseMessage response = await client.GetAsync(FeedUrl + "shared.json");
+        if (response.StatusCode == HttpStatusCode.OK)
+        {
+            string content = await response.Content.ReadAsStringAsync();
+            return JsonConvert.DeserializeObject<List<News>>(content);
+        }
 
 
+        return null;
     }
     }
 }
 }

+ 9 - 4
src/PixiEditor/Views/Dialogs/HelloTherePopup.xaml

@@ -13,10 +13,11 @@
         xmlns:views="clr-namespace:PixiEditor.Views"
         xmlns:views="clr-namespace:PixiEditor.Views"
         xmlns:helpers="clr-namespace:PixiEditor.Helpers"
         xmlns:helpers="clr-namespace:PixiEditor.Helpers"
         xmlns:ui="clr-namespace:PixiEditor.Extensions.UI;assembly=PixiEditor.Extensions"
         xmlns:ui="clr-namespace:PixiEditor.Extensions.UI;assembly=PixiEditor.Extensions"
+        xmlns:newsFeed="clr-namespace:PixiEditor.Views.UserControls.NewsFeed"
         mc:Ignorable="d" ShowInTaskbar="False"
         mc:Ignorable="d" ShowInTaskbar="False"
         Title="Hello there!" Height="662" Width="832" MinHeight="500" MinWidth="500"
         Title="Hello there!" Height="662" Width="832" MinHeight="500" MinWidth="500"
         d:DataContext="{d:DesignInstance local:HelloTherePopup}"
         d:DataContext="{d:DesignInstance local:HelloTherePopup}"
-        WindowStyle="None" WindowStartupLocation="CenterScreen"
+        WindowStyle="None" WindowStartupLocation="CenterScreen" Loaded="HelloTherePopup_OnLoaded"
         FlowDirection="{helpers:Localization FlowDirection}">
         FlowDirection="{helpers:Localization FlowDirection}">
 
 
     <Window.Resources>
     <Window.Resources>
@@ -43,7 +44,7 @@
         </Grid.RowDefinitions>
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="*"/>
             <ColumnDefinition Width="*"/>
-            <ColumnDefinition Width="200"/>
+            <ColumnDefinition Width="300"/>
         </Grid.ColumnDefinitions>
         </Grid.ColumnDefinitions>
 
 
         <DockPanel Grid.Row="0" Grid.ColumnSpan="2" Background="{StaticResource MainColor}">
         <DockPanel Grid.Row="0" Grid.ColumnSpan="2" Background="{StaticResource MainColor}">
@@ -281,8 +282,12 @@
             <Border Padding="5" BorderThickness="3 0 0 0" BorderBrush="{StaticResource MainColor}">
             <Border Padding="5" BorderThickness="3 0 0 0" BorderBrush="{StaticResource MainColor}">
                 <StackPanel Orientation="Vertical">
                 <StackPanel Orientation="Vertical">
                     <TextBlock HorizontalAlignment="Center" Text="News" FontSize="18"/>
                     <TextBlock HorizontalAlignment="Center" Text="News" FontSize="18"/>
-                    <ItemsControl>
-
+                    <ItemsControl ItemsSource="{Binding Path=News}">
+                        <ItemsControl.ItemTemplate>
+                            <DataTemplate>
+                                <newsFeed:NewsItem News="{Binding Path=.}"/>
+                            </DataTemplate>
+                        </ItemsControl.ItemTemplate>
                     </ItemsControl>
                     </ItemsControl>
                 </StackPanel>
                 </StackPanel>
             </Border>
             </Border>

+ 22 - 4
src/PixiEditor/Views/Dialogs/HelloTherePopup.xaml.cs

@@ -1,10 +1,12 @@
-using System.Diagnostics;
+using System.Collections.ObjectModel;
+using System.Diagnostics;
 using System.IO;
 using System.IO;
 using System.Windows;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Commands;
 using PixiEditor.Models.Commands;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
+using PixiEditor.Models.Services.NewsFeed;
 using PixiEditor.ViewModels.SubViewModels.Main;
 using PixiEditor.ViewModels.SubViewModels.Main;
 
 
 namespace PixiEditor.Views.Dialogs;
 namespace PixiEditor.Views.Dialogs;
@@ -22,6 +24,8 @@ internal partial class HelloTherePopup : Window
     public static readonly DependencyProperty RecentlyOpenedEmptyProperty =
     public static readonly DependencyProperty RecentlyOpenedEmptyProperty =
         DependencyProperty.Register(nameof(RecentlyOpenedEmpty), typeof(bool), typeof(HelloTherePopup));
         DependencyProperty.Register(nameof(RecentlyOpenedEmpty), typeof(bool), typeof(HelloTherePopup));
 
 
+    public WpfObservableRangeCollection<News> News { get; set; } = new WpfObservableRangeCollection<News>();
+
     public static string VersionText =>
     public static string VersionText =>
         $"v{VersionHelpers.GetCurrentAssemblyVersionString()}";
         $"v{VersionHelpers.GetCurrentAssemblyVersionString()}";
 
 
@@ -39,6 +43,8 @@ internal partial class HelloTherePopup : Window
 
 
     public bool IsClosing { get; private set; }
     public bool IsClosing { get; private set; }
 
 
+    private NewsProvider NewsProvider { get; set; }
+
     public bool ShowDonateButton => // Steam doesn't allow external donations :(
     public bool ShowDonateButton => // Steam doesn't allow external donations :(
 #if STEAM
 #if STEAM
         false;
         false;
@@ -60,23 +66,25 @@ internal partial class HelloTherePopup : Window
         RecentlyOpenedEmpty = RecentlyOpened.Count == 0;
         RecentlyOpenedEmpty = RecentlyOpened.Count == 0;
         RecentlyOpened.CollectionChanged += RecentlyOpened_CollectionChanged;
         RecentlyOpened.CollectionChanged += RecentlyOpened_CollectionChanged;
 
 
+        NewsProvider = new NewsProvider();
+
         Closing += (_, _) => { IsClosing = true; };
         Closing += (_, _) => { IsClosing = true; };
 
 
         InitializeComponent();
         InitializeComponent();
 
 
         if (RecentlyOpenedEmpty)
         if (RecentlyOpenedEmpty)
         {
         {
-            Width = 600;
+            Width = 700;
             Height = 500;
             Height = 500;
         }
         }
         else if (RecentlyOpened.Count < 4)
         else if (RecentlyOpened.Count < 4)
         {
         {
-            Width = 645;
+            Width = 745;
             Height = 500;
             Height = 500;
         }
         }
         else if (RecentlyOpened.Count < 7)
         else if (RecentlyOpened.Count < 7)
         {
         {
-            Width = 675;
+            Width = 775;
             Height = 670;
             Height = 670;
         }
         }
     }
     }
@@ -124,4 +132,14 @@ internal partial class HelloTherePopup : Window
     }
     }
 
 
     private bool CanOpenInExplorer(object parameter) => File.Exists((string)parameter);
     private bool CanOpenInExplorer(object parameter) => File.Exists((string)parameter);
+
+    private async void HelloTherePopup_OnLoaded(object sender, RoutedEventArgs e)
+    {
+        var news = await NewsProvider.FetchNewsAsync();
+        if (news is not null)
+        {
+            News.Clear();
+            News.AddRange(news);
+        }
+    }
 }
 }

+ 14 - 0
src/PixiEditor/Views/UserControls/NewsFeed/NewsItem.xaml

@@ -0,0 +1,14 @@
+<UserControl x:Class="PixiEditor.Views.UserControls.NewsFeed.NewsItem"
+             x:ClassModifier="internal"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+             xmlns:local="clr-namespace:PixiEditor.Views.UserControls.NewsFeed"
+             mc:Ignorable="d" Name="newsItem"
+             d:DesignHeight="300" d:DesignWidth="300">
+    <StackPanel Orientation="Vertical">
+        <TextBlock Foreground="White" FontSize="14" Text="{Binding ElementName=newsItem, Path=News.Title}"/>
+        <TextBlock Foreground="White" FontSize="12" Text="{Binding ElementName=newsItem, Path=News.ShortDescription}"/>
+    </StackPanel>
+</UserControl>

+ 22 - 0
src/PixiEditor/Views/UserControls/NewsFeed/NewsItem.xaml.cs

@@ -0,0 +1,22 @@
+using System.Windows;
+using System.Windows.Controls;
+using PixiEditor.Models.Services.NewsFeed;
+
+namespace PixiEditor.Views.UserControls.NewsFeed;
+
+internal partial class NewsItem : UserControl
+{
+    public static readonly DependencyProperty NewsProperty = DependencyProperty.Register(
+        nameof(News), typeof(News), typeof(NewsItem), new PropertyMetadata(default(News)));
+
+    public News News
+    {
+        get { return (News)GetValue(NewsProperty); }
+        set { SetValue(NewsProperty, value); }
+    }
+    public NewsItem()
+    {
+        InitializeComponent();
+    }
+}
+