Skip to content

Commit

Permalink
Merge pull request #29 from Difegue/dev
Browse files Browse the repository at this point in the history
v2.1
  • Loading branch information
Difegue authored Sep 21, 2021
2 parents 46c7199 + f004110 commit c8e9e30
Show file tree
Hide file tree
Showing 37 changed files with 985 additions and 212 deletions.
4 changes: 0 additions & 4 deletions Sources/Stylophone.Common/Interfaces/IInteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public interface IInteropService
Version GetAppVersion();
Task SetThemeAsync(Theme param);

void PlayStream(Uri streamUri);
void StopStream();
void SetStreamVolume(double volume);

string GetIcon(PlaybackIcon volumeFull);

Task OpenStoreReviewUrlAsync();
Expand Down
7 changes: 7 additions & 0 deletions Sources/Stylophone.Common/Services/AlbumArtService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ColorThiefDotNet;
using Microsoft.Toolkit.Mvvm.DependencyInjection;
using MpcNET.Commands.Database;
using MpcNET.Types;
using SkiaSharp;
Expand Down Expand Up @@ -135,6 +136,12 @@ private async Task<SKBitmap> GetAlbumBitmap(IMpdFile f, CancellationToken token
if (await IsAlbumArtCachedAsync(f))
return await LoadImageFromFile(fileName);

// No cache, check if remote fetching is enabled before going further
var isAlbumArtFetchingEnabled = _applicationStorageService.GetValue<bool>(nameof(SettingsViewModel.IsAlbumArtFetchingEnabled), true);

if (!isAlbumArtFetchingEnabled)
return null;

// Get albumart from MPD
List<byte> data = new List<byte>();
try
Expand Down
11 changes: 6 additions & 5 deletions Sources/Stylophone.Common/Stylophone.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CodeProject.ObjectPool" Version="5.0.2" />
<PackageReference Include="Microsoft.Toolkit" Version="7.0.1" />
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.0.1" />
<PackageReference Include="CodeProject.ObjectPool" Version="5.0.3" />
<PackageReference Include="LibVLCSharp" Version="3.6.0" />
<PackageReference Include="Microsoft.Toolkit" Version="7.0.2" />
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.0.2" />
<PackageReference Include="RangedObservableCollection" Version="1.0.1" />
<PackageReference Include="SkiaSharp" Version="2.80.2" />
<PackageReference Include="SkiaSharp.Views.Desktop.Common" Version="2.80.2" />
<PackageReference Include="SkiaSharp" Version="2.80.3" />
<PackageReference Include="SkiaSharp.Views.Desktop.Common" Version="2.80.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ private void AddToPlaylist(EventArgs obj)
_notificationService.ShowInAppNotification(Resources.NotificationNoTrackPlaying);
}

CurrentTrack.AddToPlayListCommand.Execute(CurrentTrack.File);
CurrentTrack?.AddToPlayListCommand.Execute(CurrentTrack.File);
}

private ICommand _showAlbumCommand;
Expand Down
38 changes: 29 additions & 9 deletions Sources/Stylophone.Common/ViewModels/LocalPlaybackViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using LibVLCSharp.Shared;
using Stylophone.Common.Interfaces;
using Stylophone.Common.Services;
using Stylophone.Localization.Strings;
Expand All @@ -12,6 +13,8 @@ public class LocalPlaybackViewModel : ViewModelBase
private SettingsViewModel _settingsVm;
private MPDConnectionService _mpdService;

private LibVLC _vlcCore;
private MediaPlayer _mediaPlayer;
private string _serverHost;

public LocalPlaybackViewModel(SettingsViewModel settingsVm, MPDConnectionService mpdService, IInteropService interopService, INotificationService notificationService, IDispatcherService dispatcherService): base(dispatcherService)
Expand Down Expand Up @@ -47,12 +50,23 @@ private set
{
Set(ref _isEnabled, value);

if (!value)
if (value)
{
if (_vlcCore == null)
_vlcCore = new LibVLC();

_mediaPlayer?.Dispose();
_mediaPlayer = new MediaPlayer(_vlcCore);
}
else
{
// Reset
IsPlaying = false;
Volume = 0;
_previousVolume = 10;

_vlcCore?.Dispose();
_vlcCore = null;
}

}
Expand All @@ -68,8 +82,8 @@ public string VolumeIcon
private set => Set(ref _volumeIcon, value);
}

private double _volume = 0;
public double Volume
private int _volume = 0;
public int Volume
{
get => _volume;
set
Expand All @@ -80,9 +94,10 @@ public double Volume
if (!IsPlaying && value != 0)
IsPlaying = true;

_interopService.SetStreamVolume(value);
if (_mediaPlayer != null)
_mediaPlayer.Volume = value;

if ((int)value == 0)
if (value == 0)
{
VolumeIcon = _interopService.GetIcon(PlaybackIcon.VolumeMute);
}
Expand Down Expand Up @@ -116,7 +131,7 @@ private set
}
}

