Skip to content

Commit

Permalink
CoreChanged: 添加本地艺术家、专辑管理
Browse files Browse the repository at this point in the history
  • Loading branch information
Miaoyww committed Aug 27, 2024
1 parent 6b2bae2 commit dd5b13e
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 34 deletions.
2 changes: 1 addition & 1 deletion NonsPlayer.Core/Contracts/Models/Music/IAlbum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IAlbum : IMusicModel
{
[JsonPropertyName("create_date")] public DateTime CreateDate { get; set; }
[JsonPropertyName("description")] public string Description { get; set; }
[JsonPropertyName("musics")] public IMusic[] Musics { get; set; }
[JsonPropertyName("musics")] public HashSet<IMusic> Songs { get; set; }
[JsonPropertyName("artists")] public IArtist[] Artists { get; set; }
[JsonIgnore] public string ArtistsName => string.Join("/", Artists.Select(x => x.Name));
[JsonPropertyName("collection_count")] public int CollectionCount { set; get; }
Expand Down
3 changes: 2 additions & 1 deletion NonsPlayer.Core/Contracts/Models/Music/IArtist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ namespace NonsPlayer.Core.Contracts.Models.Music;
public interface IArtist : IMusicModel
{
[JsonPropertyName("desc")] public string Description { get; set; }
[JsonPropertyName("musics")] public List<IMusic> HotMusics { get; set; }
[JsonPropertyName("musics")] public HashSet<IMusic> Songs { get; set; }
[JsonPropertyName("music_count")] public int MusicCount { get; set; }
[JsonPropertyName("trans")] public string Trans { get; set; }
[JsonIgnore] IAdapter Adapter { get; set; }

}
1 change: 1 addition & 0 deletions NonsPlayer.Core/Contracts/Models/Music/IMusic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NonsPlayer.Core.Contracts.Models.Nons;
using NonsPlayer.Core.Enums;
using NonsPlayer.Core.Exceptions;
using NonsPlayer.Core.Models;
using NonsPlayer.Core.Nons;

namespace NonsPlayer.Core.Contracts.Models.Music;
Expand Down
18 changes: 17 additions & 1 deletion NonsPlayer.Core/Models/LocalAlbum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,25 @@ public class LocalAlbum: IAlbum
public string AvatarUrl { get; set; }
public DateTime CreateDate { get; set; }
public string Description { get; set; }
public IMusic[] Musics { get; set; }
public HashSet<IMusic> Songs { get; set; }
public IArtist[] Artists { get; set; }
public int CollectionCount { get; set; }
public int TrackCount { get; set; }
public IAdapter Adapter { get; set; }

public int GetHashCode()

Check warning on line 21 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

'LocalAlbum.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 21 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

'LocalAlbum.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 21 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

'LocalAlbum.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 21 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

'LocalAlbum.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
{
return Name.GetHashCode();
}

public bool Equals(object obj)

Check warning on line 26 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

'LocalAlbum.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 26 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

'LocalAlbum.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 26 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

'LocalAlbum.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 26 in NonsPlayer.Core/Models/LocalAlbum.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

'LocalAlbum.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}

LocalAlbum other = (LocalAlbum)obj;
return Name.Equals(other.Name);
}
}
20 changes: 18 additions & 2 deletions NonsPlayer.Core/Models/LocalArtist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,32 @@

namespace NonsPlayer.Core.Models;

