Skip to content

Commit

Permalink
修复综合搜索结果为空的问题 (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy authored Dec 28, 2024
1 parent 9f4514d commit d6fc086
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -504,46 +504,59 @@
Grid.Row="1"
HorizontalAlignment="Left"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.Title}" />
<StackPanel
Grid.Row="2"
Orientation="Horizontal"
Spacing="12">
<StackPanel
VerticalAlignment="Center"
Orientation="Horizontal"
Spacing="4"
ToolTipService.ToolTip="{ext:Locale Name=PlayCount}">
<ic:SymbolIcon
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Spacing="12">
<StackPanel
VerticalAlignment="Center"
FontSize="10"
Foreground="{ThemeResource AccentTextFillColorTertiaryBrush}"
Symbol="PlayCircle" />
<TextBlock
Orientation="Horizontal"
Spacing="4"
ToolTipService.ToolTip="{ext:Locale Name=PlayCount}">
<ic:SymbolIcon
VerticalAlignment="Center"
FontSize="10"
Foreground="{ThemeResource AccentTextFillColorTertiaryBrush}"
Symbol="PlayCircle" />
<TextBlock
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorTertiaryBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.PlayCount, Converter={StaticResource CountConverter}}"
TextLineBounds="Tight" />
</StackPanel>
<StackPanel
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorTertiaryBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.PlayCount, Converter={StaticResource CountConverter}}"
TextLineBounds="Tight" />
Orientation="Horizontal"
Spacing="4"
ToolTipService.ToolTip="{ext:Locale Name=DanmakuCount}"
Visibility="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.DanmakuCount, Converter={StaticResource ObjectToVisibilityConverter}}">
<ic:SymbolIcon
VerticalAlignment="Center"
FontSize="10"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Symbol="TextBulletList" />
<TextBlock
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorTertiaryBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.DanmakuCount, Converter={StaticResource CountConverter}}"
TextLineBounds="Tight" />
</StackPanel>
</StackPanel>
<StackPanel
<TextBlock
Grid.Column="1"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Orientation="Horizontal"
Spacing="4"
ToolTipService.ToolTip="{ext:Locale Name=DanmakuCount}"
Visibility="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.DanmakuCount, Converter={StaticResource ObjectToVisibilityConverter}}">
<ic:SymbolIcon
VerticalAlignment="Center"
FontSize="10"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Symbol="TextBulletList" />
<TextBlock
VerticalAlignment="Center"
Foreground="{ThemeResource TextFillColorTertiaryBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.DanmakuCount, Converter={StaticResource CountConverter}}"
TextLineBounds="Tight" />
</StackPanel>
</StackPanel>
Foreground="{ThemeResource TextFillColorTertiaryBrush}"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.PublishRelativeTime}"
TextTrimming="CharacterEllipsis"
TextWrapping="NoWrap"
Visibility="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=ViewModel.PublishRelativeTime, Converter={StaticResource ObjectToVisibilityConverter}}" />
</Grid>
</Grid>
</Grid>
</base:CardControl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed partial class VideoSearchSectionDetailViewModel
private readonly ILogger<VideoSearchSectionDetailViewModel> _logger;

private bool _canRequest;
private string _offset;
private int? _currentPage;
private string _keyword;
private bool _isPreventLoadMore;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public VideoSearchSectionDetailViewModel(
{
_service = service;
_logger = this.Get<ILogger<VideoSearchSectionDetailViewModel>>();
Sorts = Enum.GetValues<ComprehensiveSearchSortType>().ToList();
Sorts = [.. Enum.GetValues<ComprehensiveSearchSortType>()];
}

/// <inheritdoc/>
public void Initialize(string keyword, SearchPartition partition)
{
Clear();
Sort = ComprehensiveSearchSortType.Play;
Sort = ComprehensiveSearchSortType.Default;
_keyword = keyword;
}

Expand All @@ -47,10 +47,10 @@ public void Clear()
this.Get<DispatcherQueue>().TryEnqueue(DispatcherQueuePriority.Low, Items.Clear);
}

