Skip to content

Commit 91b3fed

Browse files
committed
Episode #3
1 parent 549f437 commit 91b3fed

File tree

5 files changed

+41
-30
lines changed

5 files changed

+41
-30
lines changed
Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
11
using BangumiNet.Api.V0.Models;
22
using ReactiveUI.SourceGenerators;
33
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using System.Threading.Tasks;
46

57
namespace BangumiNet.ViewModels;
68

79
public partial class EpisodeListViewModel : ViewModelBase
810
{
9-
public EpisodeListViewModel(Paged_Episode? episodes)
11+
public EpisodeListViewModel(int? subjectId)
1012
{
13+
SubjectId = subjectId;
1114
EpisodeViewModels = [];
12-
PageItems = [];
13-
14-
AddEpisodes(episodes);
15+
Sources = [];
1516
}
1617

17-
public void AddEpisodes(Paged_Episode? episodes)
18+
private int lastOffset = 0;
19+
/// <param name="offset">分页 offset</param>
20+
/// <param name="limit">每页集数</param>
21+
/// <returns>是否已取得全部集数</returns>
22+
public async Task<bool?> LoadEpisodes(int? offset = null, int limit = 100)
1823
{
19-
if (episodes?.Data != null)
20-
for (int i = 0; i < episodes.Data.Count; i++)
21-
{
22-
if (episodes.Offset is int offset)
23-
if (PageItems.Contains(offset + 1)) continue;
24-
else PageItems.Add(offset + i);
25-
26-
EpisodeViewModels.Add(new(episodes.Data[i]));
27-
}
24+
offset ??= lastOffset;
25+
if (offset >= EpTotal) return true;
26+
lastOffset += limit;
27+
28+
var epPage = await ApiC.V0.Episodes.GetAsync(config =>
29+
{
30+
config.QueryParameters.Limit = limit;
31+
config.QueryParameters.Offset = 0;
32+
config.QueryParameters.Type = null;
33+
config.QueryParameters.SubjectId = SubjectId;
34+
});
35+
36+
if (epPage == null) return null;
37+
Sources.Add(epPage);
38+
EpTotal = epPage.Total;
39+
40+
if (epPage.Data is { } episodes)
41+
for (int i = 0; i < episodes.Count; i++)
42+
if (!EpisodeViewModels.Any(x => x.Id == episodes[i].Id))
43+
EpisodeViewModels.Add(new(episodes[i]));
44+
45+
return EpisodeViewModels.Count >= EpTotal;
2846
}
2947

48+
[Reactive] public partial ObservableCollection<Paged_Episode> Sources { get; set; }
3049
[Reactive] public partial ObservableCollection<EpisodeViewModel> EpisodeViewModels { get; set; }
31-
32-
public ObservableCollection<int> PageItems { get; }
50+
[Reactive] public partial int? SubjectId { get; set; }
51+
[Reactive] public partial int? EpTotal { get; set; }
3352
}

BangumiNet/ViewModels/SubjectViewModel.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public SubjectViewModel(Subject subject)
9797

9898
public void Init()
9999
{
100+
EpisodeListViewModel = new(Id);
101+
100102
OpenInNewWindowCommand = ReactiveCommand.Create(() => new SecondaryWindow() { Content = new SubjectView() { DataContext = this } }.Show());
101103
SearchGoogleCommand = ReactiveCommand.Create(() => Common.OpenUrlInBrowser(UrlProvider.GoogleQueryBase + WebUtility.UrlEncode(Name)));
102104
OpenInBrowserCommand = ReactiveCommand.Create(() => Common.OpenUrlInBrowser(Url ?? UrlProvider.BangumiTvSubjectUrlBase + Id));
@@ -112,17 +114,6 @@ public void Init()
112114
if (Rank == 0) Rank = null;
113115
}
114116

115-
public async Task LoadEpisodes()
116-
{
117-
EpisodeListViewModel = new(await ApiC.V0.Episodes.GetAsync(config =>
118-
{
119-
config.QueryParameters.Limit = 100;
120-
config.QueryParameters.Offset = 0;
121-
config.QueryParameters.Type = null;
122-
config.QueryParameters.SubjectId = Id;
123-
}));
124-
}
125-
126117
[Reactive] public partial object? Source { get; set; }
127118
[Reactive] public bool IsLegacy { get; set; }
128119
[Reactive] public partial int? CollectionTotal { get; set; }

BangumiNet/Views/EpisodeBadgeView.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
mc:Ignorable="d" d:DesignWidth="22" d:DesignHeight="22"
1010
x:DataType="vm:EpisodeViewModel"
1111
x:Class="BangumiNet.Views.EpisodeBadgeView">
12-
<Border Classes="Card" Background="#3F7FFF7F" MinWidth="22" CornerRadius="5">
12+
<Border Classes="Card" Background="#3F7FFF7F" MinWidth="22" CornerRadius="5"
13+
ToolTip.Tip="{Binding .,Converter={StaticResource NameCnConverter}}">
1314
<TextBlock Text="{Binding Sort}" HorizontalAlignment="Center"/>
1415
</Border>
1516
</UserControl>

BangumiNet/Views/SubjectView.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<TextBlock ToolTip.Tip="在看" IsVisible="{Binding Collection.Doing,Converter={StaticResource NullCvt}}"><ic:FluentIcon Icon="Tv"/><Run Text="{Binding Collection.Doing}"/></TextBlock>
3333
</StackPanel>
3434
<v:EpisodeListView DataContext="{Binding EpisodeListViewModel}"/>
35-
<TextBlock Text="{Binding Summary}" TextWrapping="Wrap"/>
35+
<TextBlock Text="{Binding Summary}" TextWrapping="Wrap" Margin="0,0,0,5"/>
3636
<v:TagListView DataContext="{Binding TagListViewModel}"/>
3737
<StackPanel Orientation="Horizontal" Spacing="5">
3838
<Button Classes="Icon" Command="{Binding OpenInNewWindowCommand}" Content="{ice:FluentIcon Icon=Open}" ToolTip.Tip="在新窗口显示"/>

BangumiNet/Views/SubjectView.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public SubjectView()
1919
if (fullSubject == null) return;
2020
dataContextChanges += 1;
2121
var vm = new SubjectViewModel(fullSubject);
22-
_ = vm.LoadEpisodes();
22+
_ = vm.EpisodeListViewModel?.LoadEpisodes();
2323
DataContext = vm;
2424
};
2525
}

0 commit comments

Comments
 (0)