public class LocalArtist: IArtist
public class LocalArtist : IArtist
{
public string Id { get; set; }
public string Md5 { get; set; }
public string Name { get; set; }
public string ShareUrl { get; set; }
public string AvatarUrl { get; set; }
public string Description { get; set; }
public List<IMusic> HotMusics { get; set; }
public HashSet<IMusic> Songs { get; set; }
public int MusicCount { get; set; }
public string Trans { get; set; }
public IAdapter Adapter { get; set; }

public int GetHashCode()

Check warning on line 19 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

'LocalArtist.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 19 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

'LocalArtist.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 19 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

'LocalArtist.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 19 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

'LocalArtist.GetHashCode()' hides inherited member 'object.GetHashCode()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
{
return Name.GetHashCode();
}

public bool Equals(object obj)

Check warning on line 24 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

'LocalArtist.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 24 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

'LocalArtist.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 24 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Release, x64)

'LocalArtist.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 24 in NonsPlayer.Core/Models/LocalArtist.cs

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

'LocalArtist.Equals(object)' hides inherited member 'object.Equals(object?)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}

LocalArtist other = (LocalArtist)obj;
return Name.Equals(other.Name);
}
}
14 changes: 12 additions & 2 deletions NonsPlayer.Core/Models/LocalMusic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,20 @@ public void Init()
Md5 = track.GetHashCode().ToString();
Id = $"{Name}_{Md5}";
Url = track.Path;
Album = new LocalAlbum() { Name = track.Album, Id = $"{track.Album}_{Md5}", AvatarUrl = Url, };
Album = new LocalAlbum()
{
Name = track.Album,
Id = $"{track.Album}_{Md5}",
AvatarUrl = Url,
Songs = [this]
};
Artists =
[
new LocalArtist() { Name = track.Artist, Id = $"{track.Artist}_{Md5}" }
new LocalArtist()
{
Name = track.Artist, Id = $"{track.Artist}_{Md5}",
Songs = [this]
}
];
Duration = TimeSpan.FromSeconds(track.Duration);
}
Expand Down
1 change: 1 addition & 0 deletions NonsPlayer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public App()
services.AddTransient<LocalQueueCardViewModel>();
services.AddTransient<LocalNewFolderCardViewModel>();
services.AddTransient<LocalFolderItemViewModel>();
services.AddTransient<LocalArtistItemViewModel>();
#endregion
Expand Down
25 changes: 25 additions & 0 deletions NonsPlayer/Components/ViewModels/Local/LocalArtistItemViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using NonsPlayer.Core.Models;
using NonsPlayer.Core.Utils;
using NonsPlayer.Helpers;
using NonsPlayer.Services;
using System.Diagnostics;
using Windows.Storage;
using Windows.Storage.Pickers;
using FileAttributes = Windows.Storage.FileAttributes;

namespace NonsPlayer.Components.ViewModels;

[INotifyPropertyChanged]
public partial class LocalArtistItemViewModel
{
[ObservableProperty] private string name;
[ObservableProperty] private string count;

public void Init(LocalArtist artist)
{
Name = artist.Name;
Count = artist.Songs.Count.ToString();
}
}
34 changes: 33 additions & 1 deletion NonsPlayer/Components/ViewModels/MusicListItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using NonsPlayer.Helpers;
using NonsPlayer.ViewModels;
using Microsoft.UI.Xaml.Controls;
using NonsPlayer.Services;

namespace NonsPlayer.Components.ViewModels;

Expand All @@ -33,10 +34,10 @@ public partial class MusicListItemViewModel : ObservableObject
[ObservableProperty] private Visibility coverVisibility = Visibility.Collapsed;
[ObservableProperty] private Visibility likeVisibility = Visibility.Collapsed;
[ObservableProperty] private Visibility localVisibility = Visibility.Collapsed;

[ObservableProperty]
private SolidColorBrush titleColor = Application.Current.Resources["CommonTextColor"] as SolidColorBrush;

private LocalService localService = App.GetService<LocalService>();
public ObservableCollection<MetadataItem> ArtistsMetadata = new();

[RelayCommand]
Expand All @@ -58,6 +59,37 @@ public async void Init(IMusic music)
{
if (!((LocalMusic)music).IsInit) ((LocalMusic)music).Init();
LocalVisibility = Visibility.Visible;
if (music.Artists != null)
{
foreach (LocalArtist artist in music.Artists)
{
var existingArtist = localService.Artists.FirstOrDefault(a => a.Equals(artist));
if (existingArtist != null)
{
existingArtist.Songs.Add(music);
}
else
{
localService.Artists.Add(artist);
}
}
}

if (music.Album != null)
{
var existingAlbum = localService.Albums.FirstOrDefault(a => a.Equals(music.Album));
if (existingAlbum != null)
{
existingAlbum.Songs.Add(music);
}
else
{
ServiceHelper.DispatcherQueue.TryEnqueue(() =>
{
localService.Albums.Add((LocalAlbum)music.Album);
});
}
}
}

