Skip to content

Commit 18642d6

Browse files
committed
Renamed a bunch of events to conform to .NET event pattern
1 parent 72ffd07 commit 18642d6

9 files changed

+154
-164
lines changed

Spotify/DomainObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private set
121121
{
122122
_handle = value;
123123
}
124-
}
124+
}
125125

126126
protected delegate int MakeListGetCount(IntPtr p);
127127
protected delegate IntPtr MakeListGetItem<T>(IntPtr p, int index);

Spotify/Image.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ namespace Spotify
77
{
88
public class Image : DomainObject
99
{
10-
public event EventHandler OnLoaded;
10+
public event EventHandler Loaded;
1111

1212
internal Image(IntPtr handle, bool preIncremented = true)
1313
: base(handle, LibSpotify.sp_image_add_ref_r, LibSpotify.sp_image_release_r, preIncremented)
1414
{
15-
ThrowHelper.ThrowIfError(LibSpotify.sp_image_add_load_callback_r(Handle, HandleImageLoaded, IntPtr.Zero));
15+
ThrowHelper.ThrowIfError(LibSpotify.sp_image_add_load_callback_r(Handle, OnImageLoaded, IntPtr.Zero));
1616
}
1717

1818
public System.Drawing.Image ToImage()
@@ -28,15 +28,15 @@ protected override void Dispose(bool disposing)
2828

2929
if (disposing)
3030
{
31-
LibSpotify.sp_image_remove_load_callback_r(Handle, HandleImageLoaded, IntPtr.Zero);
31+
LibSpotify.sp_image_remove_load_callback_r(Handle, OnImageLoaded, IntPtr.Zero);
3232
}
3333

3434
base.Dispose(disposing);
3535
}
3636

37-
private void HandleImageLoaded(IntPtr image, IntPtr user)
37+
private void OnImageLoaded(IntPtr image, IntPtr user)
3838
{
39-
EventDispatcher.Dispatch(this, image, OnLoaded, EventArgs.Empty);
39+
EventDispatcher.Dispatch(this, image, Loaded, EventArgs.Empty);
4040
}
4141

4242
public bool IsLoaded

Spotify/Internal/ImageLoader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static IAsyncResult Begin(ImageLoad load, IntPtr p, Session session, Imag
2525
AsyncLoadImageResult result = new AsyncLoadImageResult(userCallback, state);
2626
Image image = new Image(LibSpotify.sp_image_create_r(session.Handle, load(p, size)));
2727
result.Closure = image;
28-
image.OnLoaded += result.HandleImageLoaded;
28+
image.Loaded += result.HandleImageLoaded;
2929
return result;
3030
}
3131

Spotify/LibSpotify.cs

+1-23
Original file line numberDiff line numberDiff line change
@@ -1140,29 +1140,7 @@ public static System.IntPtr sp_playlist_subscribers_r(System.IntPtr playlist)
11401140
{
11411141
lock (_apiLock)
11421142
return sp_playlist_subscribers(playlist);
1143-
}
1144-
1145-
internal static IList<string> GetPlaylistSubscribers(IntPtr playlist)
1146-
{
1147-
List<string> subscribers = new List<string>();
1148-
1149-
IntPtr p = LibSpotify.sp_playlist_subscribers_r(playlist);
1150-
if (p != IntPtr.Zero)
1151-
{
1152-
int n = Marshal.ReadInt32(p);
1153-
1154-
int offset = Marshal.SizeOf(typeof(Int32));
1155-
for (int i = 0; i < n; ++i)
1156-
{
1157-
subscribers.Add(ReadUtf8(Marshal.ReadIntPtr(p, offset)));
1158-
offset += Marshal.SizeOf(typeof(IntPtr));
1159-
}
1160-
1161-
LibSpotify.sp_playlist_subscribers_free_r(p);
1162-
}
1163-
1164-
return subscribers;
1165-
}
1143+
}
11661144

11671145
[System.Runtime.InteropServices.DllImport("libspotify")]
11681146
private static extern Spotify.Error sp_playlist_subscribers_free(System.IntPtr subscribers);

