Skip to content

Commit

Permalink
(#86) Load albumart from cache faster in library view
Browse files Browse the repository at this point in the history
  • Loading branch information
Difegue committed Mar 29, 2024
1 parent a7e7bab commit 7b63a08
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Sources/Stylophone.Common/Services/AlbumArtService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public void Stop()
/// <param name="f">MpdFile to check for art</param>
/// <returns>True if the art is cached, false otherwise.</returns>
public async Task<bool> IsAlbumArtCachedAsync(IMpdFile f) => await _applicationStorageService.DoesFileExistAsync(Miscellaneous.GetFileIdentifier(f), "AlbumArt");
public async Task<bool> IsAlbumArtCachedAsync(string albumName) => await _applicationStorageService.DoesFileExistAsync(albumName, "AlbumArt");

/// <summary>
/// Queue up an AlbumViewModel for the service to grab its album art.
Expand Down Expand Up @@ -224,7 +225,7 @@ private QuantizedColor GetDominantColor(SKBitmap art)

private async Task SaveArtToFileAsync(string fileName, List<byte> data) => await _applicationStorageService.SaveDataToFileAsync(fileName, data.ToArray(), "AlbumArt");

private async Task<SKBitmap> LoadImageFromFile(string fileName)
internal async Task<SKBitmap> LoadImageFromFile(string fileName)
{
try
{
Expand Down
15 changes: 14 additions & 1 deletion Sources/Stylophone.Common/ViewModels/Items/AlbumViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
using MpcNET.Tags;
using MpcNET.Types;
using SkiaSharp;
using Stylophone.Common.Helpers;
using Stylophone.Common.Interfaces;
using Stylophone.Common.Services;
using Stylophone.Localization.Strings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks;
using System.Windows.Input;

Expand Down Expand Up @@ -180,6 +182,16 @@ private async Task PlayAlbum()
}
}

public async Task LoadAlbumArtFromCacheAsync()
{
// Try to show album art early if we have it in cache
var cacheName = Miscellaneous.EscapeFilename(Name);
if (await _albumArtService.IsAlbumArtCachedAsync(cacheName))
{
AlbumArt = SKImage.FromBitmap(await _albumArtService.LoadImageFromFile(cacheName));
}
}

/// <summary>
/// Load Album Data. You can either provide a MpcConnection object (for batch loading)
/// or leave as empty to automatically pick up a connection from the datasource.
Expand All @@ -196,7 +208,8 @@ public async Task LoadAlbumDataAsync()
public async Task LoadAlbumDataAsync(MpcConnection c)
{
IsDetailLoading = true;
AlbumArt = await _interop.GetPlaceholderImageAsync();
AlbumArt ??= await _interop.GetPlaceholderImageAsync();

try
{
var findReq = await c.SendAsync(new FindCommand(MpdTags.Album, Name));
Expand Down
7 changes: 7 additions & 0 deletions Sources/Stylophone.iOS/ViewModels/LibraryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ internal void LoadItems(IEnumerable<int> indexes, CancellationToken token = defa
// Albumart loads still use their own connections.
Task.Run(async () =>
{
// Try to show album artwork early if we have it in cache
foreach (var i in indexes)
{
var album = BackingCollection[i];
await album.LoadAlbumArtFromCacheAsync();
}
using (var c = await _mpdService.GetConnectionAsync(token))
foreach (var i in indexes)
{
Expand Down
7 changes: 7 additions & 0 deletions Sources/Stylophone/ViewModels/LibraryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public void RangesChanged(ItemIndexRange visibleRange, IReadOnlyList<ItemIndexRa
// Albumart loads still use their own connections.
Task.Run(async () =>
{
// Try to show album artwork early if we have it in cache
for (var i = visibleRange.FirstIndex; i < visibleRange.LastIndex + 1; i++) // Increment LastIndex by one to properly cover the visible range
{
var album = this[i];
await album.LoadAlbumArtFromCacheAsync();
}
using (var c = await _mpdService.GetConnectionAsync(token))
for (var i = visibleRange.FirstIndex; i < visibleRange.LastIndex + 1; i++) // Increment LastIndex by one to properly cover the visible range
{
Expand Down

0 comments on commit 7b63a08

Please sign in to comment.