More complete library implementation

This commit is contained in:
Royce551 2022-04-10 18:53:12 -05:00
parent 9e2e07dcbd
commit 7899b558f2
3 changed files with 88 additions and 34 deletions

View file

@ -13,11 +13,13 @@ namespace FRESHMusicPlayer.ViewModels
public class LibraryTabViewModel : ViewModelBase public class LibraryTabViewModel : ViewModelBase
{ {
public MainWindowViewModel MainWindowWm { get; set; } public MainWindowViewModel MainWindowWm { get; set; }
private Tab selectedTab;
public void Initialize(Tab selectedTab, string initialSearch = null) public void Initialize(Tab selectedTab, string initialSearch = null)
{ {
this.selectedTab = selectedTab;
// TODO: library changed event handling // TODO: library changed event handling
LoadLibrary(selectedTab); LoadLibrary();
if (initialSearch != null) if (initialSearch != null)
{ {
Thread.Sleep(10); Thread.Sleep(10);
@ -49,33 +51,45 @@ namespace FRESHMusicPlayer.ViewModels
get => selectedCategory; get => selectedCategory;
set set
{ {
selectedCategory = value; this.RaiseAndSetIfChanged(ref selectedCategory, value);
switch (selectedTab)
{
case Tab.Artists:
ShowTracksForArtist();
break;
case Tab.Albums:
ShowTracksForAlbum();
break;
case Tab.Playlists:
ShowTracksForPlaylist();
break;
}
} }
} }
public ObservableCollection<DatabaseTrack> ContentItems { get; set; } = new(); public ObservableCollection<DatabaseTrack> ContentItems { get; set; } = new();
public async void LoadLibrary(Tab selectedTab) public void LoadLibrary()
{ {
ContentInfo = null; ContentInfo = null;
switch (selectedTab) switch (selectedTab)
{ {
case Tab.Tracks: case Tab.Tracks:
await ShowTracks(); ShowTracks();
break; break;
case Tab.Artists: case Tab.Artists:
await ShowArtists(); ShowArtists();
break; break;
case Tab.Albums: case Tab.Albums:
await ShowAlbums(); ShowAlbums();
break; break;
case Tab.Playlists: case Tab.Playlists:
await ShowPlaylists(); ShowPlaylists();
break; break;
} }
} }
public async Task ShowTracks() public void ShowTracks()
{ {
SidebarWidth = 0; SidebarWidth = 0;
@ -86,19 +100,72 @@ namespace FRESHMusicPlayer.ViewModels
ContentInfo = $"{Resources.Tracks}: {ContentItems.Count} ・ {lengthString}"; ContentInfo = $"{Resources.Tracks}: {ContentItems.Count} ・ {lengthString}";
} }
public async Task ShowArtists() public void ShowArtists()
{ {
SidebarWidth = 222;
foreach (var thing in MainWindowWm.Library.Read("Artist"))
{
if (CategoryItems.Contains(thing.Artist)) continue;
CategoryItems.Add(thing.Artist);
}
} }
public async Task ShowAlbums() public void ShowAlbums()
{ {
SidebarWidth = 222;
foreach (var thing in MainWindowWm.Library.Read("Album"))
{
if (CategoryItems.Contains(thing.Album)) continue;
CategoryItems.Add(thing.Album);
}
} }
public async Task ShowPlaylists() public void ShowPlaylists()
{ {
SidebarWidth = 222;
var playlists = MainWindowWm.Library.Database.GetCollection<DatabasePlaylist>("playlists").Query().OrderBy("Name").ToList();
if (playlists.Count == 0) MainWindowWm.Library.CreatePlaylist("Liked");
foreach (var thing in playlists)
{
if (CategoryItems.Contains(thing.Name)) continue;
CategoryItems.Add(thing.Name);
}
}
public void ShowTracksForArtist()
{
ContentItems = new ObservableCollection<DatabaseTrack>(MainWindowWm.Library.ReadTracksForArtist(selectedCategory));
this.RaisePropertyChanged(nameof(ContentItems));
var lengthTimeSpan = TimeSpan.FromSeconds(ContentItems.Sum(x => x.Length));
var lengthString = lengthTimeSpan.Days != 0 ? lengthTimeSpan.ToString(@"d\:hh\:mm\:ss") : lengthTimeSpan.ToString(@"hh\:mm\:ss");
ContentInfo = $"{Resources.Tracks}: {ContentItems.Count} ・ {lengthString}";
}
public void ShowTracksForAlbum()
{
ContentItems = new ObservableCollection<DatabaseTrack>(MainWindowWm.Library.ReadTracksForAlbum(selectedCategory));
this.RaisePropertyChanged(nameof(ContentItems));
var lengthTimeSpan = TimeSpan.FromSeconds(ContentItems.Sum(x => x.Length));
var lengthString = lengthTimeSpan.Days != 0 ? lengthTimeSpan.ToString(@"d\:hh\:mm\:ss") : lengthTimeSpan.ToString(@"hh\:mm\:ss");
ContentInfo = $"{Resources.Tracks}: {ContentItems.Count} ・ {lengthString}";
}
public async void ShowTracksForPlaylist()
{
ContentItems.Clear();
List<DatabaseTrack> playlists = null; // this weird oddity can be removed when library api becomes asyncified
await Task.Run(() =>
{
playlists = MainWindowWm.Library.ReadTracksForPlaylist(selectedCategory);
});
ContentItems = new ObservableCollection<DatabaseTrack>(playlists);
this.RaisePropertyChanged(nameof(ContentItems));
var lengthTimeSpan = TimeSpan.FromSeconds(ContentItems.Sum(x => x.Length));
var lengthString = lengthTimeSpan.Days != 0 ? lengthTimeSpan.ToString(@"d\:hh\:mm\:ss") : lengthTimeSpan.ToString(@"hh\:mm\:ss");
ContentInfo = $"{Resources.Tracks}: {ContentItems.Count} ・ {lengthString}";
} }
} }
} }

