This repository has been archived on 2020-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
aurora-sharp-desktop/Aurora/Design/Views/Party/PartyViewModel.cs

250 lines
7.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Linq;
2019-06-26 01:17:52 +00:00
using Xamarin.Forms;
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;
using Aurora.Models.Media;
2019-07-05 18:17:09 +00:00
namespace Aurora.Design.Views.Party
{
2019-06-26 01:17:52 +00:00
enum PartyState
{
SelectingHost,
InParty,
Connecting,
}
public class PartyViewModel : BaseViewModel
{
2019-06-26 01:17:52 +00:00
private PartyState _state;
private string _hostname;
2019-07-05 21:37:10 +00:00
private ObservableCollection<PartyMember> _members;
private ObservableCollection<RemoteMediaData> _queue;
public PartyViewModel()
{
2019-06-26 01:17:52 +00:00
this.JoinCommand = new Command(OnJoinExecute, CanJoinExecute);
this.HostCommand = new Command(OnHostExecute, CanHostExecute);
_members = new ObservableCollection<PartyMember>();
_queue = new ObservableCollection<RemoteMediaData>();
2019-06-26 01:17:52 +00:00
SetState(PartyState.SelectingHost);
2019-11-02 19:47:41 +00:00
//Hook up event handler
ClientService.Instance.EventReceived += this.OnEventReceived;
}
~PartyViewModel()
{
2019-11-02 19:47:41 +00:00
//Task.Run(ServerService.Instance.Stop);
}
2019-06-26 01:17:52 +00:00
#region Properties
2019-07-05 21:37:10 +00:00
public ObservableCollection<PartyMember> Members
{
2019-07-05 21:37:10 +00:00
get
{
return _members;
}
set
{
SetProperty(ref _members, value);
}
}
2019-06-26 01:17:52 +00:00
public bool IsSelectingHost
{
get { return _state == PartyState.SelectingHost; }
}
public bool IsNotSelectingHost
{
get { return _state != PartyState.SelectingHost; }
}
public ObservableCollection<RemoteMediaData> Queue
{
get
{
return _queue;
}
set
{
if (value != _queue)
{
SetProperty(ref _queue, value);
}
}
}
2019-06-26 01:17:52 +00:00
public Command JoinCommand { get; set; }
public Command HostCommand { get; set; }
public string Hostname
{
get { return _hostname; }
set { SetProperty(ref _hostname, value); }
}
#endregion Properties
2019-11-02 19:47:41 +00:00
#region Events
/// <summary>
/// An event handler for the client receiving update events
/// </summary>
/// <param name="sender">The object that sent the event</param>
/// <param name="eventArgs">The event arguments</param>
public void OnEventReceived(object sender, EventReceivedEventArgs eventArgs)
{
switch (eventArgs.BaseEvent.DerivedEventCase)
{
case BaseEvent.DerivedEventOneofCase.None:
{
throw new InvalidOperationException();
}
case BaseEvent.DerivedEventOneofCase.PartyMemberJoinedEvent:
{
PartyMemberJoinedEvent derivedEvent = eventArgs.BaseEvent.PartyMemberJoinedEvent;
PartyMember member = new PartyMember
{
UserName = derivedEvent.Member.UserName,
Id = derivedEvent.Member.Id,
IpAddress = derivedEvent.Member.IpAddress,
Port = derivedEvent.Member.Port
};
AddMember(member);
break;
}
case BaseEvent.DerivedEventOneofCase.PartyMemberLeftEvent:
{
PartyMemberJoinedEvent derivedEvent = eventArgs.BaseEvent.PartyMemberJoinedEvent;
var found = Members.Where(x => x.Id == derivedEvent.Member.Id);
foreach (PartyMember member in found)
{
_members.Remove(member);
}
break;
}
}
}
#endregion Events
2019-06-26 01:17:52 +00:00
#region Commands
2019-11-02 19:47:41 +00:00
private async void OnJoinExecute()
2019-06-26 01:17:52 +00:00
{
2019-11-02 19:47:41 +00:00
ClientService.Instance.Start(Hostname, SettingsService.Instance.DefaultPort.ToString());
await JoinParty();
2019-07-06 19:52:28 +00:00
SetState(PartyState.Connecting);
2019-06-26 01:17:52 +00:00
}
private bool CanJoinExecute()
{
return true;
}
2019-11-02 19:47:41 +00:00
private async void OnHostExecute()
2019-06-26 01:17:52 +00:00
{
ServerService.Instance.Start();
string localHost = ServerService.GetLocalIPAddress();
2019-11-02 19:47:41 +00:00
ClientService.Instance.Start(localHost, SettingsService.Instance.DefaultPort.ToString());
await JoinParty();
ClientService.Instance.GetEvents();
2019-07-05 21:37:10 +00:00
//Change state
SetState(PartyState.Connecting);
2019-06-26 01:17:52 +00:00
}
private bool CanHostExecute()
{
return true;
}
#endregion Commands
#region Private Methods
/// <summary>
/// Join the remote party.
/// </summary>
/// <returns></returns>
2019-11-02 19:47:41 +00:00
private async Task JoinParty()
{
2019-11-02 19:47:41 +00:00
try
2019-07-06 19:52:28 +00:00
{
2019-11-02 19:47:41 +00:00
JoinPartyResponse resp = await ClientService.Instance.RemotePartyClient.JoinPartyAsync(new JoinPartyRequest
{
UserName = SettingsService.Instance.Username,
});
2019-11-02 19:47:41 +00:00
SettingsService.Instance.ClientId = resp.ClientId;
2019-11-02 19:47:41 +00:00
RefreshMembers();
//Subscribe to events
SubscribeRequest req = new SubscribeRequest();
req.EventTypes.Add(EventType.PartyMemberJoined);
req.EventTypes.Add(EventType.PartyMemberLeft);
if (!string.IsNullOrWhiteSpace(SettingsService.Instance.ClientId))
{
req.ClientId = SettingsService.Instance.ClientId;
}
Console.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", SettingsService.Instance.ClientId));
2019-11-02 19:47:41 +00:00
ClientService.Instance.RemoteEventClient.SubscribeToEvents(req);
QueueResponse queueResponse = ClientService.Instance.RemotePartyClient.GetQueue(new Empty());
Queue.Clear();
foreach (RemoteMediaData data in queueResponse.MediaList)
{
Queue.Add(data);
}
}
catch (Exception ex)
{
Console.WriteLine("Error subscribing to events: " + ex.Message);
}
}
/// <summary>
/// Refresh members list.
/// </summary>
private void RefreshMembers()
{
2019-11-02 19:47:41 +00:00
MembersResponse response = ClientService.Instance.RemotePartyClient.GetPartyMembers(new Empty());
//Add members
foreach (PartyMember member in response.Members)
{
Members.Add(member);
}
}
private void SetState(PartyState state)
{
_state = state;
OnPropertyChanged("IsSelectingHost");
OnPropertyChanged("IsNotSelectingHost");
}
private void AddMember(PartyMember member)
{
Members.Add(member);
}
#endregion Private Methods
}
}