Removed old proto definitions (not tested)
This commit is contained in:
@ -24,7 +24,7 @@ namespace Aurora.Design.Components.MemberList
|
||||
/// <returns></returns>
|
||||
public static readonly BindableProperty MembersProperty =
|
||||
BindableProperty.Create(propertyName: "Members",
|
||||
returnType: typeof(ObservableCollection<PartyMember>),
|
||||
returnType: typeof(ObservableCollection<Member>),
|
||||
declaringType: typeof(MemberList),
|
||||
defaultBindingMode: BindingMode.Default,
|
||||
propertyChanged: OnMembersChanged);
|
||||
@ -33,11 +33,11 @@ namespace Aurora.Design.Components.MemberList
|
||||
/// Backing property for MembersProperty
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public ObservableCollection<PartyMember> Members
|
||||
public ObservableCollection<Member> Members
|
||||
{
|
||||
get
|
||||
{
|
||||
return (ObservableCollection<PartyMember>)GetValue(MembersProperty);
|
||||
return (ObservableCollection<Member>)GetValue(MembersProperty);
|
||||
}
|
||||
set
|
||||
{
|
||||
|
@ -1,19 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Linq;
|
||||
using Xamarin.Forms;
|
||||
using Aurora.Services;
|
||||
using Aurora.Proto.General;
|
||||
using Aurora.Proto.Party;
|
||||
using Aurora.Proto.Events;
|
||||
using Aurora.Services.ClientService;
|
||||
using Aurora.Services.ClientService.Events;
|
||||
using Aurora.Services.Player;
|
||||
using Aurora.Services.EventManager;
|
||||
using Aurora.Models.Media;
|
||||
using Aurora.Services.Client;
|
||||
using Aurora.Design.Views.Party.NewPartyDialog;
|
||||
using Aurora.Services.Settings;
|
||||
using Aurora.Services.Server;
|
||||
using Aurora.Services.EventManager;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace Aurora.Design.Views.Party
|
||||
{
|
||||
@ -25,25 +24,37 @@ namespace Aurora.Design.Views.Party
|
||||
Hosting,
|
||||
Connecting,
|
||||
}
|
||||
|
||||
public delegate void EventHandler(BaseEvent e);
|
||||
public class PartyViewModel : BaseViewModel
|
||||
{
|
||||
private PartyState _state;
|
||||
private string _hostname = "";
|
||||
private ObservableCollection<PartyMember> _members;
|
||||
private ObservableCollection<Member> _members;
|
||||
private ObservableCollection<BaseMedia> _queue;
|
||||
private BaseMedia _selectedMedia;
|
||||
private IClientService _client;
|
||||
// private IClientService _client;
|
||||
private ISettingsService _settingsService;
|
||||
private IClientService _clientService;
|
||||
private IServerService _serverService;
|
||||
private IEventManager _eventManager;
|
||||
|
||||
private CancellationTokenSource _eventCancellationTokenSource;
|
||||
|
||||
private Dictionary<BaseEvent.DerivedEventOneofCase, EventHandler> _eventHandlers;
|
||||
|
||||
private int _selectedTabIndex;
|
||||
|
||||
public PartyViewModel(ISettingsService settingsService, IClientService clientService)
|
||||
public PartyViewModel(
|
||||
ISettingsService settingsService,
|
||||
IServerService serverService,
|
||||
IEventManager eventManager)
|
||||
{
|
||||
_members = new ObservableCollection<PartyMember>();
|
||||
_members = new ObservableCollection<Member>();
|
||||
_queue = new ObservableCollection<BaseMedia>();
|
||||
|
||||
this._settingsService = settingsService;
|
||||
this._serverService = serverService;
|
||||
this._eventManager = eventManager;
|
||||
|
||||
SetState(PartyState.SelectingHost);
|
||||
|
||||
@ -51,19 +62,15 @@ namespace Aurora.Design.Views.Party
|
||||
|
||||
LeavePartyCommand = new Command(OnLeavePartyCommandExecute, CanLeavePartyCommandExecute);
|
||||
|
||||
_client = clientService;
|
||||
|
||||
_client.OnMediaPaused += this.OnRemoteMediaPaused;
|
||||
_client.OnMediaResumed += this.OnRemoteMediaResumed;
|
||||
_client.OnNewMediaPlaying += this.OnNewRemoteMediaPlaying;
|
||||
_client.OnPartyMemberJoined += this.OnPartyMemberJoined;
|
||||
_client.OnPartyMemberLeft += this.OnPartyMemberLeft;
|
||||
|
||||
}
|
||||
|
||||
~PartyViewModel()
|
||||
{
|
||||
//Task.Run(ServerService.Instance.Stop);
|
||||
//Setup event handlers
|
||||
_eventHandlers = new Dictionary<BaseEvent.DerivedEventOneofCase, EventHandler>()
|
||||
{
|
||||
{BaseEvent.DerivedEventOneofCase.MediaPausedEvent, this.OnRemoteMediaPaused},
|
||||
{BaseEvent.DerivedEventOneofCase.MediaResumedEvent, this.OnRemoteMediaResumed},
|
||||
{BaseEvent.DerivedEventOneofCase.NewMediaPlayingEvent, this.OnNewRemoteMediaPlaying},
|
||||
{BaseEvent.DerivedEventOneofCase.MemberCreatedEvent, this.OnPartyMemberJoined},
|
||||
{BaseEvent.DerivedEventOneofCase.MemberDeletedEvent, this.OnPartyMemberLeft}
|
||||
};
|
||||
}
|
||||
|
||||
#region Properties
|
||||
@ -78,7 +85,7 @@ namespace Aurora.Design.Views.Party
|
||||
/// Publc property for the members list
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public ObservableCollection<PartyMember> Members
|
||||
public ObservableCollection<Member> Members
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -140,7 +147,7 @@ namespace Aurora.Design.Views.Party
|
||||
if (this._state == PartyState.Hosting ||
|
||||
this._state == PartyState.InParty)
|
||||
{
|
||||
await _client.GetEvents().ConfigureAwait(false);
|
||||
await this.GetEvents().ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -177,7 +184,7 @@ namespace Aurora.Design.Views.Party
|
||||
/// <returns></returns>
|
||||
public override Task OnInactive()
|
||||
{
|
||||
_client.StopEvents();
|
||||
this._eventCancellationTokenSource.Cancel();
|
||||
return Task.FromResult<object>(null);
|
||||
}
|
||||
|
||||
@ -186,7 +193,7 @@ namespace Aurora.Design.Views.Party
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
public void OnRemoteMediaPaused(object sender, MediaPausedEventArgs args)
|
||||
public void OnRemoteMediaPaused(BaseEvent e)
|
||||
{
|
||||
StopPlaying();
|
||||
}
|
||||
@ -196,9 +203,9 @@ namespace Aurora.Design.Views.Party
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
public void OnNewRemoteMediaPlaying(object sender, NewMediaPlayingEventArgs args)
|
||||
public void OnNewRemoteMediaPlaying(BaseEvent e)
|
||||
{
|
||||
PlayFromBeginning(GetMediaFromQueue(args.Event.Media.Id));
|
||||
PlayFromBeginning(GetMediaFromQueue(e.NewMediaPlayingEvent.Media.Name));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -206,7 +213,7 @@ namespace Aurora.Design.Views.Party
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
public void OnRemoteMediaResumed(object sender, MediaResumedEventArgs args)
|
||||
public void OnRemoteMediaResumed(BaseEvent e)
|
||||
{
|
||||
PlayResume();
|
||||
}
|
||||
@ -216,17 +223,9 @@ namespace Aurora.Design.Views.Party
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
public void OnPartyMemberJoined(object sender, PartyMemberJoinedEventArgs args)
|
||||
public void OnPartyMemberJoined(BaseEvent e)
|
||||
{
|
||||
PartyMember member = new PartyMember
|
||||
{
|
||||
UserName = args.Event.Member.UserName,
|
||||
Id = args.Event.Member.Id,
|
||||
IpAddress = args.Event.Member.IpAddress,
|
||||
Port = args.Event.Member.Port
|
||||
};
|
||||
|
||||
Members.Add(member);
|
||||
Members.Add(e.MemberCreatedEvent.Member);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -234,10 +233,10 @@ namespace Aurora.Design.Views.Party
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
public void OnPartyMemberLeft(object sender, PartyMemberLeftEventArgs args)
|
||||
public void OnPartyMemberLeft(BaseEvent e)
|
||||
{
|
||||
var found = Members.Where(x => x.Id == args.Event.Member.Id);
|
||||
foreach (PartyMember member in found)
|
||||
var found = Members.Where(x => x.Name == e.MemberDeletedEvent.MemberName);
|
||||
foreach (Member member in found)
|
||||
{
|
||||
_members.Remove(member);
|
||||
}
|
||||
@ -249,14 +248,14 @@ namespace Aurora.Design.Views.Party
|
||||
private async void OnJoinCommandExecute()
|
||||
{
|
||||
SetState(PartyState.Connecting);
|
||||
_client.Start(_hostname, this._settingsService.DefaultPort.ToString());
|
||||
_clientService.Start(_hostname, this._settingsService.DefaultPort.ToString());
|
||||
await JoinParty(false);
|
||||
|
||||
//TODO add cancellation token
|
||||
try
|
||||
{
|
||||
SetState(PartyState.InParty);
|
||||
await _client.GetEvents().ConfigureAwait(true);
|
||||
await GetEvents().ConfigureAwait(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -273,10 +272,9 @@ namespace Aurora.Design.Views.Party
|
||||
{
|
||||
//Change state
|
||||
SetState(PartyState.Connecting);
|
||||
ServerService.Instance.Start();
|
||||
_serverService.Start("test", "asdf");
|
||||
string localHost = ServerService.GetLocalIPAddress();
|
||||
_client.IsHost = true;
|
||||
_client.Start(localHost, this._settingsService.DefaultPort.ToString());
|
||||
_clientService.Start(localHost, this._settingsService.DefaultPort.ToString());
|
||||
await JoinParty(true);
|
||||
|
||||
|
||||
@ -284,7 +282,7 @@ namespace Aurora.Design.Views.Party
|
||||
try
|
||||
{
|
||||
SetState(PartyState.Hosting);
|
||||
await _client.GetEvents().ConfigureAwait(true);
|
||||
// await _clientService.RemotePartyServiceClient.GetEvents().ConfigureAwait(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -299,7 +297,10 @@ namespace Aurora.Design.Views.Party
|
||||
|
||||
private async void OnLeavePartyCommandExecute()
|
||||
{
|
||||
await _client.RemotePartyClient.LeavePartyAsync(new LeavePartyRequest());
|
||||
await _clientService.RemotePartyServiceClient.DeleteMemberAsync(new DeleteMemberRequest()
|
||||
{
|
||||
Name = _settingsService.ClientName
|
||||
});
|
||||
}
|
||||
|
||||
private bool CanLeavePartyCommandExecute()
|
||||
@ -315,7 +316,7 @@ namespace Aurora.Design.Views.Party
|
||||
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
||||
MediaPausedEvent mediaPaused = new MediaPausedEvent();
|
||||
|
||||
EventManager.Instance.FireEvent(new BaseEvent()
|
||||
_eventManager.FireEvent(new BaseEvent()
|
||||
{
|
||||
MediaPausedEvent = mediaPaused
|
||||
});
|
||||
@ -326,7 +327,7 @@ namespace Aurora.Design.Views.Party
|
||||
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
||||
MediaResumedEvent mediaResumed = new MediaResumedEvent();
|
||||
|
||||
EventManager.Instance.FireEvent(new BaseEvent()
|
||||
_eventManager.FireEvent(new BaseEvent()
|
||||
{
|
||||
MediaResumedEvent = mediaResumed
|
||||
});
|
||||
@ -357,16 +358,17 @@ namespace Aurora.Design.Views.Party
|
||||
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
||||
NewMediaPlayingEvent mediaPlaying = new NewMediaPlayingEvent()
|
||||
{
|
||||
Media = new RemoteMediaData()
|
||||
Media = new Media()
|
||||
{
|
||||
Id = _selectedMedia.Id,
|
||||
//TODO need full resource name
|
||||
Name = _selectedMedia.Id,
|
||||
Title = meta.Title,
|
||||
Artist = meta.Artist,
|
||||
Album = meta.Album,
|
||||
}
|
||||
};
|
||||
|
||||
EventManager.Instance.FireEvent(new BaseEvent()
|
||||
_eventManager.FireEvent(new BaseEvent()
|
||||
{
|
||||
NewMediaPlayingEvent = mediaPlaying
|
||||
});
|
||||
@ -389,24 +391,31 @@ namespace Aurora.Design.Views.Party
|
||||
{
|
||||
try
|
||||
{
|
||||
JoinPartyResponse resp = await _client.RemotePartyClient.JoinPartyAsync(new JoinPartyRequest
|
||||
Member resp = await _clientService.RemotePartyServiceClient.CreateMemberAsync(new CreateMemberRequest
|
||||
{
|
||||
UserName = this._settingsService.Username,
|
||||
Member = new Member()
|
||||
{
|
||||
UserName = this._settingsService.Username,
|
||||
}
|
||||
});
|
||||
|
||||
this._settingsService.ClientId = resp.ClientId;
|
||||
this._settingsService.ClientName = resp.Name;
|
||||
|
||||
RefreshMembers();
|
||||
await RefreshMembers();
|
||||
|
||||
//Subscribe to events
|
||||
await SubscribeToEvents();
|
||||
|
||||
Queue.Clear();
|
||||
QueueResponse queueResponse = _client.RemotePartyClient.GetQueue(new Empty());
|
||||
|
||||
ListMediaResponse mediaResponse = await _clientService.RemotePartyServiceClient.ListMediaAsync(new ListMediaRequest()
|
||||
{
|
||||
PageSize = 50,
|
||||
Parent = "TODO"
|
||||
});
|
||||
|
||||
//Convert received data to remote audio models
|
||||
foreach (RemoteMediaData data in queueResponse.MediaList)
|
||||
foreach (Media data in mediaResponse.Media)
|
||||
{
|
||||
//Assign received metadata (since this can't be aquired from a file)
|
||||
AudioMetadata meta = new AudioMetadata();
|
||||
@ -415,11 +424,10 @@ namespace Aurora.Design.Views.Party
|
||||
meta.Artist = data.Artist;
|
||||
meta.Duration = data.Duration;
|
||||
|
||||
RemoteAudio remote = new RemoteAudio(data.Id,
|
||||
RemoteAudio remote = new RemoteAudio(data.Name,
|
||||
asHost,
|
||||
meta,
|
||||
_client.RemotePlaybackClient,
|
||||
_client.RemoteSyncClient);
|
||||
_clientService.RemotePartyServiceClient);
|
||||
|
||||
Queue.Add(remote);
|
||||
OnPropertyChanged("Queue");
|
||||
@ -434,47 +442,52 @@ namespace Aurora.Design.Views.Party
|
||||
private async Task LeaveParty()
|
||||
{
|
||||
//Stop receiving events
|
||||
_client.StopEvents();
|
||||
// _client.StopEvents();
|
||||
|
||||
//Unsubscribe
|
||||
await UnsubscribeFromEvents();
|
||||
|
||||
//Leave party
|
||||
LeavePartyRequest leaveReq = new LeavePartyRequest();
|
||||
await _client.RemotePartyClient.LeavePartyAsync(leaveReq);
|
||||
DeleteMemberRequest req = new DeleteMemberRequest()
|
||||
{
|
||||
Name = _settingsService.ClientName
|
||||
};
|
||||
|
||||
await _clientService.RemotePartyServiceClient.DeleteMemberAsync(req);
|
||||
}
|
||||
|
||||
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);
|
||||
if (!string.IsNullOrWhiteSpace(this._settingsService.ClientId))
|
||||
{
|
||||
req.ClientId = this._settingsService.ClientId;
|
||||
}
|
||||
CreateEventSubscriptionListRequest req = new CreateEventSubscriptionListRequest();
|
||||
req.Parent = this._settingsService.ClientName;
|
||||
req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MemberCreated });
|
||||
req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MemberDeleted });
|
||||
req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MediaPlaying });
|
||||
req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MediaStopped });
|
||||
|
||||
|
||||
Console.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", this._settingsService.ClientId));
|
||||
await _client.RemoteEventClient.SubscribeToEventsAsync(req);
|
||||
Console.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", this._settingsService.ClientName));
|
||||
await _clientService.RemotePartyServiceClient.CreateEventSubscriptionListAsync(req);
|
||||
}
|
||||
private async Task UnsubscribeFromEvents()
|
||||
{
|
||||
UnsubscribeAllRequest unsubscribeReq = new UnsubscribeAllRequest();
|
||||
await _client.RemoteEventClient.UnsubscribeFromAllAsync(unsubscribeReq);
|
||||
DeleteAllEventSubscriptionsRequest unsubscribeReq = new DeleteAllEventSubscriptionsRequest();
|
||||
await _clientService.RemotePartyServiceClient.DeleteAllEventSubscriptionsAsync(unsubscribeReq);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refresh members list.
|
||||
/// </summary>
|
||||
private void RefreshMembers()
|
||||
private async Task RefreshMembers()
|
||||
{
|
||||
Members.Clear();
|
||||
MembersResponse response = _client.RemotePartyClient.GetPartyMembers(new Empty());
|
||||
ListMembersResponse response = await _clientService.RemotePartyServiceClient.ListMembersAsync(
|
||||
new ListMembersRequest()
|
||||
{
|
||||
Parent = "TODO",
|
||||
PageSize = 50,
|
||||
});
|
||||
//Add members
|
||||
foreach (PartyMember member in response.Members)
|
||||
foreach (Member member in response.Members)
|
||||
{
|
||||
Members.Add(member);
|
||||
}
|
||||
@ -514,6 +527,47 @@ namespace Aurora.Design.Views.Party
|
||||
base.ChangePlayerState(null, Main.PlayAction.Pause);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronous function for processing events off of the event stream.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task GetEvents()
|
||||
{
|
||||
_eventCancellationTokenSource = new CancellationTokenSource();
|
||||
string clientName = this._settingsService.ClientName;
|
||||
Console.WriteLine(string.Format("CLIENT {0} - GetEvents called from client with id", clientName));
|
||||
using (AsyncServerStreamingCall<BaseEvent> eventStream = _clientService.RemotePartyServiceClient
|
||||
.GetEvents(new GetEventsRequest { Parent = this._settingsService.ClientName }))
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!_eventCancellationTokenSource.Token.IsCancellationRequested &&
|
||||
await eventStream.ResponseStream.MoveNext(_eventCancellationTokenSource.Token))
|
||||
{
|
||||
try
|
||||
{
|
||||
BaseEvent e = new BaseEvent(eventStream.ResponseStream.Current);
|
||||
|
||||
_eventHandlers.TryGetValue(e.DerivedEventCase, out EventHandler handler);
|
||||
|
||||
if (handler != null && handler != null)
|
||||
{
|
||||
handler.Invoke(e);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Exception while parsing event ---" + ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(string.Format("EXCEPTION while parsing events --- " + ex.Message));
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion Private Methods
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user