internal void SetFirstPageData(IReadOnlyList<VideoInformation> videos, string initialOffset)
internal void SetFirstPageData(IReadOnlyList<VideoInformation> videos, int? initialOffset)
{
_canRequest = true;
_offset = initialOffset;
_currentPage = initialOffset;
this.Get<DispatcherQueue>().TryEnqueue(DispatcherQueuePriority.Low, () =>
{
Items.Clear();
Expand Down Expand Up @@ -81,17 +81,17 @@ internal void SetFirstPageData(IReadOnlyList<VideoInformation> videos, string in
[RelayCommand]
private async Task LoadItemsAsync()
{
if (!_canRequest || IsEmpty || _isPreventLoadMore || IsLoading)
if (!_canRequest || IsEmpty || _isPreventLoadMore || IsLoading || _currentPage == null)
{
return;
}

try
{
IsLoading = true;
var (videos, _, nextOffset) = await _service.GetComprehensiveSearchResultAsync(_keyword, _offset, Sort);
_offset = nextOffset;
_canRequest = !string.IsNullOrEmpty(_offset);
var (videos, nextOffset) = await _service.GetComprehensiveSearchResultAsync(_keyword, _currentPage, Sort);
_currentPage = nextOffset;
_canRequest = _currentPage != null;
foreach (var item in videos)
{
if (Items.Any(p => p.Data.Equals(item)))
Expand Down Expand Up @@ -122,6 +122,7 @@ private async Task RefreshAsync()
this.Get<DispatcherQueue>().TryEnqueue(DispatcherQueuePriority.Low, async () =>
{
var keyword = _keyword;
_currentPage = 1;
Clear();
_keyword = keyword;
_canRequest = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,16 @@ private async Task SearchAsync(string keyword)
try
{
IsSearching = true;
var (videos, partitions, nextVideoOffset) = await _service.GetComprehensiveSearchResultAsync(keyword, sort: Richasy.BiliKernel.Models.ComprehensiveSearchSortType.Play);
var sections = new List<ISearchSectionDetailViewModel>()
var (videos, nextVideoOffset) = await _service.GetComprehensiveSearchResultAsync(keyword);
var sections = new List<ISearchSectionDetailViewModel>
{
CreateVideoSection(videos, nextVideoOffset),
CreateSection(SearchSectionType.Anime),
CreateSection(SearchSectionType.Cinema),
CreateSection(SearchSectionType.Live),
CreateSection(SearchSectionType.User),
CreateSection(SearchSectionType.Article),
};
if (partitions is not null)
{
foreach (var section in partitions)
{
var sec = CreateSection(section);
if (sec is not null)
{
sections.Add(sec);
}
}
}

Sections = [.. sections];
SelectSection(Sections.FirstOrDefault());
SectionInitialized?.Invoke(this, EventArgs.Empty);
Expand Down Expand Up @@ -129,17 +122,16 @@ private void SelectSection(ISearchSectionDetailViewModel section)
section.TryFirstLoadCommand.Execute(default);
}

private VideoSearchSectionDetailViewModel CreateVideoSection(IReadOnlyList<VideoInformation> videos, string offset)
private VideoSearchSectionDetailViewModel CreateVideoSection(IReadOnlyList<VideoInformation> videos, int? page)
{
var newSection = new VideoSearchSectionDetailViewModel(_service);
newSection.Initialize(Keyword, new SearchPartition(0, ResourceToolkit.GetLocalizedString(StringNames.Video)));
newSection.SetFirstPageData(videos, offset);
newSection.SetFirstPageData(videos, page);
return newSection;
}

private ISearchSectionDetailViewModel CreateSection(SearchPartition partition)
private ISearchSectionDetailViewModel CreateSection(SearchSectionType type)
{
var type = (SearchSectionType)partition.Id;
var sectionDetail = type switch
{
SearchSectionType.Anime or SearchSectionType.Cinema => (ISearchSectionDetailViewModel)new PgcSearchSectionDetailViewModel(_service),
Expand All @@ -149,6 +141,16 @@ private ISearchSectionDetailViewModel CreateSection(SearchPartition partition)
_ => default
};

var partition = type switch
{
SearchSectionType.Anime => new SearchPartition(7, ResourceToolkit.GetLocalizedString(StringNames.Anime)),
SearchSectionType.Cinema => new SearchPartition(8, ResourceToolkit.GetLocalizedString(StringNames.Cinema)),
SearchSectionType.Live => new SearchPartition(4, ResourceToolkit.GetLocalizedString(StringNames.Live)),
SearchSectionType.User => new SearchPartition(2, ResourceToolkit.GetLocalizedString(StringNames.User)),
SearchSectionType.Article => new SearchPartition(6, ResourceToolkit.GetLocalizedString(StringNames.Article)),
_ => default
};

sectionDetail?.Initialize(Keyword, partition);
return sectionDetail;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Libs/bili-kernel

0 comments on commit d6fc086

Please sign in to comment.