Spotify/Playlist.cs

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Runtime.InteropServices;
45
using System.Linq;
56

67
using Spotify.Internal;
@@ -21,7 +22,7 @@ public class Playlist : DomainObject //, INotifyPropertyChanged
2122
public event EventHandler<PlaylistTrackSeenChangedEventArgs> TrackSeenChanged;
2223
public event EventHandler<PlaylistDescriptionChangedEventArgs> DescriptionChanged;
2324

24-
//public event PropertyChangedEventHandler PropertyChanged;
25+
// public event PropertyChangedEventHandler PropertyChanged;
2526

2627
// TODO ImageChanged
2728

@@ -64,7 +65,6 @@ public bool IsLoaded
6465
}
6566
}
6667

67-
6868
// I'm still unsure whether exposing NumTracks/TrackAt(int) is better than a List of Tracks. The obvious
6969
// advantage is not creating a list of Disposable objects everytime someone touches the Tracks. Maybe caching
7070
// the results would be better and just invalidating on changes
@@ -83,17 +83,15 @@ private Track TrackAt(int index)
8383
return ListItem(index, p => { return new Track(p, false); },
8484
LibSpotify.sp_playlist_num_tracks_r, LibSpotify.sp_playlist_track_r);
8585
}
86-
87-
88-
private IList<Track> _tracks = null;
86+
8987
public IList<Track> Tracks
9088
{
9189
get
9290
{
9391
if (_tracks == null)
9492
_tracks = MakeList(p => { return new Track(p, false); }, LibSpotify.sp_playlist_num_tracks_r, LibSpotify.sp_playlist_track_r);
9593
return _tracks;
96-
}
94+
}
9795
}
9896

9997
public string Name
@@ -156,21 +154,20 @@ public string Description
156154
}
157155
}
158156

159-
public int NumSubscribers
157+
private int NumSubscribers
160158
{
161159
get
162160
{
163161
return Convert.ToInt32(LibSpotify.sp_playlist_num_subscribers_r(Handle));
164162
}
165163
}
166-
167-
public IList<string> _subscribers = null;
164+
168165
public IList<string> Subscribers
169166
{
170167
get
171168
{
172169
if (_subscribers == null)
173-
_subscribers = LibSpotify.GetPlaylistSubscribers(Handle);
170+
_subscribers = MarshalPlaylistSubscribers(Handle);
174171
return _subscribers;
175172
}
176173
}
@@ -263,7 +260,29 @@ public string GetTrackMessage(Track t)
263260
}
264261
#endregion
265262

266-
#region Private Methods
263+
#region Private Methods
264+
private static IList<string> MarshalPlaylistSubscribers(IntPtr playlist)
265+
{
266+
List<string> subscribers = new List<string>();
267+
268+
IntPtr p = LibSpotify.sp_playlist_subscribers_r(playlist);
269+
if (p != IntPtr.Zero)
270+
{
271+
int n = Marshal.ReadInt32(p);
272+
273+
int offset = Marshal.SizeOf(typeof(Int32));
274+
for (int i = 0; i < n; ++i)
275+
{
276+
subscribers.Add(LibSpotify.ReadUtf8(Marshal.ReadIntPtr(p, offset)));
277+
offset += Marshal.SizeOf(typeof(IntPtr));
278+
}
279+
280+
LibSpotify.sp_playlist_subscribers_free_r(p);
281+
}
282+
283+
return subscribers;
284+
}
285+
267286
private static void ThrowIfDuplicates(IEnumerable<int> e)
268287
{
269288
// check for duplicates
@@ -364,6 +383,8 @@ private void OnSubscribersChanged(IntPtr playlist, IntPtr state)
364383
#endregion
365384

366385
#region Fields
386+
private IList<Track> _tracks = null;
387+
private IList<string> _subscribers = null;
367388
private LibSpotify.sp_playlist_callbacks _callbacks;
368389
#endregion
369390
}

Spotify/PlaylistContainer.cs

