Skip to content

Commit e05c729

Browse files
committed
fix for closed media sessions not updating DataModel
1 parent cf74ec8 commit e05c729

6 files changed

Lines changed: 32 additions & 20 deletions

File tree

Artemis.MediaInfo/DataModels/MediaInfoDataModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class MediaInfoDataModel : DataModel
3131
public bool HasArt { get; set; }
3232

3333
public ColorSwatch ArtColors { get; set; }
34-
35-
public string SessionName { get; set; }
3634

37-
public ISet<MediaManager.MediaSession> MediaSessions { get; set; }
38-
public ISet<MediaManager.MediaSession> ArtMediaSessions { get; set; }
35+
public string SessionName { get; set; } = "";
36+
37+
public ISet<MediaManager.MediaSession> MediaSessions { get; set; } = new HashSet<MediaManager.MediaSession>();
38+
public ISet<MediaManager.MediaSession> ArtMediaSessions { get; set; } = new HashSet<MediaManager.MediaSession>();
3939
}

Artemis.MediaInfo/MediaInfoModule.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,12 @@ private async void MediaWatcherOnArtStateChanged(object? sender, ArtStateChanged
115115
{
116116
if (e.Thumbnail != null)
117117
{
118+
DataModel.HasArt = true;
118119
DataModel.ArtColors = await ReadMediaColors(e.Thumbnail);
119120
}
120121
else
121122
{
122-
var mediaSession = _mediaWatcher.AlbumArtSessions.LastOrDefault();
123-
if (mediaSession!= null)
124-
{
125-
var mediaProperties = mediaSession.ControlSession.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
126-
DataModel.ArtColors = await ReadMediaColors(mediaProperties);
127-
}
123+
DataModel.HasArt = false;
128124
}
129-
DataModel.HasArt = _mediaWatcher.AlbumArtSessions.Any();
130125
}
131126
}

Artemis.MediaInfo/MediaWatch/MediaEvents.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ public MediaAddedEventArgs(MediaManager.MediaSession mediaSession)
1717

1818
public class ArtStateChangedEventArgs : EventArgs
1919
{
20-
public MediaManager.MediaSession MediaSession { get; }
2120
public IRandomAccessStreamReference? Thumbnail { get; }
2221

23-
public ArtStateChangedEventArgs(MediaManager.MediaSession mediaSession, IRandomAccessStreamReference? thumbnail)
22+
public ArtStateChangedEventArgs(IRandomAccessStreamReference? thumbnail)
2423
{
25-
MediaSession = mediaSession;
2624
Thumbnail = thumbnail;
2725
}
2826
}

Artemis.MediaInfo/MediaWatch/MediaWatcher.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using Windows.Media.Control;
56
using WindowsMediaController;
@@ -75,7 +76,11 @@ private void MediaManager_OnSessionOpened(MediaSession mediaSession)
7576

7677
private void MediaManager_OnAnySessionClosed(MediaSession mediaSession)
7778
{
78-
AlbumArtSessions.Remove(mediaSession);
79+
var artSessionRemoved = AlbumArtSessions.Remove(mediaSession);
80+
if (artSessionRemoved)
81+
{
82+
NotifyNextMediaSession();
83+
}
7984
MediaSessions.Remove(mediaSession);
8085
_mediaManager.ForceUpdate();
8186
}
@@ -93,17 +98,31 @@ private void MediaManager_OnAnyMediaPropertyChanged(MediaSession mediaSession,
9398
if (mediaProperties.Thumbnail is null)
9499
{
95100
AlbumArtSessions.Remove(mediaSession);
96-
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaSession, null));
101+
NotifyNextMediaSession();
97102
return;
98103
}
99104

100105
AlbumArtSessions.Add(mediaSession);
101-
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaSession, mediaProperties.Thumbnail));
106+
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaProperties.Thumbnail));
102107
}
103108
catch
104109
{
105110
AlbumArtSessions.Remove(mediaSession);
106-
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaSession, null));
111+
NotifyNextMediaSession();
112+
}
113+
}
114+
115+
private void NotifyNextMediaSession()
116+
{
117+
var nextArtSession = AlbumArtSessions.LastOrDefault();
118+
if (nextArtSession != null)
119+
{
120+
var mediaProperties = nextArtSession.ControlSession.TryGetMediaPropertiesAsync().GetAwaiter().GetResult();
121+
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(mediaProperties.Thumbnail));
122+
}
123+
else
124+
{
125+
ArtStateChanged?.Invoke(this, new ArtStateChangedEventArgs(null));
107126
}
108127
}
109128

Artemis.MediaInfo/Utils/RegistryWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public RegistryWatcher(WatchedRegistry watchedRegistry, string key, string value
6363

6464
public void StartWatching()
6565
{
66-
ManagementScope scope = new ManagementScope("\\\\.\\root\\default");
66+
var scope = new ManagementScope("\\\\.\\root\\default");
6767
_eventWatcher = new ManagementEventWatcher(scope, _query);
6868
_eventWatcher.EventArrived += KeyWatcherOnEventArrived;
6969
_eventWatcher.Start();

Artemis.MediaInfo/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"Main": "Artemis.MediaInfo.dll",
77
"Name": "Media Info",
88
"Repository": "https://github.com/Aytackydln/Artemis.MediaInfo",
9-
"Version": "1.1.0.0",
9+
"Version": "1.2.1.0",
1010
"Platforms":"Windows"
1111
}

0 commit comments

Comments
 (0)