more refactoring

Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
Matt Bruce 2016-07-24 23:01:58 -05:00
parent 60271802b0
commit 07982a5697
5 changed files with 134 additions and 85 deletions

View File

@ -13,14 +13,12 @@ namespace KaraokePlayer.Classes
{ {
public class FirebaseSong : ISong public class FirebaseSong : ISong
{ {
public string FirebaseId { get; set; }
public int Id { get; set; } public int Id { get; set; }
public int Order { get; set; } public int Order { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Artist { get; set; } public string Artist { get; set; }
public FileType FileType { get; set; } public FileType FileType { get; set; }
public string Path { get; set; } public string Path { get; set; }
public string Description { get { return Artist + " - " + Title; } } public string Description { get { return Artist + " - " + Title; } }
} }
@ -31,44 +29,59 @@ namespace KaraokePlayer.Classes
AuthSecret = "wj0ERDFZqNSysTtIXcCgCr8Itahr6pJOBeqCjvDF", AuthSecret = "wj0ERDFZqNSysTtIXcCgCr8Itahr6pJOBeqCjvDF",
BasePath = "https://karaokecontroller.firebaseio.com/" BasePath = "https://karaokecontroller.firebaseio.com/"
}; };
private IFirebaseClient client; private string _state = "stop";
private ControllerStateChangedEventHandler StateChanged; private IFirebaseClient _client;
private ControllerSongChangedEventHandler SongChanged; private ControllerStateChangedEventHandler _stateChanged;
private ControllerPlayQueueChangedEventHandler PlayQueueChanged; private ControllerSongChangedEventHandler _songChanged;
public FirebaseController(ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged, ControllerPlayQueueChangedEventHandler playQueueChanged) private ControllerPlayQueueChangedEventHandler _playQueueChanged;
private string StatePath
{ {
StateChanged = stateChanged; get { return string.Format("Controllers/{0}/State", this.Id); }
SongChanged = songChanged; }
PlayQueueChanged = playQueueChanged; private string PlayQueuePath
PlayQueue = new List<ISong>(); {
client = new FirebaseClient(config); get { return string.Format("Controllers/{0}/PlayQueue", this.Id); }
client.DeleteAsync("controller/state"); }
client.PushAsync("controller/state", "stop"); private string CurrentSongPath
client.DeleteAsync("controller/playQueue"); {
client.DeleteAsync("controller/currentSong"); get { return string.Format("Controllers/{0}/CurrentSong", this.Id); }
ListenToStream();
} }
public int Id { get; set; } public int Id { get; set; }
public ISong CurrentSong { get; set; } public ISong CurrentSong { get; set; }
public List<ISong> PlayQueue { get; set; } public List<ISong> PlayQueue { get; set; }
public void GetNextSong()
public string State {
get { return State; }
set {
_state = value;
Update(string.Format("Controllers/{0}", this.Id), new { State = _state });
}
}
public FirebaseController(int clientId = 1, ControllerStateChangedEventHandler stateChanged = null, ControllerSongChangedEventHandler songChanged = null, ControllerPlayQueueChangedEventHandler playQueueChanged = null)
{
Id = clientId;
_stateChanged = stateChanged;
_songChanged = songChanged;
_playQueueChanged = playQueueChanged;
PlayQueue = new List<ISong>();
_client = new FirebaseClient(config);
Reset();
SetupListener();
}
public void NextSong()
{ {
ISong song = PlayQueue.FirstOrDefault(); ISong song = PlayQueue.FirstOrDefault();
Stop(); Stop();
client.DeleteAsync("controller/currentSong"); if(song != null) { PlaySong(song); }
client.PushAsync("controller/currentSong", song);
} }
public void PlaySong(ISong song) public void PlaySong(ISong song) { Update(string.Format("Controllers/{0}", this.Id), new { CurrentSong = song }); }
{
client.DeleteAsync("controller/currentSong"); public void AddSong(ISong song) { Update(string.Format("Controllers/{0}/PlayQueue/{1}", this.Id, song.Id), song); }
client.PushAsync("controller/currentSong", song);
}
public void AddSongToQueue(ISong song)
{
client.PushAsync("controller/playQueue", song);
}
public void RemoveSong(ISong song) public void RemoveSong(ISong song)
{ {
@ -76,128 +89,138 @@ namespace KaraokePlayer.Classes
if (found != null) if (found != null)
{ {
PlayQueue.Remove(found); PlayQueue.Remove(found);
client.DeleteAsync("controller/playQueue/" + ((FirebaseSong)song).FirebaseId); Delete(PlayQueuePath + "/" + song.Id);
} }
} }
public void Next()
public void SkipSong()
{ {
client.DeleteAsync("controller/state"); ISong song = null;
client.PushAsync("controller/state", "next"); int count = PlayQueue.Count();
int index = PlayQueue.FindIndex(s => s.Id == CurrentSong.Id);
if (index + 1 == count && count > 1)
{
song = PlayQueue.First();
}
else {
if (PlayQueue.Count() > index)
{
song = PlayQueue[index + 1];
}
}
Stop();
if(song != null)
{
PlaySong(song);
}
} }
public void Play() public void Next() { this.State = "Next"; }
public void Play() { this.State = "Play"; }
public void Stop() { this.State = "Stop"; }
public void Pause() { this.State = "Pause"; }
private void Delete(string path) { _client.DeleteAsync(path); }
private void Update(string path, object data) { _client.UpdateAsync(path, data); }
private void Reset()
{ {
client.DeleteAsync("controller/state"); Stop();
client.PushAsync("controller/state", "play"); Delete(PlayQueuePath);
Delete(CurrentSongPath);
} }
public void Stop() private async void SetupListener()
{ {
client.DeleteAsync("controller/state"); await _client.OnAsync(CurrentSongPath,
client.PushAsync("controller/state", "stop");
}
public void Pause()
{
client.DeleteAsync("controller/state");
client.PushAsync("controller/state", "pause");
}
private async void ListenToStream()
{
await client.OnAsync("controller/currentSong",
added: (s, args, obj) => added: (s, args, obj) =>
{ {
if (args.Path.Contains("Id")) if (args.Path.Contains("Id"))
{ {
CurrentSongChanged(); CurrentSongDidChange();
} }
}, },
changed: (s, args, obj) => changed: (s, args, obj) =>
{ {
if (args.Path.Contains("Id")) if (args.Path.Contains("Id"))
{ {
CurrentSongChanged(); CurrentSongDidChange();
} }
}, },
removed: null removed: null
); );
await client.OnAsync("controller/playQueue", await _client.OnAsync(PlayQueuePath,
added: (s, args, obj) => added: (s, args, obj) =>
{ {
if (args.Path.Contains("Id")) if (args.Path.Contains("Id"))
{ {
ReloadPlayQueue(); PlayQueueDidChange();
} }
}, },
changed: (s, args, obj) => changed: (s, args, obj) =>
{ {
if (args.Path.Contains("Id")) if (args.Path.Contains("Id"))
{ {
ReloadPlayQueue(); PlayQueueDidChange();
} }
}, },
removed: (s, args, obj) => removed: (s, args, obj) =>
{ {
ReloadPlayQueue(); PlayQueueDidChange();
} }
); );
await client.OnAsync("controller/state", await _client.OnAsync(StatePath,
added: (s, args, obj) => added: (s, args, obj) =>
{ {
RemoteStateChanged(args.Data); StateDidChange(args.Data);
}, },
changed: (s, args, obj) => changed: (s, args, obj) =>
{ {
RemoteStateChanged(args.Data); StateDidChange(args.Data);
}, },
removed: null removed: null
); );
} }
private void ReloadPlayQueue() private void PlayQueueDidChange()
{ {
bool autoPlay = PlayQueue.Count() == 0; bool autoPlay = PlayQueue.Count() == 0;
var response = client.Get("controller/playQueue"); var response = _client.Get(PlayQueuePath);
var dict = response.ResultAs<Dictionary<string, FirebaseSong>>(); var dict = response.ResultAs<Dictionary<string, FirebaseSong>>();
PlayQueue.Clear(); PlayQueue.Clear();
if (dict != null && dict.Count() > 0) if (dict != null && dict.Count() > 0)
{ {
foreach (KeyValuePair<string, FirebaseSong> entry in dict)
{
entry.Value.FirebaseId = entry.Key;
}
var array = dict.Values.OrderBy(s => s.Order).ToArray(); var array = dict.Values.OrderBy(s => s.Order).ToArray();
PlayQueue.AddRange(array); PlayQueue.AddRange(array);
if (autoPlay) if (autoPlay)
{ {
GetNextSong(); NextSong();
} }
} }
if (PlayQueueChanged != null) { PlayQueueChanged(); } if (_playQueueChanged != null) { _playQueueChanged(); }
} }
private void CurrentSongChanged() private void CurrentSongDidChange()
{ {
var response = client.Get("controller/currentSong"); var response = _client.Get(CurrentSongPath);
var dict = response.ResultAs<Dictionary<string, FirebaseSong>>(); var song = response.ResultAs<FirebaseSong>();
if (dict != null) if (song != null)
{ {
var song = dict.Values.FirstOrDefault(); CurrentSong = song;
if (song != null) _songChanged(new ControllerSongChangedEventArgs(false, song));
{
CurrentSong = song;
SongChanged(new ControllerSongChangedEventArgs(false, song));
}
} }
} }
private void RemoteStateChanged(string state) private void StateDidChange(string state)
{ {
_state = state;
PlayerState s = PlayerState.Play; PlayerState s = PlayerState.Play;
if (state.ToLower() == "pause") if (state.ToLower() == "pause")
{ {
@ -211,7 +234,7 @@ namespace KaraokePlayer.Classes
{ {
s = PlayerState.Next; s = PlayerState.Next;
} }
StateChanged(new ControllerStateChangedEventArgs(s)); _stateChanged(new ControllerStateChangedEventArgs(s));
} }
} }

View File

@ -21,9 +21,13 @@ namespace KaraokePlayer.Interfaces
public interface IController public interface IController
{ {
int Id { get; set; } int Id { get; set; }
ISong CurrentSong { get; set; }
string State { get; set; }
List<ISong> PlayQueue { get; set; } List<ISong> PlayQueue { get; set; }
void GetNextSong(); void NextSong();
void RemoveSong(ISong song); void RemoveSong(ISong song);
void AddSong(ISong song);
void SkipSong();
void Play(); void Play();
void Pause(); void Pause();
void Stop(); void Stop();

View File

@ -30,7 +30,7 @@ namespace KaraokePlayer
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized; this.WindowState = FormWindowState.Maximized;
this.ShowInTaskbar = false; this.ShowInTaskbar = true;
karaokeCDGPlayer.Dock = DockStyle.Fill; karaokeCDGPlayer.Dock = DockStyle.Fill;
karaokeMP4Player.Dock = DockStyle.Fill; karaokeMP4Player.Dock = DockStyle.Fill;
@ -81,7 +81,7 @@ namespace KaraokePlayer
private void next() private void next()
{ {
controller.RemoveSong(currentSong); controller.RemoveSong(currentSong);
controller.GetNextSong(); controller.NextSong();
} }
private void stop() private void stop()

View File

@ -39,6 +39,7 @@
this.listBox2 = new System.Windows.Forms.ListBox(); this.listBox2 = new System.Windows.Forms.ListBox();
this.button6 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.SuspendLayout(); this.SuspendLayout();
// //
// textBox1 // textBox1
@ -135,11 +136,22 @@
this.button7.UseVisualStyleBackColor = true; this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click); this.button7.Click += new System.EventHandler(this.button7_Click);
// //
// button8
//
this.button8.Location = new System.Drawing.Point(488, 155);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(75, 23);
this.button8.TabIndex = 10;
this.button8.Text = "Skip";
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(575, 325); this.ClientSize = new System.Drawing.Size(575, 325);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7); this.Controls.Add(this.button7);
this.Controls.Add(this.button6); this.Controls.Add(this.button6);
this.Controls.Add(this.listBox2); this.Controls.Add(this.listBox2);
@ -170,6 +182,7 @@
private System.Windows.Forms.ListBox listBox2; private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7; private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
} }
} }

