XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFENewsApplication

【WinUI】新闻软件

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFENewsApplication

优化了图片展示

37a4de9
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

8 个文件 +134 -13
Modified XFENewsApplication.Core/Utilities/Helpers/ClimbHelper.cs +2 -2
@@ -50,9 +50,9 @@ public static class ClimbHelper
50 50 if (MSNAPI.IsNullOrWhiteSpace())
51 51 MSNAPI = await GetMSNAPI(cancellationToken);
52 52 if (lastCardCount == 0 || skip == 0)
53 return await ClimbMSNNews($"https://assets.msn.com/service/news/feed/pages/weblayout?User=m-155BB6F5B09364CA1581A33DB1AB65B7&activityId=DB241CDF-B902-4268-A815-0B8C46791830&adoffsets=c1:-1,c2:-1&apikey={MSNAPI}&audienceMode=adult&cm=zh-cn&colstatus=c1:0,c2:0&column=c2&colwidth=300&contentType=article&it=edgeid&l3v=2&layout=c2&memory=8&newsSkip=0&newsTop=48&ocid=hponeservicefeed&pgc=11&private=1&scn=APP_ANON&timeOut=1000&vpSize=721x674&wposchema=byregion", cancellationToken);
53 return await ClimbMSNNews($"https://assets.msn.com/service/news/feed/pages/weblayout?User=&activityId=&adoffsets=c1:-1,c2:-1&apikey={MSNAPI}&audienceMode=adult&cm=zh-cn&colstatus=c1:0,c2:0&column=c2&colwidth=300&contentType=article&it=edgeid&l3v=2&layout=c2&memory=8&newsSkip=0&newsTop=48&ocid=hponeservicefeed&pgc=11&private=1&scn=APP_ANON&timeOut=1000&vpSize=721x674&wposchema=byregion", cancellationToken);
54 54 else
55 return await ClimbMSNNews($"https://assets.msn.com/service/news/feed/pages/weblayout?$skip={skip}&User=m-155BB6F5B09364CA1581A33DB1AB65B7&activityId=DB241CDF-B902-4268-A815-0B8C46791830&adoffsets=c1:304,c2:912,c3:-1&adsTimeout=600&alladoffsets=c1:1824|c2:1520,608|c3:304,912,3041|c4:2129,304,608,2129|c5:1825,304,1216,608,1825|&allcoloffsets=c1:0|c2:0,0|c3:0,0,0|c4:0,0,0,0|c5:0,0,0,0,0|&alltwocoffsets=c1:7601|c2:304,304|c3:1824,304,304|c4:1216,1216,1520,1520|c5:0,0,304,304,1825|&apikey={MSNAPI}&audienceMode=adult&cardsServed={servedCardCount}&cm=zh-cn&colstatus=c1:0,c2:0,c3:0&column=c3&colwidth=300&contentType=article&cookieWallPresent=false&l3v=2&lastcardrank={lastCardCount}&layout=c3&memory=8&mobile=false&ocid=hponeservicefeed&private=1&renderedSegments=35734127939585&timeOut=2000&vpSize=1097x1317&wpoCmsAdServed=0&wposchema=byregion", cancellationToken);
55 return await ClimbMSNNews($"https://assets.msn.com/service/news/feed/pages/weblayout?$skip={skip}&User=&activityId=&adoffsets=c1:304,c2:912,c3:-1&adsTimeout=600&alladoffsets=c1:1824|c2:1520,608|c3:304,912,3041|c4:2129,304,608,2129|c5:1825,304,1216,608,1825|&allcoloffsets=c1:0|c2:0,0|c3:0,0,0|c4:0,0,0,0|c5:0,0,0,0,0|&alltwocoffsets=c1:7601|c2:304,304|c3:1824,304,304|c4:1216,1216,1520,1520|c5:0,0,304,304,1825|&apikey={MSNAPI}&audienceMode=adult&cardsServed={servedCardCount}&cm=zh-cn&colstatus=c1:0,c2:0,c3:0&column=c3&colwidth=300&contentType=article&cookieWallPresent=false&l3v=2&lastcardrank={lastCardCount}&layout=c3&memory=8&mobile=false&ocid=hponeservicefeed&private=1&renderedSegments=35734127939585&timeOut=2000&vpSize=1097x1317&wpoCmsAdServed=0&wposchema=byregion", cancellationToken);
56 56 }
57 57
58 58 public static async Task<NewsResult> ClimbMSNNews(string requestUrl, CancellationToken cancellationToken)
Added XFENewsApplication/Models/AuthorArticlePart.cs +9 -0
@@ -0,0 +1,9 @@
1 namespace XFENewsApplication.Models;
2
3 public class AuthorArticlePart : ArticlePart
4 {
5 public string? AuthorName { get; set; }
6 public string? ProviderName { get; set; }
7 public string? PublishDateTime { get; set; }
8 public string? ReadTime { get; set; }
9 }
Modified XFENewsApplication/Models/ImageArticlePart.cs +4 -1
@@ -1,3 +1,6 @@
1 1 namespace XFENewsApplication.Models;
2 2
3 public class ImageArticlePart : ArticlePart { }
3 public class ImageArticlePart : ArticlePart
4 {
5 public string? From { get; set; }
6 }
Added XFENewsApplication/Utilities/Converter/StringEmptyVisibilityConverter.cs +11 -0
@@ -0,0 +1,11 @@
1 using Microsoft.UI.Xaml.Data;
2 using XFEExtension.NetCore.StringExtension;
3
4 namespace XFENewsApplication.Utilities.Converter;
5
6 public partial class StringEmptyVisibilityConverter : IValueConverter
7 {
8 public object Convert(object value, Type targetType, object parameter, string language) => value is not string stringValue || stringValue.IsNullOrEmpty() ? Visibility.Collapsed : Visibility.Visible;
9
10 public object ConvertBack(object value, Type targetType, object parameter, string language) => value;
11 }
Modified XFENewsApplication/Utilities/Helper/ArticleDisplayHelper.cs +65 -6
@@ -14,17 +14,30 @@ public static class ArticleDisplayHelper
14 14 var articleParts = new List<ArticlePart>();
15 15 var imageDictionary = new Dictionary<string, string>();
16 16 var document = JsonDocument.Parse(newsDocument);
17 var title = document.RootElement.GetProperty("title").ToString();
17 18 articleParts.Add(new TextArticlePart
18 19 {
19 20 FontSize = 40,
20 21 FontWeight = FontWeights.Bold,
21 Content = document.RootElement.GetProperty("title").ToString()
22 Content = title
22 23 });
24 if (document.RootElement.TryGetProperty("provider", out var provider))
25 {
26 var providerName = provider.GetProperty("name").ToString();
27 articleParts.Add(new AuthorArticlePart
28 {
29 Content = provider.GetProperty("logo").GetProperty("url").ToString(),
30 PublishDateTime = GetRelativeTimeDescription(document.RootElement.GetProperty("publishedDateTime").ToString()),
31 ReadTime = document.RootElement.TryGetProperty("readTimeMin", out var readTime) && readTime.GetInt32() > 0 ? readTime.ToString() : string.Empty,
32 ProviderName = providerName,
33 AuthorName = document.RootElement.TryGetProperty("authors", out var authors) && authors.EnumerateArray().Any() ? string.Join(", ", authors.EnumerateArray().Select(x => x.GetProperty("name").ToString())) : providerName
34 });
35 }
23 36 foreach (var imageNode in document.RootElement.GetProperty("imageResources").EnumerateArray())
24 37 imageDictionary.Add(imageNode.GetProperty("cmsId").ToString(), imageNode.GetProperty("url").ToString());
25 38 var htmlDocument = new HtmlDocument();
26 39 htmlDocument.LoadHtml(document.RootElement.GetProperty("body").ToString());
27 AnalysisMSNDocument(articleParts, imageDictionary, htmlDocument.DocumentNode.ChildNodes, htmlDocument.DocumentNode);
40 AnalysisMSNDocument(articleParts, title, imageDictionary, htmlDocument.DocumentNode.ChildNodes, htmlDocument.DocumentNode);
28 41 return articleParts;
29 42 }
30 43
@@ -48,7 +61,7 @@ public static class ArticleDisplayHelper
48 61 return customTextArticlePart;
49 62 }
50 63
51 private static void AnalysisMSNDocument(List<ArticlePart> articleParts, Dictionary<string, string> imageDictionary, HtmlNodeCollection htmlNodes, HtmlNode parentNode)
64 private static void AnalysisMSNDocument(List<ArticlePart> articleParts, string title, Dictionary<string, string> imageDictionary, HtmlNodeCollection htmlNodes, HtmlNode parentNode)
52 65 {
53 66 foreach (var node in htmlNodes)
54 67 {
@@ -57,14 +70,21 @@ public static class ArticleDisplayHelper
57 70 case "img":
58 71 articleParts.Add(new ImageArticlePart
59 72 {
60 Content = imageDictionary[node.GetAttributeValue("data-document-id", string.Empty).Replace("\"", string.Empty)]
73 Content = imageDictionary[node.GetAttributeValue("data-document-id", string.Empty).Replace("\"", string.Empty)],
74 From = title
61 75 });
62 76 break;
63 77 case "#text":
64 78 switch (parentNode.Name)
65 79 {
66 80 case "p":
67 var customTextArticlePart = CheckLastArticlePart(articleParts, parentNode, new Run
81 CheckLastArticlePart(articleParts, parentNode, new Run
82 {
83 Text = node.InnerText
84 });
85 break;
86 case "span":
87 CheckLastArticlePart(articleParts, parentNode.ParentNode, new Run
68 88 {
69 89 Text = node.InnerText
70 90 });
@@ -150,9 +170,48 @@ public static class ArticleDisplayHelper
150 170 break;
151 171 default:
152 172 if (node.HasChildNodes)
153 AnalysisMSNDocument(articleParts, imageDictionary, node.ChildNodes, node);
173 AnalysisMSNDocument(articleParts, title, imageDictionary, node.ChildNodes, node);
154 174 break;
155 175 }
156 176 }
157 177 }
178
179 public static string GetRelativeTimeDescription(DateTime time)
180 {
181 var now = DateTime.Now;
182 var span = now - time;
183
184 if (span.TotalSeconds < 60)
185 {
186 return "现在";
187 }
188 else if (span.TotalMinutes < 60)
189 {
190 int minutes = (int)span.TotalMinutes;
191 return $"{minutes} 分钟前";
192 }
193 else if (span.TotalHours < 24)
194 {
195 int hours = (int)span.TotalHours;
196 return $"{hours} 小时前";
197 }
198 else if (span.TotalDays < 7)
199 {
200 int days = (int)span.TotalDays;
201 return $"{days} 天前";
202 }
203 else
204 {
205 return time.ToString("yyyy年MM月dd日 HH:mm");
206 }
207 }
208
209 public static string GetRelativeTimeDescription(string time)
210 {
211 if (DateTime.TryParse(time, out var dateTime))
212 {
213 return GetRelativeTimeDescription(dateTime);
214 }
215 return "暂无时间";
216 }
158 217 }
Modified XFENewsApplication/Utilities/Selector/ArticleDataTemplateSelector.cs +3 -0
@@ -7,6 +7,7 @@ public partial class ArticleDataTemplateSelector : DataTemplateSelector
7 7 {
8 8 public DataTemplate? DefaultDataTemplate { get; set; }
9 9 public DataTemplate? TextDataTemplate { get; set; }
10 public DataTemplate? AuthorDataTemplate { get; set; }
10 11 public DataTemplate? TitleDataTemplate { get; set; }
11 12 public DataTemplate? QuoteDataTemplate { get; set; }
12 13 public DataTemplate? CustomTextDataTemplate { get; set; }
@@ -27,6 +28,8 @@ public partial class ArticleDataTemplateSelector : DataTemplateSelector
27 28 }
28 29 if (articlePart is ImageArticlePart)
29 30 return ImageDataTemplate;
31 if (articlePart is AuthorArticlePart)
32 return AuthorDataTemplate;
30 33 return TextDataTemplate;
31 34 }
32 35 return DefaultDataTemplate;
Modified XFENewsApplication/ViewModels/NewsViewPageViewModel.cs +5 -1
@@ -106,6 +106,7 @@ public partial class NewsViewPageViewModel : ViewModelBase
106 106 IsSideBarMoreContentLoadingVisible = true;
107 107 break;
108 108 case "Favorite":
109 IsSideBarMoreContentLoadingVisible = false;
109 110 if (NavigationViewService is not null)
110 111 NavigationViewService.ContentMargin = new(0, 24, 0, 0);
111 112 LoadNewsSource(UserProfile.FavoriteArticleList.Select(favorite => favorite.NewsSource));
@@ -114,6 +115,7 @@ public partial class NewsViewPageViewModel : ViewModelBase
114 115 IsContentLoadingVisible = false;
115 116 break;
116 117 case "History":
118 IsSideBarMoreContentLoadingVisible = false;
117 119 if (NavigationViewService is not null)
118 120 NavigationViewService.ContentMargin = new(0, 24, 0, 0);
119 121 LoadNewsSource(UserProfile.HistoryArticleList);
@@ -214,7 +216,7 @@ public partial class NewsViewPageViewModel : ViewModelBase
214 216 isLoadingNews = true;
215 217 var tokenSource = new CancellationTokenSource();
216 218 TokenSource = tokenSource;
217 var result = await ClimbHelper.ClimbMSNNews(tokenSource.Token, skip, lastCardCount);
219 var result = await ClimbHelper.ClimbMSNNews(tokenSource.Token, skip, lastCardCount, servedCardCount);
218 220 if (result.Success)
219 221 {
220 222 newsList.AddRange(result.NewsSourceList);
@@ -275,6 +277,7 @@ public partial class NewsViewPageViewModel : ViewModelBase
275 277 NewsSource = newsSource,
276 278 ArticleContent = currentNewsContent ?? string.Empty
277 279 });
280 UserProfile.SaveProfile();
278 281 MessageService?.ShowMessage("已添加到收藏夹", "收藏成功", InfoBarSeverity.Success);
279 282 }
280 283 }
@@ -283,6 +286,7 @@ public partial class NewsViewPageViewModel : ViewModelBase
283 286 if (NewsListViewService.ListView.SelectedItem is NewsSource newsSource)
284 287 {
285 288 UserProfile.FavoriteArticleList.Remove(UserProfile.FavoriteArticleList.First(favorite => favorite.NewsSource.Url == newsSource.Url));
289 UserProfile.SaveProfile();
286 290 MessageService?.ShowMessage("已从收藏夹移除", "取消收藏");
287 291 }
288 292 }
Modified XFENewsApplication/Views/NewsViewPage.xaml +35 -3
@@ -7,6 +7,7 @@
7 7 xmlns:coremodel="using:XFENewsApplication.Core.Models"
8 8 xmlns:model="using:XFENewsApplication.Models"
9 9 xmlns:converter="using:XFEExtension.NetCore.WinUIHelper.Utilities.Converter"
10 xmlns:innerconverter="using:XFENewsApplication.Utilities.Converter"
10 11 xmlns:selector="using:XFENewsApplication.Utilities.Selector"
11 12 xmlns:controls="using:CommunityToolkit.WinUI.Controls"
12 13 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -14,9 +15,27 @@
14 15 mc:Ignorable="d">
15 16 <Page.Resources>
16 17 <converter:URLImageConverter x:Key="URLImageConverter"/>
18 <converter:StringFormatConverter x:Key="StringFormatConverter"/>
19 <innerconverter:StringEmptyVisibilityConverter x:Key="StringEmptyVisibilityConverter"/>
17 20 <DataTemplate x:Key="TextDataTemplate" x:DataType="model:TextArticlePart">
18 21 <TextBlock Margin="0,10" Text="{x:Bind Content}" FontSize="{x:Bind FontSize}" FontWeight="{x:Bind FontWeight}" TextWrapping="Wrap" IsTextSelectionEnabled="True"/>
19 22 </DataTemplate>
23 <DataTemplate x:Key="AuthorDataTemplate" x:DataType="model:AuthorArticlePart">
24 <Border Padding="10,10,16,10" Margin="0,24" HorizontalAlignment="Stretch" Background="{ThemeResource ControlFillColorDefault}" CornerRadius="8">
25 <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
26 <PersonPicture Width="40" Height="40" Margin="10,0,0,0" ProfilePicture="{x:Bind Content}" VerticalAlignment="Center"/>
27 <TextBlock Margin="10,0,0,0" Text="{x:Bind ProviderName}" FontSize="18" FontWeight="Bold" VerticalAlignment="Center"/>
28 <Rectangle Margin="22,0" Fill="{ThemeResource TextFillColorDisabled}" Width="1" RadiusY="0" RadiusX="0" VerticalAlignment="Stretch"/>
29 <TextBlock Text="{x:Bind AuthorName}" FontSize="18" VerticalAlignment="Center"/>
30 <TextBlock Margin="4,0,0,0" Text="{x:Bind PublishDateTime, Converter={StaticResource StringFormatConverter}, ConverterParameter='的报道 · (0)'}" FontSize="18" VerticalAlignment="Center" Foreground="{ThemeResource TextFillColorDisabled}"/>
31 <StackPanel Visibility="{x:Bind ReadTime, Converter={StaticResource StringEmptyVisibilityConverter}, FallbackValue=Collapsed}" Orientation="Horizontal" VerticalAlignment="Center" Margin="8,0,0,0" Spacing="8">
32 <TextBlock Text="·" FontSize="18" VerticalAlignment="Center" Foreground="{ThemeResource TextFillColorDisabled}"/>
33 <FontIcon Glyph="&#xE916;" FontSize="18" VerticalAlignment="Center" Foreground="#13A10E"/>
34 <TextBlock Text="{x:Bind ReadTime, Converter={StaticResource StringFormatConverter}, ConverterParameter='阅读时间 (0) 分钟'}" FontSize="18" VerticalAlignment="Center" Foreground="#13A10E"/>
35 </StackPanel>
36 </StackPanel>
37 </Border>
38 </DataTemplate>
20 39 <DataTemplate x:Key="TitleDataTemplate" x:DataType="model:TitleArticlePart">
21 40 <StackPanel Margin="0,24,0,10" Orientation="Vertical" Spacing="10">
22 41 <Rectangle Fill="#2169EB" Height="10" Width="80" RadiusY="0" RadiusX="0" HorizontalAlignment="Left"/>
@@ -35,9 +54,21 @@
35 54 <ContentPresenter Content="{x:Bind CustomTextBlock}"/>
36 55 </DataTemplate>
37 56 <DataTemplate x:Key="ImageDataTemplate" x:DataType="model:ImageArticlePart">
38 <Image Source="{x:Bind Content, Converter={StaticResource URLImageConverter}}"/>
57 <Grid HorizontalAlignment="Center" Margin="0,10">
58 <Border Margin="1" CornerRadius="8" Background="{ThemeResource FocusStrokeColorOuterBrush}"/>
59 <Image MaxWidth="950" MaxHeight="950" Margin="10" Source="{x:Bind Content, Converter={StaticResource URLImageConverter}}"/>
60 <Border CornerRadius="8" Background="{ThemeResource AcrylicInAppFillColorDefaultBrush}">
61 <StackPanel Orientation="Vertical" Spacing="10">
62 <Image Margin="10,10,10,0" MaxWidth="950" MaxHeight="950" Source="{x:Bind Content, Converter={StaticResource URLImageConverter}}"/>
63 <StackPanel Margin="2,0" Orientation="Horizontal" Spacing="10" Visibility="{x:Bind From, Converter={StaticResource StringEmptyVisibilityConverter}}">
64 <TextBlock Text="└─" FontSize="28" Foreground="#2169EB"/>
65 <TextBlock Text="{x:Bind From}" FontSize="12" VerticalAlignment="Center"/>
66 </StackPanel>
67 </StackPanel>
68 </Border>
69 </Grid>
39 70 </DataTemplate>
40 <selector:ArticleDataTemplateSelector x:Key="ArticleDataTemplateSelector" DefaultDataTemplate="{StaticResource TextDataTemplate}" TextDataTemplate="{StaticResource TextDataTemplate}" ImageDataTemplate="{StaticResource ImageDataTemplate}" TitleDataTemplate="{StaticResource TitleDataTemplate}" QuoteDataTemplate="{StaticResource QuoteDataTemplate}" CustomTextDataTemplate="{StaticResource CustomTextDataTemplate}"/>
71 <selector:ArticleDataTemplateSelector x:Key="ArticleDataTemplateSelector" DefaultDataTemplate="{StaticResource TextDataTemplate}" TextDataTemplate="{StaticResource TextDataTemplate}" ImageDataTemplate="{StaticResource ImageDataTemplate}" TitleDataTemplate="{StaticResource TitleDataTemplate}" QuoteDataTemplate="{StaticResource QuoteDataTemplate}" CustomTextDataTemplate="{StaticResource CustomTextDataTemplate}" AuthorDataTemplate="{StaticResource AuthorDataTemplate}"/>
41 72 </Page.Resources>
42 73
43 74 <Grid>
@@ -80,7 +111,7 @@
80 111 </Grid>
81 112 </Border>
82 113 <controls:ContentSizer Grid.Column="1" TargetControl="{x:Bind sideBar}" CornerRadius="4"/>
83 <Border Grid.Column="2" MinWidth="300">
114 <Border Grid.Column="2" MinWidth="500">
84 115 <Grid>
85 116 <Grid.RowDefinitions>
86 117 <RowDefinition/>
@@ -91,6 +122,7 @@
91 122 <Style TargetType="ListViewItem">
92 123 <Setter Property="MaxWidth" Value="950"/>
93 124 <Setter Property="Margin" Value="0"/>
125 <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
94 126 </Style>
95 127 </ListView.ItemContainerStyle>
96 128 </ListView>