private double _previousVolume = 10;
private int _previousVolume = 25;
/// <summary>
/// Toggle if we should mute
/// </summary>
Expand All @@ -140,13 +155,18 @@ private void UpdatePlayback()
{
if (IsPlaying && _serverHost != null && _mpdService.IsConnected)
{
var urlString = "http://" + _serverHost + ":8000/mpd.ogg";
var urlString = "http://" + _serverHost + ":8000";
var streamUrl = new Uri(urlString);
_interopService.PlayStream(streamUrl);
var media = new Media(_vlcCore, streamUrl);

_mediaPlayer.Play(media);

// This set won't work on UWP, see https://code.videolan.org/videolan/LibVLCSharp/-/issues/423
_mediaPlayer.Volume = _volume;
}
else
{
_interopService.StopStream();
_mediaPlayer?.Stop();
}
}
catch (Exception e)
Expand Down
62 changes: 33 additions & 29 deletions Sources/Stylophone.Common/ViewModels/PlaylistViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ public async Task LoadDataAsync(string playlistName)
{
var placeholder = await _interop.GetPlaceholderImageAsync();

ArtLoaded = false;
PlaylistArt = placeholder;
PlaylistArt2 = placeholder;
PlaylistArt3 = placeholder;
Expand Down Expand Up @@ -267,37 +268,40 @@ public async Task LoadDataAsync(string playlistName)
PlaylistInfo = $"{Source.Count} Tracks, Total Time: {Miscellaneous.ToReadableString(t)}";


await Task.Run(async () =>
if (Source.Count > 0)
{
// Get album art for three albums to display in the playlist view
Random r = new Random();
var distinctAlbums = Source.GroupBy(tr => tr.File.Album).Select(tr => tr.First()).OrderBy((item) => r.Next()).ToList();
if (distinctAlbums.Count > 1)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[0].File, true);
PlaylistArt = art != null ? art.ArtBitmap : PlaylistArt;
DominantColor = (art?.DominantColor?.Color).GetValueOrDefault();
IsLight = (!art?.DominantColor?.IsDark).GetValueOrDefault();
}
if (distinctAlbums.Count > 2)
await Task.Run(async () =>
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[1].File, false);
PlaylistArt2 = art != null ? art.ArtBitmap : PlaylistArt2;
}
else PlaylistArt2 = PlaylistArt;
if (distinctAlbums.Count > 3)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[2].File, false);
PlaylistArt3 = art != null ? art.ArtBitmap : PlaylistArt3;
}
else PlaylistArt3 = PlaylistArt2;
ArtLoaded = true;
});
// Get album art for three albums to display in the playlist view
Random r = new Random();
var distinctAlbums = Source.GroupBy(tr => tr.File.Album).Select(tr => tr.First()).OrderBy((item) => r.Next()).ToList();
if (distinctAlbums.Count > 1)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[0].File, true);
PlaylistArt = art != null ? art.ArtBitmap : PlaylistArt;
DominantColor = (art?.DominantColor?.Color).GetValueOrDefault();
IsLight = (!art?.DominantColor?.IsDark).GetValueOrDefault();
}
if (distinctAlbums.Count > 2)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[1].File, false);
PlaylistArt2 = art != null ? art.ArtBitmap : PlaylistArt2;
}
else PlaylistArt2 = PlaylistArt;
if (distinctAlbums.Count > 3)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[2].File, false);
PlaylistArt3 = art != null ? art.ArtBitmap : PlaylistArt3;
}
else PlaylistArt3 = PlaylistArt2;
ArtLoaded = true;
});
}
}

private async void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
Expand Down
26 changes: 22 additions & 4 deletions Sources/Stylophone.Common/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ public bool IsCompactSizing
}
}

private bool _albumArtEnabled;
public bool IsAlbumArtFetchingEnabled
{
get { return _albumArtEnabled; }
set
{
if (value != _albumArtEnabled)
{
_applicationStorageService.SetValue(nameof(IsAlbumArtFetchingEnabled), value);
}
Set(ref _albumArtEnabled, value);
}
}