View file

@ -383,29 +383,11 @@ namespace FRESHMusicPlayer.ViewModels
case Tab.Tracks: case Tab.Tracks:
return new LibraryTab().SetStuff(this, SelectedTab, null); return new LibraryTab().SetStuff(this, SelectedTab, null);
case Tab.Artists: case Tab.Artists:
return new UserControl return new LibraryTab().SetStuff(this, SelectedTab, null);
{
Content = new TextBlock
{
Text = "Artists Tab"
}
};
case Tab.Albums: case Tab.Albums:
return new UserControl return new LibraryTab().SetStuff(this, SelectedTab, null);
{
Content = new TextBlock
{
Text = "Albums Tab"
}
};
case Tab.Playlists: case Tab.Playlists:
return new UserControl return new LibraryTab().SetStuff(this, SelectedTab, null);
{
Content = new TextBlock
{
Text = "Playlists Tab"
}
};
case Tab.Import: case Tab.Import:
return new UserControl return new UserControl
{ {

View file

@ -11,8 +11,13 @@
</UserControl.DataContext> </UserControl.DataContext>
<DockPanel> <DockPanel>
<ListBox DockPanel.Dock="Left" Background="{DynamicResource SecondaryColor}" Width="{Binding SidebarWidth}" Items="{Binding CategoryItems}"> <ListBox DockPanel.Dock="Left" Background="{DynamicResource SecondaryColor}" Width="{Binding SidebarWidth}" Items="{Binding CategoryItems}" SelectedItem="{Binding SelectedCategory}" VirtualizationMode="Simple">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="10,1"/>
<Setter Property="Margin" Value="0"/>
</Style>
</ListBox.Styles>
</ListBox> </ListBox>
<DockPanel DockPanel.Dock="Bottom" LastChildFill="False"> <DockPanel DockPanel.Dock="Bottom" LastChildFill="False">