more refactoring
Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>
This commit is contained in:
parent
60271802b0
commit
07982a5697
@ -13,14 +13,12 @@ namespace KaraokePlayer.Classes
|
||||
{
|
||||
public class FirebaseSong : ISong
|
||||
{
|
||||
public string FirebaseId { get; set; }
|
||||
public int Id { get; set; }
|
||||
public int Order { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Artist { get; set; }
|
||||
public FileType FileType { get; set; }
|
||||
public string Path { get; set; }
|
||||
|
||||
public string Description { get { return Artist + " - " + Title; } }
|
||||
}
|
||||
|
||||
@ -31,44 +29,59 @@ namespace KaraokePlayer.Classes
|
||||
AuthSecret = "wj0ERDFZqNSysTtIXcCgCr8Itahr6pJOBeqCjvDF",
|
||||
BasePath = "https://karaokecontroller.firebaseio.com/"
|
||||
};
|
||||
private IFirebaseClient client;
|
||||
private ControllerStateChangedEventHandler StateChanged;
|
||||
private ControllerSongChangedEventHandler SongChanged;
|
||||
private ControllerPlayQueueChangedEventHandler PlayQueueChanged;
|
||||
public FirebaseController(ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged, ControllerPlayQueueChangedEventHandler playQueueChanged)
|
||||
private string _state = "stop";
|
||||
private IFirebaseClient _client;
|
||||
private ControllerStateChangedEventHandler _stateChanged;
|
||||
private ControllerSongChangedEventHandler _songChanged;
|
||||
private ControllerPlayQueueChangedEventHandler _playQueueChanged;
|
||||
|
||||
private string StatePath
|
||||
{
|
||||
StateChanged = stateChanged;
|
||||
SongChanged = songChanged;
|
||||
PlayQueueChanged = playQueueChanged;
|
||||
PlayQueue = new List<ISong>();
|
||||
client = new FirebaseClient(config);
|
||||
client.DeleteAsync("controller/state");
|
||||
client.PushAsync("controller/state", "stop");
|
||||
client.DeleteAsync("controller/playQueue");
|
||||
client.DeleteAsync("controller/currentSong");
|
||||
ListenToStream();
|
||||
get { return string.Format("Controllers/{0}/State", this.Id); }
|
||||
}
|
||||
private string PlayQueuePath
|
||||
{
|
||||
get { return string.Format("Controllers/{0}/PlayQueue", this.Id); }
|
||||
}
|
||||
private string CurrentSongPath
|
||||
{
|
||||
get { return string.Format("Controllers/{0}/CurrentSong", this.Id); }
|
||||
}
|
||||
|
||||
public int Id { get; set; }
|
||||
public ISong CurrentSong { 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();
|
||||
Stop();
|
||||
client.DeleteAsync("controller/currentSong");
|
||||
client.PushAsync("controller/currentSong", song);
|
||||
if(song != null) { PlaySong(song); }
|
||||
}
|
||||
|
||||
public void PlaySong(ISong song)
|
||||
{
|
||||
client.DeleteAsync("controller/currentSong");
|
||||
client.PushAsync("controller/currentSong", song);
|
||||
}
|
||||
public void AddSongToQueue(ISong song)
|
||||
{
|
||||
client.PushAsync("controller/playQueue", song);
|
||||
}
|
||||
public void PlaySong(ISong song) { Update(string.Format("Controllers/{0}", this.Id), new { CurrentSong = song }); }
|
||||
|
||||
public void AddSong(ISong song) { Update(string.Format("Controllers/{0}/PlayQueue/{1}", this.Id, song.Id), song); }
|
||||
|
||||
public void RemoveSong(ISong song)
|
||||
{
|
||||
@ -76,128 +89,138 @@ namespace KaraokePlayer.Classes
|
||||
if (found != null)
|
||||
{
|
||||
PlayQueue.Remove(found);
|
||||
client.DeleteAsync("controller/playQueue/" + ((FirebaseSong)song).FirebaseId);
|
||||
Delete(PlayQueuePath + "/" + song.Id);
|
||||
}
|
||||
}
|
||||
public void Next()
|
||||
{
|
||||
client.DeleteAsync("controller/state");
|
||||
client.PushAsync("controller/state", "next");
|
||||
}
|
||||
|
||||
public void Play()
|
||||
public void SkipSong()
|
||||
{
|
||||
client.DeleteAsync("controller/state");
|
||||
client.PushAsync("controller/state", "play");
|
||||
ISong song = null;
|
||||
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 Stop()
|
||||
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");
|
||||
client.PushAsync("controller/state", "stop");
|
||||
Stop();
|
||||
Delete(PlayQueuePath);
|
||||
Delete(CurrentSongPath);
|
||||
}
|
||||
|
||||
public void Pause()
|
||||
private async void SetupListener()
|
||||
{
|
||||
client.DeleteAsync("controller/state");
|
||||
client.PushAsync("controller/state", "pause");
|
||||
}
|
||||
|
||||
private async void ListenToStream()
|
||||
{
|
||||
await client.OnAsync("controller/currentSong",
|
||||
await _client.OnAsync(CurrentSongPath,
|
||||
added: (s, args, obj) =>
|
||||
{
|
||||
if (args.Path.Contains("Id"))
|
||||
{
|
||||
CurrentSongChanged();
|
||||
CurrentSongDidChange();
|
||||
}
|
||||
},
|
||||
changed: (s, args, obj) =>
|
||||
{
|
||||
if (args.Path.Contains("Id"))
|
||||
{
|
||||
CurrentSongChanged();
|
||||
CurrentSongDidChange();
|
||||
}
|
||||
},
|
||||
removed: null
|
||||
);
|
||||
|
||||
await client.OnAsync("controller/playQueue",
|
||||
await _client.OnAsync(PlayQueuePath,
|
||||
added: (s, args, obj) =>
|
||||
{
|
||||
if (args.Path.Contains("Id"))
|
||||
{
|
||||
ReloadPlayQueue();
|
||||
PlayQueueDidChange();
|
||||
}
|
||||
},
|
||||
changed: (s, args, obj) =>
|
||||
{
|
||||
if (args.Path.Contains("Id"))
|
||||
{
|
||||
ReloadPlayQueue();
|
||||
PlayQueueDidChange();
|
||||
}
|
||||
},
|
||||
removed: (s, args, obj) =>
|
||||
{
|
||||
ReloadPlayQueue();
|
||||
PlayQueueDidChange();
|
||||
}
|
||||
);
|
||||
|
||||
await client.OnAsync("controller/state",
|
||||
await _client.OnAsync(StatePath,
|
||||
added: (s, args, obj) =>
|
||||
{
|
||||
RemoteStateChanged(args.Data);
|
||||
StateDidChange(args.Data);
|
||||
},
|
||||
changed: (s, args, obj) =>
|
||||
{
|
||||
RemoteStateChanged(args.Data);
|
||||
StateDidChange(args.Data);
|
||||
},
|
||||
removed: null
|
||||
);
|
||||
}
|
||||
|
||||
private void ReloadPlayQueue()
|
||||
private void PlayQueueDidChange()
|
||||
{
|
||||
bool autoPlay = PlayQueue.Count() == 0;
|
||||
var response = client.Get("controller/playQueue");
|
||||
var response = _client.Get(PlayQueuePath);
|
||||
var dict = response.ResultAs<Dictionary<string, FirebaseSong>>();
|
||||
|
||||
PlayQueue.Clear();
|
||||
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();
|
||||
PlayQueue.AddRange(array);
|
||||
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 dict = response.ResultAs<Dictionary<string, FirebaseSong>>();
|
||||
if (dict != null)
|
||||
{
|
||||
var song = dict.Values.FirstOrDefault();
|
||||
var response = _client.Get(CurrentSongPath);
|
||||
var song = response.ResultAs<FirebaseSong>();
|
||||
if (song != null)
|
||||
{
|
||||
CurrentSong = song;
|
||||
SongChanged(new ControllerSongChangedEventArgs(false, song));
|
||||
}
|
||||
_songChanged(new ControllerSongChangedEventArgs(false, song));
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoteStateChanged(string state)
|
||||
private void StateDidChange(string state)
|
||||
{
|
||||
_state = state;
|
||||
PlayerState s = PlayerState.Play;
|
||||
if (state.ToLower() == "pause")
|
||||
{
|
||||
@ -211,7 +234,7 @@ namespace KaraokePlayer.Classes
|
||||
{
|
||||
s = PlayerState.Next;
|
||||
}
|
||||
StateChanged(new ControllerStateChangedEventArgs(s));
|
||||
_stateChanged(new ControllerStateChangedEventArgs(s));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -21,9 +21,13 @@ namespace KaraokePlayer.Interfaces
|
||||
public interface IController
|
||||
{
|
||||
int Id { get; set; }
|
||||
ISong CurrentSong { get; set; }
|
||||
string State { get; set; }
|
||||
List<ISong> PlayQueue { get; set; }
|
||||
void GetNextSong();
|
||||
void NextSong();
|
||||
void RemoveSong(ISong song);
|
||||
void AddSong(ISong song);
|
||||
void SkipSong();
|
||||
void Play();
|
||||
void Pause();
|
||||
void Stop();
|
||||
|
||||
@ -30,7 +30,7 @@ namespace KaraokePlayer
|
||||
|
||||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
|
||||
this.WindowState = FormWindowState.Maximized;
|
||||
this.ShowInTaskbar = false;
|
||||
this.ShowInTaskbar = true;
|
||||
karaokeCDGPlayer.Dock = DockStyle.Fill;
|
||||
karaokeMP4Player.Dock = DockStyle.Fill;
|
||||
|
||||
@ -81,7 +81,7 @@ namespace KaraokePlayer
|
||||
private void next()
|
||||
{
|
||||
controller.RemoveSong(currentSong);
|
||||
controller.GetNextSong();
|
||||
controller.NextSong();
|
||||
}
|
||||
|
||||
private void stop()
|
||||
|
||||
13
TestSelector/Form1.Designer.cs
generated
13
TestSelector/Form1.Designer.cs
generated
@ -39,6 +39,7 @@
|
||||
this.listBox2 = new System.Windows.Forms.ListBox();
|
||||
this.button6 = new System.Windows.Forms.Button();
|
||||
this.button7 = new System.Windows.Forms.Button();
|
||||
this.button8 = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// textBox1
|
||||
@ -135,11 +136,22 @@
|
||||
this.button7.UseVisualStyleBackColor = true;
|
||||
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
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(575, 325);
|
||||
this.Controls.Add(this.button8);
|
||||
this.Controls.Add(this.button7);
|
||||
this.Controls.Add(this.button6);
|
||||
this.Controls.Add(this.listBox2);
|
||||
@ -170,6 +182,7 @@
|
||||
private System.Windows.Forms.ListBox listBox2;
|
||||
private System.Windows.Forms.Button button6;
|
||||
private System.Windows.Forms.Button button7;
|
||||
private System.Windows.Forms.Button button8;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -36,6 +36,10 @@ namespace TestSelector
|
||||
{
|
||||
this.Invoke(new Action(() => { buttonUpdateForPlay(); }));
|
||||
}
|
||||
else if (args.State == KaraokePlayer.Enums.PlayerState.Stop)
|
||||
{
|
||||
this.Invoke(new Action(() => { buttonUpdateForPlay(); }));
|
||||
}
|
||||
},
|
||||
songChanged: (args) =>
|
||||
{
|
||||
@ -115,7 +119,7 @@ namespace TestSelector
|
||||
song.Title = tag.Tag.Title;
|
||||
song.Artist = tag.Tag.Performers[0];
|
||||
song.Path = file.FullName.Replace(@"D:\KaraokeData\Karaoke\", @"Z:\");
|
||||
controller.AddSongToQueue(song);
|
||||
controller.AddSong(song);
|
||||
}
|
||||
|
||||
private void button3_Click(object sender, EventArgs e)
|
||||
@ -137,7 +141,7 @@ namespace TestSelector
|
||||
private void button5_Click(object sender, EventArgs e)
|
||||
{
|
||||
controller.RemoveSong(controller.CurrentSong);
|
||||
controller.GetNextSong();
|
||||
controller.NextSong();
|
||||
}
|
||||
|
||||
private void button6_Click(object sender, EventArgs e)
|
||||
@ -151,5 +155,10 @@ namespace TestSelector
|
||||
ISong song = (ISong)listBox2.SelectedItem;
|
||||
controller.RemoveSong(song);
|
||||
}
|
||||
|
||||
private void button8_Click(object sender, EventArgs e)
|
||||
{
|
||||
controller.SkipSong();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user