2019-05-17 22:21:02 +00:00
|
|
|
|
using System;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2019-07-15 19:03:59 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
using System.Linq;
|
2019-07-05 18:17:09 +00:00
|
|
|
|
using Aurora.Design.Components.NavigationMenu;
|
|
|
|
|
using Aurora.Design.Views.MainView;
|
2019-05-17 22:21:02 +00:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using Xamarin.Forms.Xaml;
|
2019-11-07 03:32:43 +00:00
|
|
|
|
using Aurora.Models.Media;
|
|
|
|
|
using Aurora.Design.Components.MediaPlayer;
|
2019-05-17 22:21:02 +00:00
|
|
|
|
|
2019-07-05 18:17:09 +00:00
|
|
|
|
namespace Aurora.Design.Views.Main
|
2019-05-24 14:27:19 +00:00
|
|
|
|
{
|
2019-05-17 22:21:02 +00:00
|
|
|
|
[XamlCompilation(XamlCompilationOptions.Compile)]
|
|
|
|
|
public partial class MainView : MasterDetailPage
|
|
|
|
|
{
|
2019-07-15 19:03:59 +00:00
|
|
|
|
private Dictionary<int, BaseViewModel> _viewModels;
|
|
|
|
|
private BaseViewModel _lastViewModel;
|
|
|
|
|
|
2019-05-17 22:21:02 +00:00
|
|
|
|
public MainView()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2019-05-18 21:25:36 +00:00
|
|
|
|
BindingContext = new MainViewModel();
|
2019-07-15 19:03:59 +00:00
|
|
|
|
_viewModels = new Dictionary<int, BaseViewModel>();
|
2019-11-07 03:32:43 +00:00
|
|
|
|
MasterPage.ListView.ItemSelected += OnNavItemSelected;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
|
2019-05-24 19:59:26 +00:00
|
|
|
|
Appearing += OnAppearing;
|
|
|
|
|
}
|
|
|
|
|
~MainView()
|
|
|
|
|
{
|
|
|
|
|
Appearing -= OnAppearing;
|
2019-05-17 22:21:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
|
public Command PlayCommand { get; set; }
|
|
|
|
|
public Command PreviousCommand { get; set; }
|
|
|
|
|
public Command NextCommand { get; set; }
|
|
|
|
|
public BaseMedia Media { get; set; }
|
|
|
|
|
|
|
|
|
|
private void OnNavItemSelected(object sender, SelectedItemChangedEventArgs e)
|
2019-05-17 22:21:02 +00:00
|
|
|
|
{
|
|
|
|
|
var item = e.SelectedItem as NavigationItem;
|
|
|
|
|
if (item == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-05-24 14:27:19 +00:00
|
|
|
|
var view = (View)Activator.CreateInstance(item.TargetType);
|
2019-05-17 22:21:02 +00:00
|
|
|
|
|
2019-07-15 19:03:59 +00:00
|
|
|
|
//Check if we have an instantiated viewModel
|
|
|
|
|
BaseViewModel vm = new BaseViewModel();
|
|
|
|
|
if (_viewModels.ContainsKey(item.Id))
|
|
|
|
|
{
|
|
|
|
|
_viewModels.TryGetValue(item.Id, out vm);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (item.TargetViewModelType.BaseType != typeof(BaseViewModel))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("TargetViewModel field must be of type BaseViewModel");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Instantiate new view model
|
|
|
|
|
vm = (BaseViewModel)Activator.CreateInstance(item.TargetViewModelType);
|
|
|
|
|
_viewModels.Add(item.Id, vm);
|
|
|
|
|
}
|
2019-11-07 03:32:43 +00:00
|
|
|
|
|
|
|
|
|
//Assign player controls to viewmodel
|
|
|
|
|
Player player = (Player)ContentPage.FindByName("Player");
|
|
|
|
|
player.PlayButtonCommand = new Command(vm.PlayExecute, vm.PlayCanExecute);
|
|
|
|
|
player.NextButtonCommand = new Command(vm.NextExecute, vm.NextCanExecute);
|
|
|
|
|
player.PreviousButtonCommand = new Command(vm.PreviousExecute, vm.PreviousCanExecute);
|
|
|
|
|
vm.Media = this.Media;
|
|
|
|
|
|
2019-07-15 19:03:59 +00:00
|
|
|
|
//Activate viewmodel
|
|
|
|
|
vm.OnActive();
|
|
|
|
|
|
|
|
|
|
//Deactivate last viewModel
|
|
|
|
|
_lastViewModel.OnInactive();
|
|
|
|
|
|
|
|
|
|
//Assign viewModel
|
|
|
|
|
_lastViewModel = vm;
|
|
|
|
|
view.BindingContext = vm;
|
|
|
|
|
|
2019-05-24 14:27:19 +00:00
|
|
|
|
ContentPresenter viewContent = (ContentPresenter)ContentPage.Content.FindByName("ViewContent");
|
|
|
|
|
viewContent.Content = view;
|
2019-05-17 22:21:02 +00:00
|
|
|
|
|
|
|
|
|
MasterPage.ListView.SelectedItem = null;
|
|
|
|
|
}
|
2019-05-24 19:59:26 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event handler for page appearing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The object that fired the event.</param>
|
|
|
|
|
/// <param name="args">The event arguments</param>
|
|
|
|
|
private void OnAppearing(object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
//Set initial view from first item in list
|
|
|
|
|
ObservableCollection<NavigationGroupItem> screenList = (ObservableCollection<NavigationGroupItem>)MasterPage.ListView.ItemsSource;
|
2019-07-15 19:03:59 +00:00
|
|
|
|
|
|
|
|
|
//Assign viewModel
|
|
|
|
|
NavigationItem firstNavItem = screenList.FirstOrDefault().FirstOrDefault();
|
|
|
|
|
var view = (View)Activator.CreateInstance(firstNavItem.TargetType);
|
|
|
|
|
|
|
|
|
|
BaseViewModel vm = new BaseViewModel();
|
|
|
|
|
if (_viewModels.ContainsKey(firstNavItem.Id))
|
|
|
|
|
{
|
|
|
|
|
_viewModels.TryGetValue(firstNavItem.Id, out vm);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Instantiate new view model
|
|
|
|
|
vm = (BaseViewModel)Activator.CreateInstance(firstNavItem.TargetViewModelType);
|
|
|
|
|
_viewModels.Add(firstNavItem.Id, vm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
view.BindingContext = vm;
|
|
|
|
|
_lastViewModel = vm;
|
2019-11-07 03:32:43 +00:00
|
|
|
|
Player player = (Player)ContentPage.FindByName("Player");
|
|
|
|
|
player.PlayButtonCommand = new Command(vm.PlayExecute, vm.PlayCanExecute);
|
|
|
|
|
player.NextButtonCommand = new Command(vm.NextExecute, vm.NextCanExecute);
|
|
|
|
|
player.PreviousButtonCommand = new Command(vm.PreviousExecute, vm.PreviousCanExecute);
|
|
|
|
|
vm.Media = this.Media;
|
2019-07-15 19:03:59 +00:00
|
|
|
|
vm.OnActive();
|
2019-05-24 19:59:26 +00:00
|
|
|
|
|
|
|
|
|
ContentPresenter viewContent = (ContentPresenter)ContentPage.Content.FindByName("ViewContent");
|
|
|
|
|
viewContent.Content = view;
|
|
|
|
|
|
|
|
|
|
MasterPage.ListView.SelectedItem = screenList.FirstOrDefault();
|
|
|
|
|
}
|
2019-11-07 03:32:43 +00:00
|
|
|
|
|
2019-05-17 22:21:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|