private bool _enableAnalytics;
public bool EnableAnalytics
{
Expand Down Expand Up @@ -240,8 +254,9 @@ public async Task EnsureInstanceInitializedAsync()
_serverHost = _applicationStorageService.GetValue<string>(nameof(ServerHost));
_serverHost = _serverHost?.Replace("\"", ""); // TODO: This is a quickfix for 1.x updates

_serverPort = _applicationStorageService.GetValue<int>(nameof(ServerPort), 6600);
_enableAnalytics = _applicationStorageService.GetValue<bool>(nameof(EnableAnalytics), true);
_serverPort = _applicationStorageService.GetValue(nameof(ServerPort), 6600);
_enableAnalytics = _applicationStorageService.GetValue(nameof(EnableAnalytics), true);
_albumArtEnabled = _applicationStorageService.GetValue(nameof(IsAlbumArtFetchingEnabled), true);
_localPlaybackEnabled = _applicationStorageService.GetValue<bool>(nameof(IsLocalPlaybackEnabled));

Enum.TryParse(_applicationStorageService.GetValue<string>(nameof(ElementTheme)), out _elementTheme);
Expand Down Expand Up @@ -303,12 +318,15 @@ private async Task UpdateServerVersionAsync()
// Build info string
var outputs = await _mpdService.SafelySendCommandAsync(new OutputsCommand());

var songs = response.ContainsKey("songs") ? response["songs"] : "??";
var albums = response.ContainsKey("albums") ? response["albums"] : "??";

if (outputs != null)
{
var outputString = outputs.Select(o => o.Plugin).Aggregate((s, s2) => $"{s}, {s2}");

ServerInfo = $"MPD Protocol {_mpdService.Version}\n" +
$"{response["songs"]} Songs, {response["albums"]} Albums\n" +
$"{songs} Songs, {albums} Albums\n" +
$"Database last updated {lastUpdatedDb}\n" +
$"Outputs available: {outputString}";

Expand All @@ -320,7 +338,7 @@ private async Task UpdateServerVersionAsync()
else
{
ServerInfo = $"MPD Protocol {_mpdService.Version}\n" +
$"{response["songs"]} Songs, {response["albums"]} Albums\n" +
$"{songs} Songs, {albums} Albums\n" +
$"Database last updated {lastUpdatedDb}";
}

Expand Down
21 changes: 19 additions & 2 deletions Sources/Stylophone.Localization/Strings/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions Sources/Stylophone.Localization/Strings/Resources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@
<value>Clear Cache</value>
</data>
<data name="SettingsClearCacheDescription" xml:space="preserve">
<value>Stylophone stores album art locally to avoid overloading your MPD Server.
If you want to clear the album art cache, click this button.</value>
<value>Clear the local album art cache</value>
</data>
<data name="SettingsCustomization" xml:space="preserve">
<value>Personalization</value>
Expand Down Expand Up @@ -485,4 +484,10 @@ Enabling this option will show a second volume slider to control local volume.</
<data name="FirstRunFlavorText" xml:space="preserve">
<value>David Bowie is credited with playing the Stylophone on his 1969 debut hit song "Space Oddity" and also for his 2002 album Heathen track titled "Slip Away," as well as on the song "Heathen (The Rays)".</value>
</data>
<data name="SettingsAlbumArt" xml:space="preserve">
<value>Download Album Art from the MPD Server</value>
</data>
<data name="SettingsAlbumArtText" xml:space="preserve">
<value>Stylophone stores album art locally to avoid overloading your MPD Server.</value>
</data>
</root>
9 changes: 7 additions & 2 deletions Sources/Stylophone.Localization/Strings/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@
<value>Vider le cache</value>
</data>
<data name="SettingsClearCacheDescription" xml:space="preserve">
<value>Stylophone stocke les pochettes d'album sur votre ordinateur pour éviter de surcharger votre serveur MPD.
Pour nettoyer ce cache de pochettes, cliquez sur ce bouton.</value>
<value>Nettoyer le cache de pochettes local</value>
</data>
<data name="SettingsCustomization" xml:space="preserve">
<value>Personnalisation</value>
Expand Down Expand Up @@ -484,4 +483,10 @@ L'activation de cette option affichera un second slider pour contrôler le volum
<data name="FirstRunFlavorText" xml:space="preserve">
<value>David Bowie is credited with playing the Stylophone on his 1969 debut hit song "Space Oddity" and also for his 2002 album Heathen track titled "Slip Away," as well as on the song "Heathen (The Rays)".</value>
</data>
<data name="SettingsAlbumArt" xml:space="preserve">
<value>Récupérer les pochettes d'album depuis le serveur MPD</value>
</data>
<data name="SettingsAlbumArtText" xml:space="preserve">
<value>Stylophone stocke les pochettes d'album sur votre ordinateur pour éviter de surcharger votre serveur MPD.</value>
</data>
</root>
9 changes: 7 additions & 2 deletions Sources/Stylophone.Localization/Strings/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@
<value>Clear Cache</value>
</data>
<data name="SettingsClearCacheDescription" xml:space="preserve">
<value>Stylophone stores album art locally to avoid overloading your MPD Server.
If you want to clear the album art cache, click this button.</value>
<value>Clear the local album art cache</value>
</data>
<data name="SettingsCustomization" xml:space="preserve">
<value>Personalization</value>
Expand Down Expand Up @@ -485,4 +484,10 @@ Enabling this option will show a second volume slider to control local volume.</
<data name="FirstRunFlavorText" xml:space="preserve">
<value>David Bowie is credited with playing the Stylophone on his 1969 debut hit song "Space Oddity" and also for his 2002 album Heathen track titled "Slip Away," as well as on the song "Heathen (The Rays)".</value>
</data>
<data name="SettingsAlbumArt" xml:space="preserve">
<value>Download Album Art from the MPD Server</value>
</data>
<data name="SettingsAlbumArtText" xml:space="preserve">
<value>Stylophone stores album art locally to avoid overloading your MPD Server.</value>
</data>
</root>
Loading

0 comments on commit c8e9e30

Please sign in to comment.