aurora/Aurora/Design/Views/MainView/MainView.xaml.cs

263 lines
8.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
2019-12-07 18:47:45 +00:00
using System.Threading.Tasks;
using System.Linq;
2019-07-05 18:17:09 +00:00
using Aurora.Design.Components.NavigationMenu;
using Aurora.Design.Views.MainView;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Aurora.Models.Media;
using Aurora.Design.Components.MediaPlayer;
2020-02-01 01:41:45 +00:00
using Aurora.Services.Player;
using Autofac;
2019-07-05 18:17:09 +00:00
namespace Aurora.Design.Views.Main
{
public enum PlayAction
{
Play,
Pause,
Resume,
Stop
}
/// <summary>
/// Delegate for updating player metadata
/// </summary>
/// <param name="media"></param>
2019-12-07 18:47:45 +00:00
public delegate Task SetPlayerDelegate(BaseMedia media, PlayAction action);
public delegate bool GetIsPlayingDelegate();
2019-12-19 03:32:08 +00:00
public delegate void ShowModalDelegate(Type view, BaseDialogViewModel viewModel);
public delegate void HideModalDelegate();
2019-12-07 18:47:45 +00:00
public delegate void FinishDialogDelegate();
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainView : ContentPage//, IDisposable
{
private Dictionary<int, BaseViewModel> _viewModels;
private BaseViewModel _lastViewModel;
private Player _playerComponent;
2020-02-01 01:41:45 +00:00
private IPlayer _playerService;
private ContentPresenter _viewContent;
2020-02-01 01:41:45 +00:00
public MainView(IPlayer player)
{
InitializeComponent();
BindingContext = new MainViewModel();
_viewModels = new Dictionary<int, BaseViewModel>();
_playerComponent = Player;
2019-12-07 18:47:45 +00:00
2019-12-10 20:10:27 +00:00
_viewContent = (ContentPresenter)Content.FindByName("ViewContent");
2020-02-01 01:41:45 +00:00
_playerService = player;
MasterPage.ListView.ItemSelected += OnNavItemSelected;
Appearing += OnAppearing;
}
public void Dispose()
{
Appearing -= OnAppearing;
}
/// <summary>
/// Event handler for side bar items being selected
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnNavItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as NavigationItem;
if (item == null)
return;
var view = (View)Activator.CreateInstance(item.TargetType);
//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
2020-02-01 01:41:45 +00:00
vm = (BaseViewModel)App.Container.Resolve(item.TargetViewModelType); //Activator.CreateInstance(item.TargetViewModelType);
_viewModels.Add(item.Id, vm);
}
2019-12-07 18:47:45 +00:00
//Assign player controls to viewmodel
AssignPlayerControls(vm);
ChangeModalVisiblity(false);
//Activate viewmodel
vm.OnActive();
//Deactivate last viewModel
_lastViewModel.OnInactive();
//Unasign deactivating vm
UnassignPlayerControls(_lastViewModel);
//Assign viewModel
_lastViewModel = vm;
view.BindingContext = vm;
_viewContent.Content = view;
}
/// <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;
//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
2020-02-01 01:41:45 +00:00
vm = (BaseViewModel)App.Container.Resolve(firstNavItem.TargetViewModelType); //(BaseViewModel)Activator.CreateInstance(firstNavItem.TargetViewModelType);
_viewModels.Add(firstNavItem.Id, vm);
}
view.BindingContext = vm;
_lastViewModel = vm;
AssignPlayerControls(vm);
2019-12-07 18:47:45 +00:00
ChangeModalVisiblity(false);
vm.OnActive();
_viewContent.Content = view;
}
/// <summary>
/// Unassign setplayer delegate to prevent vms from changing player info when inactive
/// </summary>
/// <param name="vm"></param>
private void UnassignPlayerControls(BaseViewModel vm)
{
vm.ChangePlayerState = null;
vm.IsPlaying = null;
2019-12-07 18:47:45 +00:00
vm.ShowModal = null;
}
/// <summary>
/// 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)
{
2020-01-18 23:07:31 +00:00
_playerComponent.PlayButtonCommand = new Command(vm.OnPlayButtonCommandExecute, vm.CanPlayButtonCommandExecute);
_playerComponent.NextButtonCommand = new Command(vm.OnNextButtonExecute, vm.CanNextButtonCommandExecute);
_playerComponent.PreviousButtonCommand = new Command(vm.OnPreviousButtonExecute, vm.CanPreviousButtonCommandExecute);
vm.ChangePlayerState = ChangePlayerState;
vm.IsPlaying = () =>
{
return _playerService.PlaybackState == PlaybackState.Playing;
};
2019-12-19 03:32:08 +00:00
vm.ShowModal = this.ShowModal;
vm.HideModal = this.HideModal;
2019-12-07 18:47:45 +00:00
}
/// <summary>
/// Delegate handler for a view model controling music playback
/// </summary>
/// <param name="media"></param>
/// <param name="action"></param>
/// <returns></returns>
2019-12-07 18:47:45 +00:00
private async Task ChangePlayerState(BaseMedia media, PlayAction action)
{
if (media != null && media.Metadata is AudioMetadata)
{
AudioMetadata meta = (AudioMetadata)media.Metadata;
_playerComponent.ArtistName = meta.Artist;
_playerComponent.SongTitle = meta.Title;
}
switch (action)
{
case PlayAction.Pause:
{
_playerService.Pause();
_playerComponent.IsPlaying = false;
break;
}
case PlayAction.Play:
{
if (media == null)
{
break;
}
if (!_playerService.IsMediaLoaded(media))
{
await _playerService.LoadMedia(media).ConfigureAwait(true);
}
_playerService.Play();
_playerComponent.IsPlaying = true;
break;
}
case PlayAction.Resume:
{
_playerService.Play();
_playerComponent.IsPlaying = true;
break;
}
case PlayAction.Stop:
{
_playerService.Stop();
_playerComponent.IsPlaying = false;
break;
}
}
}
2019-12-07 18:47:45 +00:00
2019-12-19 03:32:08 +00:00
private void ShowModal(Type view, BaseDialogViewModel viewModel)
2019-12-07 18:47:45 +00:00
{
2019-12-19 03:32:08 +00:00
ContentPresenter modalContainer = (ContentPresenter)Modal.FindByName("ViewContent");
var vw = (View)Activator.CreateInstance(view);
2019-12-07 18:47:45 +00:00
2019-12-19 03:32:08 +00:00
vw.BindingContext = viewModel;
2019-12-07 18:47:45 +00:00
2019-12-19 03:32:08 +00:00
//Set modal container content
modalContainer.Content = vw;
2019-12-07 18:47:45 +00:00
2019-12-19 03:32:08 +00:00
//Set visibility
ChangeModalVisiblity(true);
}
2019-12-07 18:47:45 +00:00
2019-12-19 03:32:08 +00:00
private void HideModal()
{
ChangeModalVisiblity(false);
2019-12-07 18:47:45 +00:00
}
private void ChangeModalVisiblity(bool isVisible)
{
Modal.IsVisible = isVisible;
2019-12-19 03:32:08 +00:00
Content.IsVisible = !isVisible;
2019-12-07 18:47:45 +00:00
}
}
}