Skip to content
代码片段 群组 项目
未验证 提交 cb461fc3 编辑于 作者: WithLithum's avatar WithLithum
浏览文件

:sparkles: 优化网络内容加载类并将数据移动到资源中

Fixes #I6B5QY, #I6B5R0
上级 b3f5cbc1
No related branches found
No related tags found
无相关合并请求
namespace NexusKrop.Mir.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public record struct ProductInfo(string HomePageData, string NewsPageData);
......@@ -7,7 +7,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:NexusKrop.Mir.Pages"
xmlns:models="using:NexusKrop.Mir.Models"
xmlns:util="using:NexusKrop.Mir.Util"
xmlns:network="using:NexusKrop.Mir.Util.Network"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
......@@ -47,13 +47,13 @@
</CommandBar>
<ProgressRing Name="ProgressMain" Visibility="Collapsed" IsActive="False"/>
<FlipView Name="FlipMain"
ItemsSource="{x:Bind util:AppMetadataHelper.Instance.FlipItems}"
ItemsSource="{x:Bind network:NetworkDataService.Instance.FlipItems}"
Grid.Row="1"
ItemTemplate="{StaticResource FlipItemTemplate}">
</FlipView>
<Grid Grid.Row="2" Margin="15, 15, 15, 15" Name="GridActivity">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300px"/>
<ColumnDefinition Width="300px" />
<ColumnDefinition Width="300px" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
......@@ -63,7 +63,7 @@
</Grid.RowDefinitions>
<TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="Community" x:Uid="TextCommunity"/>
<ListBox Grid.Row="1" Name="ListCommunity"
ItemsSource="{x:Bind util:AppMetadataHelper.Instance.CommunityFeed}"
ItemsSource="{x:Bind network:NetworkDataService.Instance.CommunityFeed}"
ItemTemplate="{StaticResource ActivityTemplate}">
</ListBox>
</Grid>
......@@ -74,7 +74,7 @@
</Grid.RowDefinitions>
<TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="NexusKrop"/>
<ListBox Grid.Row="1" Name="ListOfficial"
ItemsSource="{x:Bind util:AppMetadataHelper.Instance.OfficialFeed}"
ItemsSource="{x:Bind network:NetworkDataService.Instance.OfficialFeed}"
ItemTemplate="{StaticResource ActivityTemplate}">
</ListBox>
</Grid>
......
......@@ -14,8 +14,10 @@ using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using NexusKrop.Mir.Models;
using NexusKrop.Mir.Util;
using NexusKrop.Mir.Util.Network;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
......@@ -34,7 +36,19 @@ public sealed partial class HomePage : Page
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
await AppMetadataHelper.Instance.InitializeAsync(this);
SetActivity(true);
try
{
await NetworkDataService.Instance.InitializeAsync(this);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return;
}
SetActivity(false);
}
private void SetActivity(bool activity)
......@@ -59,7 +73,7 @@ public sealed partial class HomePage : Page
{
SetActivity(true);
await AppMetadataHelper.Instance.RefreshAsync(this);
await NetworkDataService.Instance.RefreshAsync(this);
SetActivity(false);
}
......
......@@ -10,6 +10,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:util="using:NexusKrop.Mir.Util"
xmlns:network="using:NexusKrop.Mir.Util.Network"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
Loaded="Page_Loaded">
......@@ -43,7 +44,7 @@
<GridView Name="ViewMain"
Grid.Row="1"
Margin="8, 0, 0, 0"
ItemsSource="{x:Bind util:NewsHelper.Instance.NewsArticles}"
ItemsSource="{x:Bind network:NetworkDataService.Instance.NewsArticles}"
ItemTemplate="{StaticResource IconTextTemplate}"
Visibility="Visible"
SelectionChanged="ViewMain_SelectionChanged"/>
......
......@@ -14,6 +14,7 @@ using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using NexusKrop.Mir.Models;
using NexusKrop.Mir.Util;
using NexusKrop.Mir.Util.Network;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
......@@ -45,7 +46,7 @@ public sealed partial class NewsPage : Page
try
{
await NewsHelper.Instance.RefreshAsync(this);
await NetworkDataService.Instance.RefreshAsync(this);
}
catch (Exception ex)
{
......@@ -64,7 +65,7 @@ public sealed partial class NewsPage : Page
try
{
await NewsHelper.Instance.InitializeAsync(this);
await NetworkDataService.Instance.InitializeAsync(this);
}
catch (Exception ex)
{
......
......@@ -69,5 +69,17 @@ namespace NexusKrop.Mir.Resources {
return ((byte[])(obj));
}
}
/// <summary>
/// Looks up a localized string similar to {
/// &quot;HomePageData&quot;: &quot;http://localhost:8080/app.json&quot;,
/// &quot;NewsPageData&quot;: &quot;http://localhost:8080/test.json&quot;
///}.
/// </summary>
internal static string Product {
get {
return ResourceManager.GetString("Product", resourceCulture);
}
}
}
}
......@@ -121,4 +121,7 @@
<data name="NexusKrop" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>NexusKrop.ico;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="Product" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Product.json;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
</root>
\ No newline at end of file
{
"HomePageData": "http://localhost:8080/app.json",
"NewsPageData": "http://localhost:8080/test.json"
}
\ No newline at end of file
namespace NexusKrop.Mir.Util.Network;
using CommunityToolkit.WinUI;
using Microsoft.UI.Xaml;
using Newtonsoft.Json;
using NexusKrop.Mir.Models;
using NexusKrop.Mir.Resources;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class NetworkDataService
{
private bool _initialized;
public static NetworkDataService Instance { get; } = new();
public static ProductInfo ProductInfo { get; }
static NetworkDataService()
{
ProductInfo = JsonConvert.DeserializeObject<ProductInfo>(AppResources.Product);
}
public ObservableCollection<FlipItem> FlipItems { get; } = new();
public ObservableCollection<ActivityPush> CommunityFeed { get; } = new();
public ObservableCollection<ActivityPush> OfficialFeed { get; } = new();
public ObservableCollection<NewsArticle> NewsArticles { get; } = new();
internal async Task InitializeAsync(UIElement host)
{
if (!_initialized)
{
await RefreshAsync(host);
_initialized = true;
}
}
internal async Task RefreshAsync(UIElement host)
{
var homePage = await GetRemoteDataAsync<AppMetadata>(ProductInfo.HomePageData);
var newsPage = await GetRemoteDataAsync<NewsArticle[]>(ProductInfo.NewsPageData);
await host.DispatcherQueue.EnqueueAsync(() =>
{
NewsArticles.Clear();
foreach (var article in newsPage)
{
NewsArticles.Add(article);
}
CommunityFeed.Clear();
foreach (var community in homePage.CommunityActivity)
{
CommunityFeed.Add(community);
}
OfficialFeed.Clear();
foreach (var official in homePage.NexusKropActivity)
{
OfficialFeed.Add(official);
}
FlipItems.Clear();
foreach (var flip in homePage.FlipItems)
{
FlipItems.Add(flip);
}
});
}
internal static async Task<T> GetRemoteDataAsync<T>(string url)
{
try
{
using var stream = new JsonTextReader(new StreamReader(await WebUtil.HTTP_CLIENT.GetStreamAsync(url)));
return await Task.Run(() => WebUtil.SERILAIZER.Deserialize<T>(stream));
}
catch
{
throw;
}
}
}
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册