View File

@ -36,6 +36,10 @@ namespace TestSelector
{ {
this.Invoke(new Action(() => { buttonUpdateForPlay(); })); this.Invoke(new Action(() => { buttonUpdateForPlay(); }));
} }
else if (args.State == KaraokePlayer.Enums.PlayerState.Stop)
{
this.Invoke(new Action(() => { buttonUpdateForPlay(); }));
}
}, },
songChanged: (args) => songChanged: (args) =>
{ {
@ -115,7 +119,7 @@ namespace TestSelector
song.Title = tag.Tag.Title; song.Title = tag.Tag.Title;
song.Artist = tag.Tag.Performers[0]; song.Artist = tag.Tag.Performers[0];
song.Path = file.FullName.Replace(@"D:\KaraokeData\Karaoke\", @"Z:\"); song.Path = file.FullName.Replace(@"D:\KaraokeData\Karaoke\", @"Z:\");
controller.AddSongToQueue(song); controller.AddSong(song);
} }
private void button3_Click(object sender, EventArgs e) private void button3_Click(object sender, EventArgs e)
@ -137,7 +141,7 @@ namespace TestSelector
private void button5_Click(object sender, EventArgs e) private void button5_Click(object sender, EventArgs e)
{ {
controller.RemoveSong(controller.CurrentSong); controller.RemoveSong(controller.CurrentSong);
controller.GetNextSong(); controller.NextSong();
} }
private void button6_Click(object sender, EventArgs e) private void button6_Click(object sender, EventArgs e)
@ -151,5 +155,10 @@ namespace TestSelector
ISong song = (ISong)listBox2.SelectedItem; ISong song = (ISong)listBox2.SelectedItem;
controller.RemoveSong(song); controller.RemoveSong(song);
} }
private void button8_Click(object sender, EventArgs e)
{
controller.SkipSong();
}
} }
} }