Skip to content

Commit

Permalink
fix for closed media sessions not updating DataModel
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Feb 28, 2023
1 parent cf74ec8 commit e05c729
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
8 changes: 4 additions & 4 deletions Artemis.MediaInfo/DataModels/MediaInfoDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class MediaInfoDataModel : DataModel
public bool HasArt { get; set; }

public ColorSwatch ArtColors { get; set; }

public string SessionName { get; set; }

public ISet<MediaManager.MediaSession> MediaSessions { get; set; }
public ISet<MediaManager.MediaSession> ArtMediaSessions { get; set; }
public string SessionName { get; set; } = "";

public ISet<MediaManager.MediaSession> MediaSessions { get; set; } = new HashSet<MediaManager.MediaSession>();
public ISet<MediaManager.MediaSession> ArtMediaSessions { get; set; } = new HashSet<MediaManager.MediaSession>();
}
9 changes: 2 additions & 7 deletions Artemis.MediaInfo/MediaInfoModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,12 @@ private async void MediaWatcherOnArtStateChanged(object? sender, ArtStateChanged
{
if (e.Thumbnail != null)
{
DataModel.HasArt = true;
DataModel.ArtColors = await ReadMediaColors(e.Thumbnail);
}
else
{
var mediaSession = _mediaWatcher.AlbumArtSessions.LastOrDefault();
if (mediaSession!= null)
{
var mediaProperties = mediaSession.ControlSession.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
DataModel.ArtColors = await ReadMediaColors(mediaProperties);
}
DataModel.HasArt = false;
}
DataModel.HasArt = _mediaWatcher.AlbumArtSessions.Any();
}
}
4 changes: 1 addition & 3 deletions Artemis.MediaInfo/MediaWatch/MediaEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ public MediaAddedEventArgs(MediaManager.MediaSession mediaSession)

public class ArtStateChangedEventArgs : EventArgs
{
public MediaManager.MediaSession MediaSession { get; }
public IRandomAccessStreamReference? Thumbnail { get; }

public ArtStateChangedEventArgs(MediaManager.MediaSession mediaSession, IRandomAccessStreamReference? thumbnail)
public ArtStateChangedEventArgs(IRandomAccessStreamReference? thumbnail)
{
MediaSession = mediaSession;
Thumbnail = thumbnail;
}
}
Expand Down
27 changes: 23 additions & 4 deletions Artemis.MediaInfo/MediaWatch/MediaWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Media.Control;
using WindowsMediaController;
Expand Down Expand Up @@ -75,7 +76,11 @@ private void MediaManager_OnSessionOpened(MediaSession mediaSession)

private void MediaManager_OnAnySessionClosed(MediaSession mediaSession)
{
AlbumArtSessions.Remove(mediaSession);
var artSessionRemoved = AlbumArtSessions.Remove(mediaSession);
if (artSessionRemoved)
{
NotifyNextMediaSession();
}
MediaSessions.Remove(mediaSession);
_mediaManager.ForceUpdate();
}
Expand All @@ -93,17 +98,31 @@ private void MediaManager_OnAnyMediaPropertyChanged(MediaSession mediaSession,
if (mediaProperties.Thumbnail is null)
{
AlbumArtSessions.Remove(mediaSession);
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaSession, null));
NotifyNextMediaSession();
return;
}

AlbumArtSessions.Add(mediaSession);
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaSession, mediaProperties.Thumbnail));
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaProperties.Thumbnail));
}
catch
{
AlbumArtSessions.Remove(mediaSession);
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaSession, null));
NotifyNextMediaSession();
}
}

private void NotifyNextMediaSession()
{
var nextArtSession = AlbumArtSessions.LastOrDefault();
if (nextArtSession != null)
{
var mediaProperties = nextArtSession.ControlSession.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaProperties.Thumbnail));
}
else
{
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(null));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Artemis.MediaInfo/Utils/RegistryWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public RegistryWatcher(WatchedRegistry watchedRegistry, string key, string value

public void StartWatching()
{
ManagementScope scope = new ManagementScope("\\\\.\\root\\default");
var scope = new ManagementScope("\\\\.\\root\\default");
_eventWatcher = new ManagementEventWatcher(scope, _query);
_eventWatcher.EventArrived += KeyWatcherOnEventArrived;
_eventWatcher.Start();
Expand Down
2 changes: 1 addition & 1 deletion Artemis.MediaInfo/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"Main": "Artemis.MediaInfo.dll",
"Name": "Media Info",
"Repository": "https://github.com/Aytackydln/Artemis.MediaInfo",
"Version": "1.1.0.0",
"Version": "1.2.1.0",
"Platforms":"Windows"
}

0 comments on commit e05c729

Please sign in to comment.