Skip to content

Commit 96bcb7b

Browse files
committed
Some more refactoring.
1 parent cd74868 commit 96bcb7b

File tree

9 files changed

+76
-87
lines changed

9 files changed

+76
-87
lines changed

SpotifyAPI.Example/LocalControl.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,26 @@ public void Connect()
3333
{
3434
if (!SpotifyLocalAPI.IsSpotifyRunning())
3535
{
36-
MessageBox.Show("Spotify isn't running!");
36+
MessageBox.Show(@"Spotify isn't running!");
3737
return;
3838
}
3939
if (!SpotifyLocalAPI.IsSpotifyWebHelperRunning())
4040
{
41-
MessageBox.Show("SpotifyWebHelper isn't running!");
41+
MessageBox.Show(@"SpotifyWebHelper isn't running!");
4242
return;
4343
}
4444

4545
bool successful = _spotify.Connect();
4646
if (successful)
4747
{
48-
connectBtn.Text = "Connection to Spotify successful";
48+
connectBtn.Text = @"Connection to Spotify successful";
4949
connectBtn.Enabled = false;
5050
UpdateInfos();
5151
_spotify.ListenForEvents = true;
5252
}
5353
else
5454
{
55-
DialogResult res = MessageBox.Show("Couldn't connect to the spotify client. Retry?", "Spotify", MessageBoxButtons.YesNo);
55+
DialogResult res = MessageBox.Show(@"Couldn't connect to the spotify client. Retry?", @"Spotify", MessageBoxButtons.YesNo);
5656
if (res == DialogResult.Yes)
5757
Connect();
5858
}
@@ -109,7 +109,7 @@ private void _spotify_OnVolumeChange(VolumeChangeEventArgs e)
109109

110110
private void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e)
111111
{
112-
timeLabel.Text = FormatTime(e.TrackTime) + "/" + FormatTime(_currentTrack.Length);
112+
timeLabel.Text = $"{FormatTime(e.TrackTime)}/{FormatTime(_currentTrack.Length)}";
113113
timeProgressBar.Value = (int)e.TrackTime;
114114
}
115115

SpotifyAPI.Example/WebControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ private void _auth_OnResponseReceivedEvent(Token token, string state)
4242

