2019-05-27 16:23:14 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.ObjectModel;
|
2019-07-10 22:23:19 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Linq;
|
2019-06-26 01:17:52 +00:00
|
|
|
using Xamarin.Forms;
|
2019-07-10 22:23:19 +00:00
|
|
|
using Aurora.Services;
|
|
|
|
using Aurora.Proto.General;
|
|
|
|
using Aurora.Proto.Party;
|
|
|
|
using Aurora.Proto.Events;
|
2019-11-02 19:47:41 +00:00
|
|
|
using Aurora.Services.ClientService;
|
2019-11-29 17:37:57 +00:00
|
|
|
using Aurora.Services.ClientService.Events;
|
2020-02-01 01:41:45 +00:00
|
|
|
using Aurora.Services.Player;
|
2019-11-09 19:55:09 +00:00
|
|
|
using Aurora.Services.EventManager;
|
2019-11-03 21:04:05 +00:00
|
|
|
using Aurora.Models.Media;
|
2019-12-07 18:47:45 +00:00
|
|
|
using Aurora.Design.Views.Party.NewPartyDialog;
|
2020-02-01 01:41:45 +00:00
|
|
|
using Aurora.Services.Settings;
|
2019-05-27 16:23:14 +00:00
|
|
|
|
2019-07-05 18:17:09 +00:00
|
|
|
namespace Aurora.Design.Views.Party
|
2019-05-27 16:23:14 +00:00
|
|
|
{
|
2019-11-09 19:55:09 +00:00
|
|
|
//TODO refactor
|
2019-06-26 01:17:52 +00:00
|
|
|
enum PartyState
|
|
|
|
{
|
|
|
|
SelectingHost,
|
|
|
|
InParty,
|
2019-11-07 03:32:43 +00:00
|
|
|
Hosting,
|
2019-06-26 01:17:52 +00:00
|
|
|
Connecting,
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:23:14 +00:00
|
|
|
public class PartyViewModel : BaseViewModel
|
|
|
|
{
|
2019-06-26 01:17:52 +00:00
|
|
|
private PartyState _state;
|
2019-12-10 20:10:27 +00:00
|
|
|
private string _hostname = "";
|
2019-07-05 21:37:10 +00:00
|
|
|
private ObservableCollection<PartyMember> _members;
|
2019-11-04 04:17:34 +00:00
|
|
|
private ObservableCollection<BaseMedia> _queue;
|
2019-11-09 01:25:36 +00:00
|
|
|
private BaseMedia _selectedMedia;
|
2020-02-01 01:41:45 +00:00
|
|
|
private IClientService _client;
|
|
|
|
private ISettingsService _settingsService;
|
2019-11-03 21:04:05 +00:00
|
|
|
|
2019-12-24 00:06:03 +00:00
|
|
|
private int _selectedTabIndex;
|
|
|
|
|
2020-02-01 01:41:45 +00:00
|
|
|
public PartyViewModel(ISettingsService settingsService, IClientService clientService)
|
2019-05-27 16:23:14 +00:00
|
|
|
{
|
2019-07-12 15:34:06 +00:00
|
|
|
_members = new ObservableCollection<PartyMember>();
|
2019-11-04 04:17:34 +00:00
|
|
|
_queue = new ObservableCollection<BaseMedia>();
|
2019-06-26 01:17:52 +00:00
|
|
|
|
2020-02-01 01:41:45 +00:00
|
|
|
this._settingsService = settingsService;
|
|
|
|
|
2019-07-10 22:23:19 +00:00
|
|
|
SetState(PartyState.SelectingHost);
|
2019-11-02 19:47:41 +00:00
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
PlayCommand = new Command(OnDoubleClickCommandExecute, CanDoubleClickCommandExecute);
|
2019-11-09 19:55:09 +00:00
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
LeavePartyCommand = new Command(OnLeavePartyCommandExecute, CanLeavePartyCommandExecute);
|
2020-01-14 00:57:02 +00:00
|
|
|
|
2020-02-01 01:41:45 +00:00
|
|
|
_client = clientService;
|
2019-11-30 01:51:48 +00:00
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
_client.OnMediaPaused += this.OnRemoteMediaPaused;
|
|
|
|
_client.OnMediaResumed += this.OnRemoteMediaResumed;
|
|
|
|
_client.OnNewMediaPlaying += this.OnNewRemoteMediaPlaying;
|
2019-11-30 01:51:48 +00:00
|
|
|
_client.OnPartyMemberJoined += this.OnPartyMemberJoined;
|
|
|
|
_client.OnPartyMemberLeft += this.OnPartyMemberLeft;
|
2020-01-14 00:57:02 +00:00
|
|
|
|
2019-05-27 16:23:14 +00:00
|
|
|
}
|
2019-07-07 21:12:13 +00:00
|
|
|
|
|
|
|
~PartyViewModel()
|
|
|
|
{
|
2019-11-02 19:47:41 +00:00
|
|
|
//Task.Run(ServerService.Instance.Stop);
|
2019-07-07 21:12:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 01:17:52 +00:00
|
|
|
#region Properties
|
2019-05-27 16:23:14 +00:00
|
|
|
|
2019-12-24 00:06:03 +00:00
|
|
|
public int SelectedTabIndex
|
|
|
|
{
|
|
|
|
get { return _selectedTabIndex; }
|
|
|
|
set { SetProperty(ref _selectedTabIndex, value); }
|
|
|
|
}
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Publc property for the members list
|
|
|
|
/// </summary>
|
|
|
|
/// <value></value>
|
2019-07-05 21:37:10 +00:00
|
|
|
public ObservableCollection<PartyMember> Members
|
2019-05-27 16:23:14 +00:00
|
|
|
{
|
2019-07-05 21:37:10 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return _members;
|
|
|
|
}
|
2019-07-10 21:17:10 +00:00
|
|
|
set
|
|
|
|
{
|
|
|
|
SetProperty(ref _members, value);
|
|
|
|
}
|
2019-05-27 16:23:14 +00:00
|
|
|
}
|
2019-06-26 01:17:52 +00:00
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Public property for queue item source
|
|
|
|
/// </summary>
|
|
|
|
/// <value></value>
|
2019-11-04 04:17:34 +00:00
|
|
|
public ObservableCollection<BaseMedia> Queue
|
2019-11-03 21:04:05 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _queue;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value != _queue)
|
|
|
|
{
|
|
|
|
SetProperty(ref _queue, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Public property for the currently selected song.
|
|
|
|
/// </summary>
|
|
|
|
/// <value></value>
|
2019-11-04 04:17:34 +00:00
|
|
|
public BaseMedia SelectedSong
|
|
|
|
{
|
2019-11-09 01:25:36 +00:00
|
|
|
get { return _selectedMedia; }
|
|
|
|
set { SetProperty(ref _selectedMedia, value); }
|
2019-11-04 04:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Public property for playing media
|
|
|
|
/// </summary>
|
|
|
|
/// <value></value>
|
2019-11-04 04:17:34 +00:00
|
|
|
public Command PlayCommand { get; private set; }
|
|
|
|
|
2020-01-14 00:57:02 +00:00
|
|
|
public Command LeavePartyCommand { get; private set; }
|
|
|
|
|
2019-06-26 01:17:52 +00:00
|
|
|
#endregion Properties
|
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
#region Events
|
2019-11-29 17:37:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Called by framework when view becomes active
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override async Task OnActive()
|
2019-11-07 03:32:43 +00:00
|
|
|
{
|
2019-12-24 00:06:03 +00:00
|
|
|
OnPropertyChanged("SelectedTabIndex");
|
2019-11-30 01:51:48 +00:00
|
|
|
if (this._state == PartyState.Hosting ||
|
|
|
|
this._state == PartyState.InParty)
|
2019-11-29 17:37:57 +00:00
|
|
|
{
|
2019-11-30 01:51:48 +00:00
|
|
|
await _client.GetEvents().ConfigureAwait(false);
|
2019-11-29 17:37:57 +00:00
|
|
|
}
|
2019-12-07 18:47:45 +00:00
|
|
|
else
|
|
|
|
{
|
2019-12-10 20:10:27 +00:00
|
|
|
//Open host selection modal
|
2019-12-19 03:32:08 +00:00
|
|
|
NewPartyDialogViewModel vm = new NewPartyDialogViewModel();
|
|
|
|
ConnectionDetails details = new ConnectionDetails();
|
|
|
|
vm.Finish = () =>
|
2019-12-10 20:10:27 +00:00
|
|
|
{
|
2019-12-19 03:32:08 +00:00
|
|
|
this.HideModal();
|
|
|
|
details = vm.ReturnObject as ConnectionDetails;
|
2019-12-10 20:10:27 +00:00
|
|
|
_hostname = details.HostName;
|
|
|
|
switch (details.ConnectionType)
|
|
|
|
{
|
|
|
|
case ConnectionType.Host:
|
|
|
|
{
|
2020-01-18 23:07:31 +00:00
|
|
|
OnHostCommandExecute();
|
2019-12-10 20:10:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ConnectionType.Join:
|
|
|
|
{
|
2020-01-18 23:07:31 +00:00
|
|
|
OnJoinCommandExecute();
|
2019-12-10 20:10:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 03:32:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.ShowModal(typeof(NewPartyDialog.NewPartyDialog), vm);
|
2019-12-07 18:47:45 +00:00
|
|
|
}
|
2019-11-07 03:32:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 17:37:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Called by framework when view becomes inactive
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
2019-11-30 03:04:55 +00:00
|
|
|
public override Task OnInactive()
|
2019-11-07 03:32:43 +00:00
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
_client.StopEvents();
|
2019-11-30 03:04:55 +00:00
|
|
|
return Task.FromResult<object>(null);
|
2019-11-29 17:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Remote media paused event
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
/// <param name="args"></param>
|
2020-01-18 23:07:31 +00:00
|
|
|
public void OnRemoteMediaPaused(object sender, MediaPausedEventArgs args)
|
2019-11-29 17:37:57 +00:00
|
|
|
{
|
|
|
|
StopPlaying();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Remote playing new media event
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
/// <param name="args"></param>
|
2020-01-18 23:07:31 +00:00
|
|
|
public void OnNewRemoteMediaPlaying(object sender, NewMediaPlayingEventArgs args)
|
2019-11-29 17:37:57 +00:00
|
|
|
{
|
|
|
|
PlayFromBeginning(GetMediaFromQueue(args.Event.Media.Id));
|
2019-11-07 03:32:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
/// <summary>
|
2019-11-29 17:37:57 +00:00
|
|
|
/// Remote resumed playing event
|
2019-11-02 19:47:41 +00:00
|
|
|
/// </summary>
|
2019-11-29 17:37:57 +00:00
|
|
|
/// <param name="sender"></param>
|
|
|
|
/// <param name="args"></param>
|
2020-01-18 23:07:31 +00:00
|
|
|
public void OnRemoteMediaResumed(object sender, MediaResumedEventArgs args)
|
2019-11-02 19:47:41 +00:00
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
PlayResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Member joined party event
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
/// <param name="args"></param>
|
|
|
|
public void OnPartyMemberJoined(object sender, PartyMemberJoinedEventArgs args)
|
|
|
|
{
|
|
|
|
PartyMember member = new PartyMember
|
2019-11-02 19:47:41 +00:00
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
UserName = args.Event.Member.UserName,
|
|
|
|
Id = args.Event.Member.Id,
|
|
|
|
IpAddress = args.Event.Member.IpAddress,
|
|
|
|
Port = args.Event.Member.Port
|
|
|
|
};
|
2019-11-02 19:47:41 +00:00
|
|
|
|
2019-11-29 17:37:57 +00:00
|
|
|
Members.Add(member);
|
|
|
|
}
|
2019-11-02 19:47:41 +00:00
|
|
|
|
2019-11-29 17:37:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Member left party event
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
/// <param name="args"></param>
|
|
|
|
public void OnPartyMemberLeft(object sender, PartyMemberLeftEventArgs args)
|
|
|
|
{
|
|
|
|
var found = Members.Where(x => x.Id == args.Event.Member.Id);
|
|
|
|
foreach (PartyMember member in found)
|
|
|
|
{
|
|
|
|
_members.Remove(member);
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion Events
|
|
|
|
|
2019-06-26 01:17:52 +00:00
|
|
|
#region Commands
|
2020-01-18 23:07:31 +00:00
|
|
|
private async void OnJoinCommandExecute()
|
2019-06-26 01:17:52 +00:00
|
|
|
{
|
2019-11-07 03:32:43 +00:00
|
|
|
SetState(PartyState.Connecting);
|
2020-02-01 01:41:45 +00:00
|
|
|
_client.Start(_hostname, this._settingsService.DefaultPort.ToString());
|
2019-11-13 01:09:45 +00:00
|
|
|
await JoinParty(false);
|
2019-07-06 19:52:28 +00:00
|
|
|
|
2019-11-10 20:33:06 +00:00
|
|
|
//TODO add cancellation token
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SetState(PartyState.InParty);
|
|
|
|
await _client.GetEvents().ConfigureAwait(true);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine("Exception occurred while receiviing events: ", ex.Message);
|
2019-11-10 20:33:06 +00:00
|
|
|
}
|
2019-06-26 01:17:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
private bool CanJoinCommandExecute()
|
2019-06-26 01:17:52 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
private async void OnHostCommandExecute()
|
2019-06-26 01:17:52 +00:00
|
|
|
{
|
2019-11-07 03:32:43 +00:00
|
|
|
//Change state
|
|
|
|
SetState(PartyState.Connecting);
|
2019-07-10 22:23:19 +00:00
|
|
|
ServerService.Instance.Start();
|
|
|
|
string localHost = ServerService.GetLocalIPAddress();
|
2019-11-09 19:55:09 +00:00
|
|
|
_client.IsHost = true;
|
2020-02-01 01:41:45 +00:00
|
|
|
_client.Start(localHost, this._settingsService.DefaultPort.ToString());
|
2019-11-13 01:09:45 +00:00
|
|
|
await JoinParty(true);
|
2019-11-02 19:47:41 +00:00
|
|
|
|
2019-11-09 19:55:09 +00:00
|
|
|
|
|
|
|
//TODO add cancellation token
|
2019-11-07 03:32:43 +00:00
|
|
|
try
|
|
|
|
{
|
2019-11-09 01:25:36 +00:00
|
|
|
SetState(PartyState.Hosting);
|
2019-11-09 19:55:09 +00:00
|
|
|
await _client.GetEvents().ConfigureAwait(true);
|
2019-11-07 03:32:43 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine("Exception occurred while receiviing events: ", ex.Message);
|
2019-11-07 03:32:43 +00:00
|
|
|
}
|
2019-06-26 01:17:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
private bool CanHostCommandExecute()
|
2019-06-26 01:17:52 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
private async void OnLeavePartyCommandExecute()
|
2020-01-14 00:57:02 +00:00
|
|
|
{
|
|
|
|
await _client.RemotePartyClient.LeavePartyAsync(new LeavePartyRequest());
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
private bool CanLeavePartyCommandExecute()
|
2020-01-14 00:57:02 +00:00
|
|
|
{
|
|
|
|
return (this._state == PartyState.InParty || this._state == PartyState.Hosting) ? true : false;
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
public override void OnPlayButtonCommandExecute()
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
2019-12-05 04:42:23 +00:00
|
|
|
if (base.IsPlaying())
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
2019-12-05 04:42:23 +00:00
|
|
|
//Fire play stopped event
|
|
|
|
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
|
|
|
MediaPausedEvent mediaPaused = new MediaPausedEvent();
|
|
|
|
|
|
|
|
EventManager.Instance.FireEvent(new BaseEvent()
|
|
|
|
{
|
|
|
|
MediaPausedEvent = mediaPaused
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Fire play resume event
|
|
|
|
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
|
|
|
MediaResumedEvent mediaResumed = new MediaResumedEvent();
|
|
|
|
|
|
|
|
EventManager.Instance.FireEvent(new BaseEvent()
|
|
|
|
{
|
|
|
|
MediaResumedEvent = mediaResumed
|
|
|
|
});
|
2019-11-09 19:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
public override bool CanPlayButtonCommandExecute()
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
2019-12-05 04:42:23 +00:00
|
|
|
return this._state == PartyState.Hosting;
|
2019-11-09 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
public override bool CanNextButtonCommandExecute()
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
|
|
|
return this._state == PartyState.Hosting;
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
public override bool CanPreviousButtonCommandExecute()
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
|
|
|
return this._state == PartyState.Hosting;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// On double click execute, fire media playing event
|
|
|
|
/// </summary>
|
2020-01-18 23:07:31 +00:00
|
|
|
public void OnDoubleClickCommandExecute()
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
//Fire Playing event
|
2019-11-09 19:55:09 +00:00
|
|
|
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
2019-11-29 17:37:57 +00:00
|
|
|
NewMediaPlayingEvent mediaPlaying = new NewMediaPlayingEvent()
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
|
|
|
Media = new RemoteMediaData()
|
|
|
|
{
|
|
|
|
Id = _selectedMedia.Id,
|
|
|
|
Title = meta.Title,
|
|
|
|
Artist = meta.Artist,
|
|
|
|
Album = meta.Album,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EventManager.Instance.FireEvent(new BaseEvent()
|
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
NewMediaPlayingEvent = mediaPlaying
|
2019-11-09 19:55:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:07:31 +00:00
|
|
|
public bool CanDoubleClickCommandExecute()
|
2019-11-09 19:55:09 +00:00
|
|
|
{
|
|
|
|
return this._state == PartyState.Hosting;
|
|
|
|
}
|
|
|
|
|
2019-06-26 01:17:52 +00:00
|
|
|
|
|
|
|
#endregion Commands
|
|
|
|
|
2019-07-10 22:23:19 +00:00
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
|
|
/// Join the remote party.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
2019-11-13 01:09:45 +00:00
|
|
|
private async Task JoinParty(bool asHost)
|
2019-07-10 22:23:19 +00:00
|
|
|
{
|
2019-11-02 19:47:41 +00:00
|
|
|
try
|
2019-07-06 19:52:28 +00:00
|
|
|
{
|
2019-11-09 19:55:09 +00:00
|
|
|
JoinPartyResponse resp = await _client.RemotePartyClient.JoinPartyAsync(new JoinPartyRequest
|
2019-11-02 19:47:41 +00:00
|
|
|
{
|
2020-02-01 01:41:45 +00:00
|
|
|
UserName = this._settingsService.Username,
|
2019-11-02 19:47:41 +00:00
|
|
|
});
|
2019-07-10 22:23:19 +00:00
|
|
|
|
2020-02-01 01:41:45 +00:00
|
|
|
this._settingsService.ClientId = resp.ClientId;
|
2019-07-12 15:34:06 +00:00
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
RefreshMembers();
|
|
|
|
|
|
|
|
//Subscribe to events
|
2019-11-29 17:37:57 +00:00
|
|
|
await SubscribeToEvents();
|
2019-11-03 21:04:05 +00:00
|
|
|
|
2019-12-10 20:10:27 +00:00
|
|
|
Queue.Clear();
|
2019-11-09 19:55:09 +00:00
|
|
|
QueueResponse queueResponse = _client.RemotePartyClient.GetQueue(new Empty());
|
2019-11-03 21:04:05 +00:00
|
|
|
|
2019-12-10 20:10:27 +00:00
|
|
|
|
2019-11-04 04:17:34 +00:00
|
|
|
//Convert received data to remote audio models
|
2019-11-03 21:04:05 +00:00
|
|
|
foreach (RemoteMediaData data in queueResponse.MediaList)
|
|
|
|
{
|
2019-11-04 04:17:34 +00:00
|
|
|
//Assign received metadata (since this can't be aquired from a file)
|
|
|
|
AudioMetadata meta = new AudioMetadata();
|
|
|
|
meta.Title = data.Title;
|
|
|
|
meta.Album = data.Album;
|
|
|
|
meta.Artist = data.Artist;
|
|
|
|
meta.Duration = data.Duration;
|
|
|
|
|
|
|
|
RemoteAudio remote = new RemoteAudio(data.Id,
|
2019-11-13 01:09:45 +00:00
|
|
|
asHost,
|
2019-11-04 04:17:34 +00:00
|
|
|
meta,
|
2019-11-11 20:10:08 +00:00
|
|
|
_client.RemotePlaybackClient,
|
|
|
|
_client.RemoteSyncClient);
|
2019-11-04 04:17:34 +00:00
|
|
|
|
|
|
|
Queue.Add(remote);
|
2019-12-19 01:23:59 +00:00
|
|
|
OnPropertyChanged("Queue");
|
2019-11-03 21:04:05 +00:00
|
|
|
}
|
2019-07-10 22:23:19 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine("Error subscribing to events: " + ex.Message);
|
2019-07-10 22:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
private async Task LeaveParty()
|
|
|
|
{
|
|
|
|
//Stop receiving events
|
2019-11-09 19:55:09 +00:00
|
|
|
_client.StopEvents();
|
2019-11-07 03:32:43 +00:00
|
|
|
|
|
|
|
//Unsubscribe
|
2019-11-29 17:37:57 +00:00
|
|
|
await UnsubscribeFromEvents();
|
2019-11-07 03:32:43 +00:00
|
|
|
|
|
|
|
//Leave party
|
|
|
|
LeavePartyRequest leaveReq = new LeavePartyRequest();
|
2019-11-09 19:55:09 +00:00
|
|
|
await _client.RemotePartyClient.LeavePartyAsync(leaveReq);
|
2019-11-07 03:32:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 17:37:57 +00:00
|
|
|
private async Task SubscribeToEvents()
|
|
|
|
{
|
|
|
|
SubscribeRequest req = new SubscribeRequest();
|
|
|
|
req.EventTypes.Add(EventType.PartyMemberJoined);
|
|
|
|
req.EventTypes.Add(EventType.PartyMemberLeft);
|
|
|
|
req.EventTypes.Add(EventType.MediaPlaying);
|
|
|
|
req.EventTypes.Add(EventType.MediaStopped);
|
2020-02-01 01:41:45 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(this._settingsService.ClientId))
|
2019-11-29 17:37:57 +00:00
|
|
|
{
|
2020-02-01 01:41:45 +00:00
|
|
|
req.ClientId = this._settingsService.ClientId;
|
2019-11-29 17:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-01 01:41:45 +00:00
|
|
|
Console.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", this._settingsService.ClientId));
|
2019-11-29 17:37:57 +00:00
|
|
|
await _client.RemoteEventClient.SubscribeToEventsAsync(req);
|
|
|
|
}
|
|
|
|
private async Task UnsubscribeFromEvents()
|
|
|
|
{
|
|
|
|
UnsubscribeAllRequest unsubscribeReq = new UnsubscribeAllRequest();
|
|
|
|
await _client.RemoteEventClient.UnsubscribeFromAllAsync(unsubscribeReq);
|
|
|
|
}
|
|
|
|
|
2019-07-10 22:23:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Refresh members list.
|
|
|
|
/// </summary>
|
|
|
|
private void RefreshMembers()
|
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
Members.Clear();
|
2019-11-09 19:55:09 +00:00
|
|
|
MembersResponse response = _client.RemotePartyClient.GetPartyMembers(new Empty());
|
2019-07-10 22:23:19 +00:00
|
|
|
//Add members
|
|
|
|
foreach (PartyMember member in response.Members)
|
|
|
|
{
|
|
|
|
Members.Add(member);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetState(PartyState state)
|
|
|
|
{
|
|
|
|
_state = state;
|
|
|
|
OnPropertyChanged("IsSelectingHost");
|
|
|
|
OnPropertyChanged("IsNotSelectingHost");
|
|
|
|
}
|
|
|
|
|
2019-11-29 17:37:57 +00:00
|
|
|
private BaseMedia GetMediaFromQueue(string Id)
|
2019-07-15 16:14:38 +00:00
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
if (_queue.Any((BaseMedia media) => media.Id == Id))
|
|
|
|
{
|
|
|
|
BaseMedia media = _queue.First((BaseMedia med) => med.Id == Id);
|
|
|
|
return media;
|
|
|
|
}
|
|
|
|
else
|
2019-11-09 01:25:36 +00:00
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
return null;
|
2019-11-09 01:25:36 +00:00
|
|
|
}
|
2019-07-15 16:14:38 +00:00
|
|
|
}
|
2019-12-05 04:42:23 +00:00
|
|
|
private void PlayFromBeginning(BaseMedia args)
|
2019-11-29 17:37:57 +00:00
|
|
|
{
|
2019-12-05 04:42:23 +00:00
|
|
|
base.ChangePlayerState(args, Main.PlayAction.Play);
|
2019-11-29 17:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void PlayResume()
|
|
|
|
{
|
2019-12-05 04:42:23 +00:00
|
|
|
base.ChangePlayerState(null, Main.PlayAction.Resume);
|
2019-11-29 17:37:57 +00:00
|
|
|
}
|
2019-07-15 16:14:38 +00:00
|
|
|
|
2019-11-09 19:55:09 +00:00
|
|
|
private void StopPlaying()
|
2019-11-04 04:17:34 +00:00
|
|
|
{
|
2019-12-05 04:42:23 +00:00
|
|
|
base.ChangePlayerState(null, Main.PlayAction.Pause);
|
2019-11-09 00:54:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 22:23:19 +00:00
|
|
|
#endregion Private Methods
|
2019-05-27 16:23:14 +00:00
|
|
|
}
|
|
|
|
}
|