WIP splitting up the viewModels for the party
This commit is contained in:
@ -31,11 +31,12 @@ namespace Aurora.Design.Views.Main
|
||||
public delegate void ShowModalDelegate(Type view, BaseDialogViewModel viewModel);
|
||||
public delegate void HideModalDelegate();
|
||||
public delegate void FinishDialogDelegate();
|
||||
public delegate void SetViewDelegate(Type viewType, Type viewModelType);
|
||||
|
||||
[XamlCompilation(XamlCompilationOptions.Compile)]
|
||||
public partial class MainView : ContentPage//, IDisposable
|
||||
{
|
||||
private Dictionary<int, BaseViewModel> _viewModels;
|
||||
private Dictionary<string, BaseViewModel> _viewModels;
|
||||
private BaseViewModel _lastViewModel;
|
||||
private Player _playerComponent;
|
||||
private IPlayer _playerService;
|
||||
@ -45,7 +46,7 @@ namespace Aurora.Design.Views.Main
|
||||
{
|
||||
InitializeComponent();
|
||||
BindingContext = new MainViewModel();
|
||||
_viewModels = new Dictionary<int, BaseViewModel>();
|
||||
_viewModels = new Dictionary<string, BaseViewModel>();
|
||||
|
||||
_playerComponent = Player;
|
||||
|
||||
@ -74,29 +75,35 @@ namespace Aurora.Design.Views.Main
|
||||
return;
|
||||
|
||||
var view = (View)Activator.CreateInstance(item.TargetType);
|
||||
string viewModelName = item.TargetViewModelType.Name;
|
||||
|
||||
//Check if we have an instantiated viewModel
|
||||
BaseViewModel vm = new BaseViewModel();
|
||||
if (_viewModels.ContainsKey(item.Id))
|
||||
if (_viewModels.ContainsKey(viewModelName))
|
||||
{
|
||||
_viewModels.TryGetValue(item.Id, out vm);
|
||||
_viewModels.TryGetValue(viewModelName, out vm);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (item.TargetViewModelType.BaseType != typeof(BaseViewModel))
|
||||
if (item.TargetViewModelType.IsAssignableFrom(typeof(BaseViewModel)))
|
||||
{
|
||||
throw new InvalidOperationException("TargetViewModel field must be of type BaseViewModel");
|
||||
}
|
||||
|
||||
//Instantiate new view model
|
||||
vm = (BaseViewModel)App.Container.Resolve(item.TargetViewModelType); //Activator.CreateInstance(item.TargetViewModelType);
|
||||
_viewModels.Add(item.Id, vm);
|
||||
_viewModels.Add(viewModelName, vm);
|
||||
|
||||
}
|
||||
|
||||
//Assign player controls to viewmodel
|
||||
AssignPlayerControls(vm);
|
||||
if(vm is BasePlayerViewModel)
|
||||
{
|
||||
AssignPlayerControls(vm as BasePlayerViewModel);
|
||||
}
|
||||
|
||||
ChangeModalVisiblity(false);
|
||||
vm.SetView = this.SetView;
|
||||
|
||||
//Activate viewmodel
|
||||
vm.OnActive();
|
||||
@ -104,7 +111,10 @@ namespace Aurora.Design.Views.Main
|
||||
//Deactivate last viewModel
|
||||
_lastViewModel.OnInactive();
|
||||
//Unasign deactivating vm
|
||||
UnassignPlayerControls(_lastViewModel);
|
||||
if(_lastViewModel is BasePlayerViewModel)
|
||||
{
|
||||
UnassignPlayerControls(_lastViewModel as BasePlayerViewModel);
|
||||
}
|
||||
|
||||
//Assign viewModel
|
||||
_lastViewModel = vm;
|
||||
@ -128,20 +138,26 @@ namespace Aurora.Design.Views.Main
|
||||
var view = (View)Activator.CreateInstance(firstNavItem.TargetType);
|
||||
|
||||
BaseViewModel vm = new BaseViewModel();
|
||||
if (_viewModels.ContainsKey(firstNavItem.Id))
|
||||
if (_viewModels.ContainsKey(nameof(firstNavItem.TargetViewModelType)))
|
||||
{
|
||||
_viewModels.TryGetValue(firstNavItem.Id, out vm);
|
||||
_viewModels.TryGetValue(nameof(firstNavItem.TargetViewModelType), out vm);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Instantiate new view model
|
||||
vm = (BaseViewModel)App.Container.Resolve(firstNavItem.TargetViewModelType); //(BaseViewModel)Activator.CreateInstance(firstNavItem.TargetViewModelType);
|
||||
_viewModels.Add(firstNavItem.Id, vm);
|
||||
_viewModels.Add(nameof(firstNavItem.TargetViewModelType), vm);
|
||||
}
|
||||
|
||||
view.BindingContext = vm;
|
||||
_lastViewModel = vm;
|
||||
AssignPlayerControls(vm);
|
||||
if(vm is BasePlayerViewModel)
|
||||
{
|
||||
AssignPlayerControls(vm as BasePlayerViewModel);
|
||||
}
|
||||
|
||||
vm.SetView = this.SetView;
|
||||
|
||||
ChangeModalVisiblity(false);
|
||||
vm.OnActive();
|
||||
|
||||
@ -153,7 +169,7 @@ namespace Aurora.Design.Views.Main
|
||||
/// Unassign setplayer delegate to prevent vms from changing player info when inactive
|
||||
/// </summary>
|
||||
/// <param name="vm"></param>
|
||||
private void UnassignPlayerControls(BaseViewModel vm)
|
||||
private void UnassignPlayerControls(BasePlayerViewModel vm)
|
||||
{
|
||||
vm.ChangePlayerState = null;
|
||||
vm.IsPlaying = null;
|
||||
@ -164,7 +180,7 @@ namespace Aurora.Design.Views.Main
|
||||
/// Assign main views music player controls to a view model
|
||||
/// </summary>
|
||||
/// <param name="vm">BaseViewModel to assign controls to</param>
|
||||
private void AssignPlayerControls(BaseViewModel vm)
|
||||
private void AssignPlayerControls(BasePlayerViewModel vm)
|
||||
{
|
||||
_playerComponent.PlayButtonCommand = new Command(vm.OnPlayButtonCommandExecute, vm.CanPlayButtonCommandExecute);
|
||||
_playerComponent.NextButtonCommand = new Command(vm.OnNextButtonExecute, vm.CanNextButtonCommandExecute);
|
||||
@ -259,5 +275,52 @@ namespace Aurora.Design.Views.Main
|
||||
Modal.IsVisible = isVisible;
|
||||
Content.IsVisible = !isVisible;
|
||||
}
|
||||
|
||||
private void SetView(Type viewType, Type viewModelType)
|
||||
{
|
||||
BaseViewModel vm = new BaseViewModel();
|
||||
if(this._viewModels.ContainsKey(viewModelType.Name))
|
||||
{
|
||||
this._viewModels.TryGetValue(viewModelType.Name, out vm);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (viewModelType.IsAssignableFrom(typeof(BaseViewModel)))
|
||||
{
|
||||
throw new InvalidOperationException("TargetViewModel field must be of type BaseViewModel");
|
||||
}
|
||||
|
||||
//Instantiate new view model
|
||||
vm = (BaseViewModel)App.Container.Resolve(viewModelType); //Activator.CreateInstance(item.TargetViewModelType);
|
||||
_viewModels.Add(viewModelType.Name, vm);
|
||||
}
|
||||
|
||||
//Assign player controls to viewmodel
|
||||
if(vm is BasePlayerViewModel)
|
||||
{
|
||||
AssignPlayerControls(vm as BasePlayerViewModel);
|
||||
}
|
||||
|
||||
ChangeModalVisiblity(false);
|
||||
|
||||
//Activate viewmodel
|
||||
vm.OnActive();
|
||||
|
||||
//Deactivate last viewModel
|
||||
_lastViewModel.OnInactive();
|
||||
//Unasign deactivating vm
|
||||
if(_lastViewModel is BasePlayerViewModel)
|
||||
{
|
||||
UnassignPlayerControls(_lastViewModel as BasePlayerViewModel);
|
||||
}
|
||||
|
||||
var view = (View)Activator.CreateInstance(viewType);
|
||||
|
||||
//Assign viewModel
|
||||
_lastViewModel = vm;
|
||||
view.BindingContext = vm;
|
||||
|
||||
_viewContent.Content = view;
|
||||
}
|
||||
}
|
||||
}
|
@ -56,13 +56,13 @@ namespace Aurora.Design.Views.MainView
|
||||
{
|
||||
_pages = new ObservableCollection<NavigationItem>(new[]
|
||||
{
|
||||
new NavigationItem { Id = 0, Title = "Songs", Group="Your Music", TargetType = typeof(SongsView), TargetViewModelType = typeof(SongsViewModel) },
|
||||
new NavigationItem { Id = 1, Title = "Artists", Group="Your Music", TargetType = typeof(ArtistsView), TargetViewModelType = typeof(ArtistsViewModel)},
|
||||
new NavigationItem { Id = 2, Title = "Albums", Group="Your Music", TargetType = typeof(AlbumsView), TargetViewModelType = typeof(AlbumsViewModel)},
|
||||
new NavigationItem { Id = 3, Title = "Stations", Group="Your Music", TargetType = typeof(StationsView), TargetViewModelType = typeof(StationsViewModel)},
|
||||
new NavigationItem { Id = 4, Title = "Party", Group="Social", TargetType = typeof(PartyView), TargetViewModelType = typeof(PartyViewModel)},
|
||||
new NavigationItem { Id = 5, Title = "Profile", Group="Social", TargetType = typeof(ProfileView), TargetViewModelType = typeof(ProfileViewModel)},
|
||||
new NavigationItem { Id = 6, Title = "A + B", Group="Playlists", TargetType = typeof(StationsView), TargetViewModelType = typeof(StationsViewModel)}
|
||||
new NavigationItem { Title = "Songs", Group="Your Music", TargetType = typeof(SongsView), TargetViewModelType = typeof(SongsViewModel) },
|
||||
new NavigationItem { Title = "Artists", Group="Your Music", TargetType = typeof(ArtistsView), TargetViewModelType = typeof(ArtistsViewModel)},
|
||||
new NavigationItem { Title = "Albums", Group="Your Music", TargetType = typeof(AlbumsView), TargetViewModelType = typeof(AlbumsViewModel)},
|
||||
new NavigationItem { Title = "Stations", Group="Your Music", TargetType = typeof(StationsView), TargetViewModelType = typeof(StationsViewModel)},
|
||||
new NavigationItem { Title = "Party", Group="Social", TargetType = typeof(PartyView), TargetViewModelType = typeof(PartyViewModel)},
|
||||
new NavigationItem { Title = "Profile", Group="Social", TargetType = typeof(ProfileView), TargetViewModelType = typeof(ProfileViewModel)},
|
||||
new NavigationItem { Title = "A + B", Group="Playlists", TargetType = typeof(StationsView), TargetViewModelType = typeof(StationsViewModel)}
|
||||
});
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user