4343
if (state != "XSS")
4444
{
45-
MessageBox.Show("Wrong state received.", "SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
45+
MessageBox.Show(@"Wrong state received.", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
4646
return;
4747
}
4848
if (token.Error != null)
4949
{
50-
MessageBox.Show("Error: " + token.Error, "SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
50+
MessageBox.Show($"Error: {token.Error}", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
5151
return;
5252
}
5353

SpotifyAPI/Local/RemoteHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal async void SendPlayRequest()
3535
internal async void SendPlayRequest(String url, String context = "")
3636
{
3737
// TODO: instead of having an empty context, one way to fix the bug with the playback time beyond the length of a song would be to provide a 1-song context, and it would be fixed.
38-
await QueryAsync(string.Format("remote/play.json?uri={0}&context={1}", url, context), true, true, -1);
38+
await QueryAsync($"remote/play.json?uri={url}&context={context}", true, true, -1);
3939
}
4040

4141
internal async void SendQueueRequest(String url)

SpotifyAPI/Local/SpotifyLocalAPI.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.ComponentModel;
44
using System.Diagnostics;
5-
using System.Diagnostics.Contracts;
65
using System.IO;
76
using System.Runtime.InteropServices;
87
using System.Timers;
@@ -216,7 +215,7 @@ public void SetSpotifyVolume(float volume = 100)
216215
if (Environment.OSVersion.Version.Minor == 0)
217216
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
218217
if (volume < 0 || volume > 100)
219-
throw new ArgumentOutOfRangeException("Volume parameter has to be a value between 0 and 100");
218+
throw new ArgumentOutOfRangeException(nameof(volume));
220219
VolumeMixerControl.SetSpotifyVolume(volume);
221220
}
222221

SpotifyAPI/Local/VolumeMixerControl.cs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ namespace SpotifyAPI.Local
66
{
77
internal class VolumeMixerControl
88
{
9-
private const String SPOTIFY_PROCESS_NAME = "spotify";
9+
private const String SpotifyProcessName = "spotify";
1010

1111
internal static float GetSpotifyVolume()
1212
{
13-
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
13+
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
1414
if (p.Length == 0)
1515
throw new Exception("Spotify process is not running or was not found!");
1616

@@ -19,7 +19,6 @@ internal static float GetSpotifyVolume()
1919
ISimpleAudioVolume volume = GetVolumeObject(pid);
2020
if (volume == null)
2121
{
22-
Marshal.ReleaseComObject(volume);
2322
throw new COMException("Volume object creation failed");
2423
}
2524

@@ -31,7 +30,7 @@ internal static float GetSpotifyVolume()
3130

3231
internal static bool IsSpotifyMuted()
3332
{
34-
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
33+
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
3534
if (p.Length == 0)
3635
throw new Exception("Spotify process is not running or was not found!");
3736

@@ -40,7 +39,6 @@ internal static bool IsSpotifyMuted()
4039
ISimpleAudioVolume volume = GetVolumeObject(pid);
4140
if (volume == null)
4241
{
43-
Marshal.ReleaseComObject(volume);
4442
throw new COMException("Volume object creation failed");
4543
}
4644

@@ -52,7 +50,7 @@ internal static bool IsSpotifyMuted()
5250

5351
internal static void SetSpotifyVolume(float level)
5452
{
55-
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
53+
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
5654
if (p.Length == 0)
5755
throw new Exception("Spotify process is not running or was not found!");
5856

@@ -61,7 +59,6 @@ internal static void SetSpotifyVolume(float level)
6159
ISimpleAudioVolume volume = GetVolumeObject(pid);
6260
if (volume == null)
6361
{
64-
Marshal.ReleaseComObject(volume);
6562
throw new COMException("Volume object creation failed");
6663
}
6764

@@ -72,7 +69,7 @@ internal static void SetSpotifyVolume(float level)
7269

7370
internal static void MuteSpotify(bool mute)
7471
{
75-
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
72+
Process[] p = Process.GetProcessesByName(SpotifyProcessName);
7673
if (p.Length == 0)
7774
throw new Exception("Spotify process is not running or was not found!");
7875

@@ -81,7 +78,6 @@ internal static void MuteSpotify(bool mute)
8178
ISimpleAudioVolume volume = GetVolumeObject(pid);
8279
if (volume == null)
8380
{
84-
Marshal.ReleaseComObject(volume);
8581
throw new COMException("Volume object creation failed");
8682
}
8783

@@ -93,14 +89,14 @@ internal static void MuteSpotify(bool mute)
9389
private static ISimpleAudioVolume GetVolumeObject(int pid)
9490
{
9591
// get the speakers (1st render + multimedia) device
96-
IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
97-
IMMDevice speakers;
98-
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);
92+
IMmDeviceEnumerator deviceEnumerator = (IMmDeviceEnumerator)(new MMDeviceEnumerator());
93+
IMmDevice speakers;
94+
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out speakers);
9995

10096
// activate the session manager. we need the enumerator
101-
Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
97+
Guid iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
10298
object o;
103-
speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
99+
speakers.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out o);
104100
IAudioSessionManager2 mgr = (IAudioSessionManager2)o;
105101

106102
// enumerate sessions for on this device
@@ -121,7 +117,7 @@ private static ISimpleAudioVolume GetVolumeObject(int pid)
121117

122118
if (cpid == pid)
123119
{
124-
volumeControl = ctl as ISimpleAudioVolume;
120+
volumeControl = (ISimpleAudioVolume) ctl;
125121
break;
126122
}
127123
Marshal.ReleaseComObject(ctl);
@@ -142,31 +138,31 @@ private class MMDeviceEnumerator
142138

143139
private enum EDataFlow
144140
{
145-
eRender,
146-
eCapture,
147-
eAll,
148-
EDataFlow_enum_count
141+
ERender,
142+
ECapture,
143+
EAll,
144+
EDataFlowEnumCount
149145
}
150146

151147
private enum ERole
152148
{
153-
eConsole,
154-
eMultimedia,
155-
eCommunications,
156-
ERole_enum_count
149+
EConsole,
150+
EMultimedia,
151+
ECommunications,
152+
ERoleEnumCount
157153
}
158154

159155
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
160-
private interface IMMDeviceEnumerator
156+
private interface IMmDeviceEnumerator
161157
{
162158
int NotImpl1();
163159

164160
[PreserveSig]
165-
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMMDevice ppDevice);
161+
int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMmDevice ppDevice);
166162
}
167163

168164
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
169-
private interface IMMDevice
165+
private interface IMmDevice
170166
{
171167
[PreserveSig]
172168
int Activate(ref Guid iid, int dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface);
@@ -180,30 +176,30 @@ private interface IAudioSessionManager2
180176
int NotImpl2();
181177

182178
[PreserveSig]
183-
int GetSessionEnumerator(out IAudioSessionEnumerator SessionEnum);
179+
int GetSessionEnumerator(out IAudioSessionEnumerator sessionEnum);
184180
}
185181

186182
[Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
187183
private interface IAudioSessionEnumerator
188184
{
189185
[PreserveSig]
190-
int GetCount(out int SessionCount);
186+
int GetCount(out int sessionCount);
191187

192188
[PreserveSig]
193-
int GetSession(int SessionCount, out IAudioSessionControl2 Session);
189+
int GetSession(int sessionCount, out IAudioSessionControl2 session);
194190
}
195191

196192
[Guid("87CE5498-68D6-44E5-9215-6DA47EF883D8"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
197193
private interface ISimpleAudioVolume
198194
{
199195
[PreserveSig]
200-
int SetMasterVolume(float fLevel, ref Guid EventContext);
196+
int SetMasterVolume(float fLevel, ref Guid eventContext);
201197

202198
[PreserveSig]
203199
int GetMasterVolume(out float pfLevel);
204200

205201
[PreserveSig]
206-
int SetMute(bool bMute, ref Guid EventContext);
202+
int SetMute(bool bMute, ref Guid eventContext);
207203

208204
[PreserveSig]
209205
int GetMute(out bool pbMute);
@@ -219,19 +215,19 @@ private interface IAudioSessionControl2
219215
int GetDisplayName([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
220216

221217
[PreserveSig]
222-
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)]string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
218+
int SetDisplayName([MarshalAs(UnmanagedType.LPWStr)]string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
223219

224220
[PreserveSig]
225221
int GetIconPath([MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
226222

227223
[PreserveSig]
228-
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string Value, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
224+
int SetIconPath([MarshalAs(UnmanagedType.LPWStr)] string value, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
229225

230226
[PreserveSig]
231227
int GetGroupingParam(out Guid pRetVal);
232228

233229
[PreserveSig]
234-
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid EventContext);
230+
int SetGroupingParam([MarshalAs(UnmanagedType.LPStruct)] Guid Override, [MarshalAs(UnmanagedType.LPStruct)] Guid eventContext);
235231

236232
[PreserveSig]
237233
int NotImpl1();

SpotifyAPI/Web/Auth/AutorizationCodeAuth.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ public void StartHttpServer(int port = 80)
9898

9999
private void HttpServerOnOnAuth(AuthEventArgs e)
100100
{
101-
if (OnResponseReceivedEvent != null)
102-
OnResponseReceivedEvent(new AutorizationCodeAuthResponse()
103-
{
104-
Code = e.Code,
105-
State = e.State,
106-
Error = e.Error
107-
});
101+
OnResponseReceivedEvent?.Invoke(new AutorizationCodeAuthResponse()
102+
{
103+
Code = e.Code,
104+
State = e.State,
105+
Error = e.Error
106+
});
108107
}
109108

110109
/// <summary>

SpotifyAPI/Web/Auth/ImplicitGrantAuth.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ public void StartHttpServer(int port = 80)
5656

5757
private void HttpServerOnOnAuth(AuthEventArgs e)
5858
{
59-
if (OnResponseReceivedEvent != null)
60-
OnResponseReceivedEvent(new Token
61-
{
62-
AccessToken = e.Code,
63-
TokenType = e.TokenType,
64-
ExpiresIn = e.ExpiresIn,
65-
Error = e.Error
66-
}, e.State);
59+
OnResponseReceivedEvent?.Invoke(new Token
60+
{
61+
AccessToken = e.Code,
62+
TokenType = e.TokenType,
63+
ExpiresIn = e.ExpiresIn,
64+
Error = e.Error
65+
}, e.State);
6766
}
6867

6968
/// <summary>

0 commit comments

Comments
 (0)