Skip to content

Commit

Permalink
Handle failed server connect. Reconnect after host change.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Minton committed May 23, 2013
1 parent bd7ae8b commit 0bfb30d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
10 changes: 10 additions & 0 deletions SpotSharp/SpotClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@ public string Playing()
public Image AlbumArt()
{
var bits = Get("/playing.png").RawBytes;
if (bits == null) return ServerDownImage();
return Image.FromStream(new MemoryStream(bits));
}

Image ServerDownImage()
{
var img = new Bitmap(300, 300);
var g = Graphics.FromImage(img);
g.Clear(Color.Red);
g.DrawString("Server not found.", new Font("Arial", 14, GraphicsUnit.Pixel),Brushes.LightYellow,new RectangleF(0,0,300,300), new StringFormat(){Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center});
return img;
}

public string Find(string songName)
{
return Post("/find", new QueryParameter("q", songName)).Content;
Expand Down
12 changes: 7 additions & 5 deletions WindowSpot/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ void Connect()
void Sync()
{
if (bgw.IsBusy)
{
MessageBox.Show("It's busy!");
return;
}

bgw.RunWorkerAsync();
}

private void SetupClicked(object sender, EventArgs e)
{
HorriblePollingTimer.Stop();
var setup = new Setup();
setup.ShowDialog(this);
if (setup.ShowDialog(this) == DialogResult.OK)
Connect();

HorriblePollingTimer.Start();
}

private void VolumeChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -92,7 +94,7 @@ private void BeginSync(object sender, System.ComponentModel.DoWorkEventArgs e)
{
Playing = _spot.Playing(),
Image = _spot.AlbumArt(),
Volume = Convert.ToInt16(_spot.Volume())
Volume = string.IsNullOrEmpty(_spot.Volume()) ? 0 : Convert.ToInt16(_spot.Volume())
};
e.Result = state;
}
Expand Down
2 changes: 2 additions & 0 deletions WindowSpot/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ private void btnTest_Click(object sender, EventArgs e)

private void SaveClicked(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Properties.Settings.Default.Host = txtHost.Text;
Properties.Settings.Default.Save();
Close();
}

private void Setup_Load(object sender, EventArgs e)
Expand Down

0 comments on commit 0bfb30d

Please sign in to comment.