Changed Contracts to if-throw-else as requested

Changed use of nullable return types to throw exceptions instead of returning null
This commit is contained in:
mrnikbobjeff 2015-10-23 21:25:13 +02:00
parent e5e57c3020
commit 3544c34176
10 changed files with 91 additions and 73 deletions

View File

@ -1,10 +1,10 @@
using System; using SpotifyAPI.Local;
using SpotifyAPI.Local.Enums;
using SpotifyAPI.Local.Models;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Windows.Forms; using System.Windows.Forms;
using SpotifyAPI.Local;
using SpotifyAPI.Local.Enums;
using SpotifyAPI.Local.Models;
namespace SpotifyAPI.Example namespace SpotifyAPI.Example
{ {
@ -102,23 +102,23 @@ namespace SpotifyAPI.Example
isPlayingLabel.Text = playing.ToString(); isPlayingLabel.Text = playing.ToString();
} }
void _spotify_OnVolumeChange(VolumeChangeEventArgs e) private void _spotify_OnVolumeChange(VolumeChangeEventArgs e)
{ {
volumeLabel.Text = (e.NewVolume * 100).ToString(CultureInfo.InvariantCulture); volumeLabel.Text = (e.NewVolume * 100).ToString(CultureInfo.InvariantCulture);
} }
void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e) private void _spotify_OnTrackTimeChange(TrackTimeChangeEventArgs e)
{ {
timeLabel.Text = FormatTime(e.TrackTime) + "/" + FormatTime(_currentTrack.Length); timeLabel.Text = FormatTime(e.TrackTime) + "/" + FormatTime(_currentTrack.Length);
timeProgressBar.Value = (int)e.TrackTime; timeProgressBar.Value = (int)e.TrackTime;
} }
void _spotify_OnTrackChange(TrackChangeEventArgs e) private void _spotify_OnTrackChange(TrackChangeEventArgs e)
{ {
UpdateTrack(e.NewTrack); UpdateTrack(e.NewTrack);
} }
void _spotify_OnPlayStateChange(PlayStateEventArgs e) private void _spotify_OnPlayStateChange(PlayStateEventArgs e)
{ {
UpdatePlayingStatus(e.Playing); UpdatePlayingStatus(e.Playing);
} }

View File