+16-16
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ namespace Spotify
99
public class PlaylistContainer : DomainObject
1010
{
1111
#region Events
12-
public event EventHandler<PlaylistAddedEventArgs> OnPlaylistAdded;
13-
public event EventHandler<PlaylistRemovedEventArgs> OnPlaylistRemoved;
14-
public event EventHandler OnPlaylistMoved;
15-
public event EventHandler OnLoaded;
12+
public event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
13+
public event EventHandler<PlaylistRemovedEventArgs> PlaylistRemoved;
14+
public event EventHandler PlaylistMoved;
15+
public event EventHandler Loaded;
1616
#endregion
1717

1818
internal PlaylistContainer(IntPtr handle, bool preIncremented = true)
1919
: base(handle, LibSpotify.sp_playlistcontainer_add_ref_r, LibSpotify.sp_playlistcontainer_release_r, preIncremented)
2020
{
2121
LibSpotify.sp_playlistcontainer_callbacks callbacks = new LibSpotify.sp_playlistcontainer_callbacks();
22-
callbacks.playlist_added = RaisePlaylistAdded;
23-
callbacks.playlist_removed = RaisePlaylistRemoved;
24-
callbacks.playlist_moved = RaisePlaylistMoved;
25-
callbacks.container_loaded = RaiseContainerLoaded;
22+
callbacks.playlist_added = OnPlaylistAdded;
23+
callbacks.playlist_removed = OnPlaylistRemoved;
24+
callbacks.playlist_moved = OnPlaylistMoved;
25+
callbacks.container_loaded = OnContainerLoaded;
2626

2727
ThrowHelper.ThrowIfError(LibSpotify.sp_playlistcontainer_add_callbacks_r(Handle,
2828
ref callbacks, IntPtr.Zero));
@@ -134,26 +134,26 @@ public void AddFolder(int index, string name)
134134

135135

136136
#region Private Methods
137-
private void RaiseContainerLoaded(IntPtr playlistContainer, IntPtr state)
137+
private void OnContainerLoaded(IntPtr playlistContainer, IntPtr state)
138138
{
139-
EventDispatcher.Dispatch(this, playlistContainer, OnLoaded, EventArgs.Empty);
139+
EventDispatcher.Dispatch(this, playlistContainer, Loaded, EventArgs.Empty);
140140
}
141141

142-
private void RaisePlaylistAdded(IntPtr playlistContainer, IntPtr playlist, int position, IntPtr state)
142+
private void OnPlaylistAdded(IntPtr playlistContainer, IntPtr playlist, int position, IntPtr state)
143143
{
144-
EventDispatcher.Dispatch(this, playlistContainer, OnPlaylistAdded,
144+
EventDispatcher.Dispatch(this, playlistContainer, PlaylistAdded,
145145
new PlaylistAddedEventArgs(playlist, position));
146146
}
147147

148-
private void RaisePlaylistMoved(IntPtr playlistContainer, IntPtr playlist, int position, int newPosition, IntPtr state)
148+
private void OnPlaylistMoved(IntPtr playlistContainer, IntPtr playlist, int position, int newPosition, IntPtr state)
149149
{
150-
EventDispatcher.Dispatch(this, playlistContainer, OnPlaylistMoved,
150+
EventDispatcher.Dispatch(this, playlistContainer, PlaylistMoved,
151151
new PlaylistMovedEventArgs(playlist, position, newPosition));
152152
}
153153

154-
private void RaisePlaylistRemoved(IntPtr playlistContainer, IntPtr playlist, int position, IntPtr state)
154+
private void OnPlaylistRemoved(IntPtr playlistContainer, IntPtr playlist, int position, IntPtr state)
155155
{
156-
EventDispatcher.Dispatch(this, playlistContainer, OnPlaylistRemoved,
156+
EventDispatcher.Dispatch(this, playlistContainer, PlaylistRemoved,
157157
new PlaylistRemovedEventArgs(playlist, position));
158158
}
159159
#endregion

0 commit comments

Comments
 (0)