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
2019-07-06 15:52:28 -04:00

126 lines
2.9 KiB
C#

using System;
using System.Collections.ObjectModel;
using Aurora.Executors;
using Aurora.Proto;
using Xamarin.Forms;
namespace Aurora.Design.Views.Party
{
enum PartyState
{
SelectingHost,
InParty,
Connecting,
}
public class PartyViewModel : BaseViewModel
{
private PartyState _state;
private BaseExecutor _executor;
private string _hostname;
private ObservableCollection<PartyMember> _members;
public PartyViewModel()
{
this.JoinCommand = new Command(OnJoinExecute, CanJoinExecute);
this.HostCommand = new Command(OnHostExecute, CanHostExecute);
_members = new ObservableCollection<PartyMember>();
State(PartyState.SelectingHost);
}
#region Properties
public ObservableCollection<PartyMember> Members
{
get
{
return _members;
}
set { SetProperty(ref _members, value); }
}
public bool IsSelectingHost
{
get { return _state == PartyState.SelectingHost; }
}
public bool IsNotSelectingHost
{
get { return _state != PartyState.SelectingHost; }
}
public Command JoinCommand { get; set; }
public Command HostCommand { get; set; }
public string Hostname
{
get { return _hostname; }
set { SetProperty(ref _hostname, value); }
}
#endregion Properties
private void State(PartyState state)
{
_state = state;
OnPropertyChanged("IsSelectingHost");
OnPropertyChanged("IsNotSelectingHost");
}
#region Commands
private void OnJoinExecute()
{
_executor = BaseExecutor.CreateExecutor<ClientExecutor>();
_executor.Connect(this.Hostname);
SetUpMembers();
State(PartyState.Connecting);
}
private bool CanJoinExecute()
{
return true;
}
private void OnHostExecute()
{
//Instantiate and initialize all executors
_executor = BaseExecutor.CreateExecutor<HostExecutor>();
_executor.Connect(this.Hostname);
SetUpMembers();
//Change state
State(PartyState.Connecting);
}
private bool CanHostExecute()
{
return true;
}
#endregion Commands
private void SetUpMembers()
{
_members = _executor.PartyMembers;
OnPropertyChanged("Members");
_executor.PartyMembers.CollectionChanged += (sender, e) =>
{
if (_executor != null)
{
_members = _executor.PartyMembers;
}
OnPropertyChanged("Members");
};
}
}
}