@ -1,18 +1,15 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace SpotifyAPI.Example namespace SpotifyAPI.Example
{ {
static class Program internal static class Program
{ {
/// <summary> /// <summary>
/// Der Haupteinstiegspunkt für die Anwendung. /// Der Haupteinstiegspunkt für die Anwendung.
/// </summary> /// </summary>
[STAThread] [STAThread]
static void Main() private static void Main()
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);

View File

@ -1,5 +1,4 @@
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden // Allgemeine Informationen über eine Assembly werden über die folgenden

View File

@ -1,14 +1,13 @@
using System; using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Windows.Forms; using System.Windows.Forms;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using Image = System.Drawing.Image; using Image = System.Drawing.Image;
namespace SpotifyAPI.Example namespace SpotifyAPI.Example
@ -37,7 +36,7 @@ namespace SpotifyAPI.Example
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent; _auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
} }
void _auth_OnResponseReceivedEvent(Token token, string state) private void _auth_OnResponseReceivedEvent(Token token, string state)
{ {
_auth.StopHttpServer(); _auth.StopHttpServer();

View File

@ -1,5 +1,4 @@
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden // Allgemeine Informationen über eine Assembly werden über die folgenden

View File

@ -1,11 +1,11 @@
using System; using Moq;
using System.IO;
using System.Linq;
using Moq;
using Newtonsoft.Json; using Newtonsoft.Json;
using NUnit.Framework; using NUnit.Framework;
using SpotifyAPI.Web; using SpotifyAPI.Web;
using SpotifyAPI.Web.Models; using SpotifyAPI.Web.Models;
using System;
using System.IO;
using System.Linq;
namespace SpotifyAPI.Tests namespace SpotifyAPI.Tests
{ {

View File

@ -162,10 +162,12 @@ namespace SpotifyAPI.Local
public void Mute() public void Mute()
{ {
//Windows < Windows Vista Check //Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6); if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check //Windows Vista Check
if (Environment.OSVersion.Version.Major == 6) if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0); if(Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
VolumeMixerControl.MuteSpotify(true); VolumeMixerControl.MuteSpotify(true);
} }
@ -175,10 +177,12 @@ namespace SpotifyAPI.Local
public void UnMute() public void UnMute()
{ {
//Windows < Windows Vista Check //Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6); if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check //Windows Vista Check
if (Environment.OSVersion.Version.Major == 6) if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0); if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
VolumeMixerControl.MuteSpotify(false); VolumeMixerControl.MuteSpotify(false);
} }
@ -186,13 +190,15 @@ namespace SpotifyAPI.Local
/// Checks whether Spotify is muted in the Volume Mixer control (required Windows 7 or newer) /// Checks whether Spotify is muted in the Volume Mixer control (required Windows 7 or newer)
/// </summary> /// </summary>
/// <returns>Null if an error occured, otherwise the muted state</returns> /// <returns>Null if an error occured, otherwise the muted state</returns>
public bool? IsSpotifyMuted() public bool IsSpotifyMuted()
{ {
//Windows < Windows Vista Check //Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6); if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check //Windows Vista Check
if (Environment.OSVersion.Version.Major == 6) if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0); if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
return VolumeMixerControl.IsSpotifyMuted(); return VolumeMixerControl.IsSpotifyMuted();
} }
@ -202,12 +208,15 @@ namespace SpotifyAPI.Local
/// <param name="volume">A value between 0 and 100</param> /// <param name="volume">A value between 0 and 100</param>
public void SetSpotifyVolume(float volume = 100) public void SetSpotifyVolume(float volume = 100)
{ {
Contract.Requires(0 <= volume && volume <= 100);
//Windows < Windows Vista Check //Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6); if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check //Windows Vista Check
if (Environment.OSVersion.Version.Major == 6) if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0); if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
if (volume < 0 || volume > 100)
throw new ArgumentOutOfRangeException("Volume parameter has to be a value between 0 and 100");
VolumeMixerControl.SetSpotifyVolume(volume); VolumeMixerControl.SetSpotifyVolume(volume);
} }
@ -215,13 +224,15 @@ namespace SpotifyAPI.Local
/// Return the Volume Mixer volume of Spotify (requires Windows 7 or newer) /// Return the Volume Mixer volume of Spotify (requires Windows 7 or newer)
/// </summary> /// </summary>
/// <returns>Null if an error occured, otherwise a float between 0 and 100</returns> /// <returns>Null if an error occured, otherwise a float between 0 and 100</returns>
public float? GetSpotifyVolume() public float GetSpotifyVolume()
{ {
//Windows < Windows Vista Check //Windows < Windows Vista Check
Contract.Requires(Environment.OSVersion.Version.Major >= 6); if (Environment.OSVersion.Version.Major < 6)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
//Windows Vista Check //Windows Vista Check
if (Environment.OSVersion.Version.Major == 6) if (Environment.OSVersion.Version.Major == 6)
Contract.Requires(Environment.OSVersion.Version.Minor != 0); if (Environment.OSVersion.Version.Minor == 0)
throw new NotSupportedException("This feature is only available on Windows 7 or newer");
return VolumeMixerControl.GetSpotifyVolume(); return VolumeMixerControl.GetSpotifyVolume();
} }

View File

@ -8,17 +8,20 @@ namespace SpotifyAPI.Local
{ {
private const String SPOTIFY_PROCESS_NAME = "spotify"; private const String SPOTIFY_PROCESS_NAME = "spotify";
internal static float? GetSpotifyVolume() internal static float GetSpotifyVolume()
{ {
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME); Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0) if (p.Length == 0)
return null; throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id; int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
return null; {
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
float level; float level;
volume.GetMasterVolume(out level); volume.GetMasterVolume(out level);
@ -26,17 +29,20 @@ namespace SpotifyAPI.Local
return level * 100; return level * 100;
} }
internal static bool? IsSpotifyMuted() internal static bool IsSpotifyMuted()
{ {
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME); Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0) if (p.Length == 0)
return null; throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id; int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
return null; {
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
bool mute; bool mute;
volume.GetMute(out mute); volume.GetMute(out mute);
@ -48,13 +54,16 @@ namespace SpotifyAPI.Local
{ {
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME); Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0) if (p.Length == 0)
return; throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id; int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
return; {
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty; Guid guid = Guid.Empty;
volume.SetMasterVolume(level / 100, ref guid); volume.SetMasterVolume(level / 100, ref guid);
@ -65,13 +74,16 @@ namespace SpotifyAPI.Local
{ {
Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME); Process[] p = Process.GetProcessesByName(SPOTIFY_PROCESS_NAME);
if (p.Length == 0) if (p.Length == 0)
return; throw new Exception("Spotify process is not running or was not found!");
int pid = p[0].Id; int pid = p[0].Id;
ISimpleAudioVolume volume = GetVolumeObject(pid); ISimpleAudioVolume volume = GetVolumeObject(pid);
if (volume == null) if (volume == null)
return; {
Marshal.ReleaseComObject(volume);
throw new COMException("Volume object creation failed");
}
Guid guid = Guid.Empty; Guid guid = Guid.Empty;
volume.SetMute(mute, ref guid); volume.SetMute(mute, ref guid);
@ -125,6 +137,7 @@ namespace SpotifyAPI.Local
[Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] [Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
private class MMDeviceEnumerator private class MMDeviceEnumerator
{ {
} }
private enum EDataFlow private enum EDataFlow