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

135 lines
3.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.ObjectModel;
2019-07-05 18:17:09 +00:00
using Aurora.Executors;
using Aurora.Proto.Party;
2019-06-26 01:17:52 +00:00
using Xamarin.Forms;
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 BaseExecutor _executor;
2019-06-26 01:17:52 +00:00
private string _hostname;
2019-07-05 21:37:10 +00:00
private ObservableCollection<PartyMember> _members;
2019-06-26 01:17:52 +00:00
public PartyViewModel()
{
2019-06-26 01:17:52 +00:00
this.JoinCommand = new Command(OnJoinExecute, CanJoinExecute);
this.HostCommand = new Command(OnHostExecute, CanHostExecute);
2019-07-05 21:37:10 +00:00
_members = new ObservableCollection<PartyMember>();
2019-06-26 01:17:52 +00:00
State(PartyState.SelectingHost);
}
~PartyViewModel()
{
if (_executor != null)
{
_executor.Close();
}
}
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 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");
2019-07-05 21:37:10 +00:00
OnPropertyChanged("IsNotSelectingHost");
2019-06-26 01:17:52 +00:00
}
#region Commands
private void OnJoinExecute()
{
_executor = BaseExecutor.CreateExecutor<ClientExecutor>();
2019-07-06 19:52:28 +00:00
_executor.Connect(this.Hostname);
SetUpMembers();
2019-06-26 01:17:52 +00:00
State(PartyState.Connecting);
}
private bool CanJoinExecute()
{
return true;
}
private void OnHostExecute()
{
//Instantiate and initialize all executors
_executor = BaseExecutor.CreateExecutor<HostExecutor>();
2019-07-06 19:52:28 +00:00
_executor.Connect(this.Hostname);
2019-07-06 19:52:28 +00:00
SetUpMembers();
2019-07-05 21:37:10 +00:00
//Change state
2019-06-26 01:17:52 +00:00
State(PartyState.Connecting);
}
private bool CanHostExecute()
{
return true;
}
#endregion Commands
2019-07-06 19:52:28 +00:00
private void SetUpMembers()
{
_members = _executor.PartyMembers;
OnPropertyChanged("Members");
_executor.PartyMembers.CollectionChanged += (sender, e) =>
{
if (_executor != null)
{
_members = _executor.PartyMembers;
}
OnPropertyChanged("Members");
};
}
}
}