Name = Music.Name;
Expand Down
14 changes: 14 additions & 0 deletions NonsPlayer/Components/Views/Local/LocalArtistItem.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl
x:Class="NonsPlayer.Components.Views.LocalArtistItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<StackPanel>
<TextBlock Text="{x:Bind ViewModel.Name, Mode=OneWay}"/>
<TextBlock Text="{x:Bind ViewModel.Count, Mode=OneWay}"/>
</StackPanel>
</Grid>
</UserControl>
39 changes: 39 additions & 0 deletions NonsPlayer/Components/Views/Local/LocalArtistItem.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.WinUI.UI.Controls;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using NonsPlayer.Components.Models;
using NonsPlayer.Components.ViewModels;
using NonsPlayer.Core.Contracts.Models.Music;
using NonsPlayer.Core.Models;
using NonsPlayer.Core.Services;
using NonsPlayer.Helpers;
using NonsPlayer.ViewModels;
using System.Collections.ObjectModel;
using Windows.UI.Core;

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

namespace NonsPlayer.Components.Views;

[INotifyPropertyChanged]
public sealed partial class LocalArtistItem : UserControl
{
public LocalArtistItem()
{
ViewModel = App.GetService<LocalArtistItemViewModel>();
ProtectedCursor = InputCursor.CreateFromCoreCursor(new CoreCursor(CoreCursorType.Hand, 0));
InitializeComponent();
}

public LocalArtist Artist
{
set => ViewModel.Init(value);
}

public LocalArtistItemViewModel ViewModel { get; }
}
5 changes: 5 additions & 0 deletions NonsPlayer/NonsPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@
<None Remove="Components\Views\LocalCa.xaml" />
<None Remove="Components\Views\LocalProperties.xaml" />
<None Remove="Components\Views\Local\AddFolderDialog.xaml" />
<None Remove="Components\Views\Local\LocalArtistItem.xaml" />
<None Remove="Components\Views\Local\LocalFolderItem.xaml" />
<None Remove="Components\Views\Local\LocalMusicCard.xaml" />
<None Remove="Components\Views\Local\LocalQueueCard.xaml" />
<None Remove="Views\Local\LocalMusicLibPage.xaml" />
<None Remove="Views\Local\LocalQueuePage.xaml" />
</ItemGroup>
<ItemGroup>
<Page Update="Components\Views\Local\LocalArtistItem.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<SubType>Designer</SubType>
</Page>
<Page Update="Components\Views\Local\LocalFolderItem.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
<SubType>Designer</SubType>
Expand Down
16 changes: 13 additions & 3 deletions NonsPlayer/Services/LocalService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json.Linq;
using NonsPlayer.Components.Models;
using NonsPlayer.Core.Contracts.Models.Music;
using NonsPlayer.Core.Models;
using NonsPlayer.Core.Services;
using System.Collections.ObjectModel;
Expand All @@ -13,14 +14,19 @@ public class LocalService
#region 事件注册

public delegate void LocalFolderModelEventHandler();

public event LocalFolderModelEventHandler? LocalFolderChanged;

#endregion

private const string _dataKey = "local_dictionaries.json";
public ObservableCollection<LocalFolderModel> Directories = new();
public HashSet<LocalMusic> Songs = new();
public HashSet<LocalArtist> Artists = new();
public HashSet<LocalAlbum> Albums = new();

private FileService FileService = App.GetService<FileService>();

public bool TryAddDirection(string path)
{
if (string.IsNullOrEmpty(path)) return false;
Expand All @@ -38,8 +44,10 @@ public bool TryAddSong(LocalMusic song)
{
if (songItem.FilePath.Equals(song.FilePath)) return false;
}

LocalFolderChanged?.Invoke();
return Songs.Add(song);
var result = Songs.Add(song);
return result;
}

public void AddSongs(IEnumerable<LocalMusic> songs)
Expand All @@ -48,6 +56,7 @@ public void AddSongs(IEnumerable<LocalMusic> songs)
{
TryAddSong(inputSongItem);
}

LocalFolderChanged?.Invoke();
}

Expand Down Expand Up @@ -90,13 +99,14 @@ public void LoadFromFile()
Directories.Clear();
var index = 0;
foreach (var item in value)
{
{
index++;
Directories.Add(new LocalFolderModel(
(item)["path"].ToString(),
index.ToString("D2")
));
}

LocalFolderChanged?.Invoke();
}
}
Expand Down
Loading

0 comments on commit dd5b13e

Please sign in to comment.