Skip to content

Commit

Permalink
Refactoring songlist display
Browse files Browse the repository at this point in the history
Creating the ViewModel abstract of NeteaseSong and using it to display songlist
  • Loading branch information
Clrs17 committed Jul 9, 2024
1 parent 99f05ee commit 46a5fb8
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using HyPlayer.NeteaseProvider.Constants;
using HyPlayer.NeteaseProvider.Constants;
using HyPlayer.PlayCore.Abstraction.Interfaces.ProvidableItem;
using HyPlayer.PlayCore.Abstraction.Models.Containers;
using HyPlayer.PlayCore.Abstraction.Models.Resources;
using HyPlayer.PlayCore.Abstraction.Models.SingleItems;
using System.Diagnostics.Contracts;

namespace HyPlayer.NeteaseProvider.Models;

Expand All @@ -18,7 +19,7 @@ public class NeteaseSong : SingleSongBase, IHasTranslation, IHasCover
public int TrackNumber { get; set; }
public string? CoverUrl { get; set; }

public required List<PersonBase>? Artists { get; init; }
public List<PersonBase>? Artists { get; set; } = new() { };

public override Task<List<PersonBase>?> GetCreatorsAsync(CancellationToken ctk = new())
{
Expand All @@ -28,6 +29,6 @@ public class NeteaseSong : SingleSongBase, IHasTranslation, IHasCover
public string? Translation { get; set; }
public Task<ImageResourceBase?> GetCoverAsync(ImageResourceQualityTag? qualityTag = null, CancellationToken ctk = new())
{
return Task.FromResult<ImageResourceBase?>(new NeteaseImageResource() { Url = CoverUrl, QualityTag = qualityTag});
return Task.FromResult<ImageResourceBase?>(new NeteaseImageResource() { Url = CoverUrl, QualityTag = qualityTag });
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace HyPlayer.PlayCore.Abstraction.Models;
namespace HyPlayer.PlayCore.Abstraction.Models;

public abstract class ProvidableItemBase
{
public required string Name { get; set; }
public string Name { get; set; } = "";
public string ItemId => $"{ProviderId}{TypeId}{ActualId}";
public abstract string ProviderId { get; }
public abstract string TypeId { get; }
public required string ActualId { get; set; }
public string ActualId { get; set; } = "0000";
}

public interface IProvidableItem { }
13 changes: 9 additions & 4 deletions src/HyPlayer.App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
<None Remove="Views\Controls\App\DesktopNavigationView.xaml" />
<None Remove="Views\Controls\App\HomePageHeaderImage.xaml" />
<None Remove="Views\Controls\Netease\SongListView\SongListView.xaml" />
<None Remove="Views\Controls\PlayBar\PlayBar.xaml" />
<None Remove="Views\Controls\Search\SearchBox.xaml" />
<None Remove="Views\Controls\SingleSongView\SingleSongView.xaml" />
<None Remove="Views\Pages\ErrorPage.xaml" />
<None Remove="Views\Pages\HomePage.xaml" />
<None Remove="Views\Pages\PlaylistPage.xaml" />
Expand All @@ -52,6 +54,7 @@
<ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="7.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CommunityToolkit.WinUI.Behaviors" Version="8.0.240109" />
<PackageReference Include="CommunityToolkit.WinUI.Notifications" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Animations" Version="7.1.2" />
Expand Down Expand Up @@ -107,6 +110,12 @@
<None Update="Controls\App\SelectableTextBox.xaml">
<Generator>MSBuild:Compile</Generator>
</None>
<Page Update="Views\Controls\SingleSongView\SingleSongView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\Controls\PlayBar\PlayBar.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Resources\Styles\FontSizes.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down Expand Up @@ -161,9 +170,5 @@
<ProjectReference Include="..\..\refs\HyPlayer.NeteaseProvider\HyPlayer.NeteaseProvider\HyPlayer.NeteaseProvider.csproj" />
<ProjectReference Include="..\..\refs\HyPlayer.PlayCore\HyPlayer.PlayCore\HyPlayer.PlayCore.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Views\Controls\PlayBar\" />
</ItemGroup>

</Project>
50 changes: 50 additions & 0 deletions src/HyPlayer.App/ViewModels/SongViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HyPlayer.Interfaces.ViewModels;
using HyPlayer.NeteaseProvider.Models;
using HyPlayer.PlayCore.Abstraction.Models.Containers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HyPlayer.ViewModels
{
public partial class SongViewModel : ObservableObject, IScopedViewModel
{
private readonly NeteaseProvider.NeteaseProvider _provider;

[ObservableProperty] public NeteaseSong _ncmSong;
[ObservableProperty] private string? _songName;
[ObservableProperty] private string? _songId;
[ObservableProperty] private TimeSpan? _duration;
[ObservableProperty] private Uri _coverUri;
[ObservableProperty] private List<PersonBase>? _artists;
[ObservableProperty] private AlbumBase? _album;
[ObservableProperty] private int _loadIndex = -1;

public SongViewModel(NeteaseProvider.NeteaseProvider provider)
{
_provider = provider;
}
[RelayCommand]
public void GetSongInfo()
{
if (NcmSong == null)
{
SongName = "Song0";
Duration = new TimeSpan(0);
CoverUri = new Uri("ms-appx:///Assets/Images/StoreLogo.png");
return;
}

SongName = NcmSong.Name;
Duration = new TimeSpan(NcmSong.Duration);
CoverUri = new Uri(NcmSong.CoverUrl ?? "ms-appx:///Assets/Images/StoreLogo.png");
Artists = NcmSong.Artists ?? new List<PersonBase>();
SongId = NcmSong.ActualId;
Album = NcmSong.Album;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:HyPlayer.Views.Controls.Netease.SongListView"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:netease="using:HyPlayer.NeteaseProvider.Models"
xmlns:netease="using:HyPlayer.NeteaseProvider.Models"
xmlns:singlesongview="using:HyPlayer.Views.Controls.SingleSongView"
mc:Ignorable="d">

<Grid>
Expand Down Expand Up @@ -98,33 +99,9 @@
</ListView.ContextFlyout>
<ListView.ItemTemplate>
<DataTemplate x:DataType="netease:NeteaseSong">
<Grid Margin="12,8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="45" />
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Text="{x:Bind Name}" />
<TextBlock
Grid.Column="1"
VerticalAlignment="Center"
Foreground="{ThemeResource SystemControlForegroundBaseMediumBrush}"
Text="{x:Bind Artists, Converter={StaticResource ArtistListToStringConverter}}" />
<TextBlock
Grid.Column="2"
VerticalAlignment="Center"
Foreground="{ThemeResource SystemControlForegroundBaseMediumBrush}"
Text="{x:Bind Album.Name}" />
<Button
Grid.Column="3"
Height="32"
VerticalAlignment="Center"
Background="Transparent"
BorderBrush="Transparent">
<FontIcon Glyph="&#xE10C;" />
</Button>
</Grid>
<singlesongview:SingleSongView Loaded="SingleSongView_Loaded" >

</singlesongview:SingleSongView>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace HyPlayer.Views.Controls.Netease.SongListView
{
public sealed partial class SongListView : UserControl
{
private int loadIndex = 0;
public static readonly DependencyProperty ListSourceProperty = DependencyProperty.Register(
"ListSource", typeof(string),
typeof(SongListView),
Expand Down Expand Up @@ -84,5 +85,10 @@ public async void ScrollToIndex(int index)
SongContainer.SelectedItem = ViewModel.SongsList[index];
await SongContainer.SmoothScrollIntoViewWithIndexAsync(index, ScrollItemPlacement.Top, false, true);
}

private void SingleSongView_Loaded(object sender, RoutedEventArgs e)
{
(sender as SingleSongView.SingleSongView).SetSong(ViewModel.SongsList[loadIndex++], loadIndex);
}
}
}
14 changes: 14 additions & 0 deletions src/HyPlayer.App/Views/Controls/PlayBar/PlayBar.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<UserControl
x:Class="HyPlayer.Views.Controls.PlayBar.PlayBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HyPlayer.Views.Controls.PlayBar"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>

</Grid>
</UserControl>
28 changes: 28 additions & 0 deletions src/HyPlayer.App/Views/Controls/PlayBar/PlayBar.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace HyPlayer.Views.Controls.PlayBar
{
public sealed partial class PlayBar : UserControl
{
public PlayBar()
{
this.InitializeComponent();
}
}
}
65 changes: 65 additions & 0 deletions src/HyPlayer.App/Views/Controls/SingleSongView/SingleSongView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<local:SingleSongViewBase
x:Class="HyPlayer.Views.Controls.SingleSongView.SingleSongView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HyPlayer.Views.Controls.SingleSongView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
mc:Ignorable="d">

<Grid>
<Grid Margin="12,8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5" />
<ColumnDefinition Width="55" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="45" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="Index"
Text="{x:Bind ViewModel.LoadIndex}"
FontSize="16"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="-15,0,0,0"
FontWeight="Bold"/>
<controls:ImageEx
x:Name="SongCoverImage"
Width="50"
Height="50"
Stretch="UniformToFill"
EnableLazyLoading="True"
IsCacheEnabled="False"
Margin="6,0,0,0"
CornerRadius="9"
Grid.Column="1"
>
<controls:ImageEx.Source>
<BitmapImage UriSource="{x:Bind ViewModel.CoverUri}" />
</controls:ImageEx.Source>
</controls:ImageEx>
<TextBlock Margin="5,0,0,0" Grid.Column="2" VerticalAlignment="Center" Text="{x:Bind ViewModel.SongName}" />
<TextBlock
Grid.Column="3"
VerticalAlignment="Center"
Foreground="{ThemeResource SystemControlForegroundBaseMediumBrush}"
Text="{x:Bind ViewModel.Artists, Converter={StaticResource ArtistListToStringConverter}}" />
<TextBlock
Grid.Column="4"
VerticalAlignment="Center"
Foreground="{ThemeResource SystemControlForegroundBaseMediumBrush}"
Text="{x:Bind ViewModel.Album.Name}" />
<Button
Grid.Column="5"
Height="32"
VerticalAlignment="Center"
Background="Transparent"
BorderBrush="Transparent">
<FontIcon Glyph="&#xE10C;" />
</Button>
</Grid>
</Grid>
</local:SingleSongViewBase>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using HyPlayer.Interfaces.Views;
using HyPlayer.NeteaseProvider.Models;
using HyPlayer.ViewModels;
using HyPlayer.Views.Controls.Netease.SongListView;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace HyPlayer.Views.Controls.SingleSongView
{
public sealed partial class SingleSongView : SingleSongViewBase
{
public static readonly DependencyProperty SongProperty = DependencyProperty.Register(
"DisplaySong", typeof(NeteaseSong),
typeof(SingleSongView),
new PropertyMetadata(null));
public NeteaseSong DisplaySong
{
get => ((NeteaseSong)GetValue(SongProperty));
set
{
SetValue(SongProperty, value);
}
}

public SingleSongView()
{
this.InitializeComponent();
}
public void SetSong(NeteaseSong song, int loadIndex = -1)
{
ViewModel.NcmSong = song;
ViewModel.GetSongInfo();
ViewModel.LoadIndex=loadIndex; ;
Bindings.Update();
}
}
public class SingleSongViewBase : ReactiveControlBase<SongViewModel>
{

}
}

0 comments on commit 46a5fb8

Please sign in to comment.