Moved music playing controls from individual viewmodels to main view
This commit is contained in:
@ -8,25 +8,32 @@ using Xamarin.Forms;
|
||||
using Xamarin.Forms.Xaml;
|
||||
using Aurora.Models.Media;
|
||||
using Aurora.Design.Components.MediaPlayer;
|
||||
using Aurora.Services.PlayerService;
|
||||
|
||||
namespace Aurora.Design.Views.Main
|
||||
{
|
||||
public enum PlayAction
|
||||
{
|
||||
Play,
|
||||
Pause,
|
||||
Resume,
|
||||
Stop
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delegate for updating player metadata
|
||||
/// </summary>
|
||||
/// <param name="media"></param>
|
||||
public delegate void SetPlayerMetadataDelegate(BaseMedia media);
|
||||
|
||||
public delegate void SetPlayerVisibleDelegate(bool visible);
|
||||
|
||||
public delegate void SetIsPlayingDelegate(bool playing);
|
||||
public delegate void SetPlayerDelegate(BaseMedia media, PlayAction action);
|
||||
public delegate bool GetIsPlayingDelegate();
|
||||
|
||||
[XamlCompilation(XamlCompilationOptions.Compile)]
|
||||
public partial class MainView : ContentPage//, IDisposable
|
||||
{
|
||||
private Dictionary<int, BaseViewModel> _viewModels;
|
||||
private BaseViewModel _lastViewModel;
|
||||
private Player _player;
|
||||
private Player _playerComponent;
|
||||
private PlayerService _playerService;
|
||||
private ContentPresenter _viewContent;
|
||||
|
||||
public MainView()
|
||||
@ -35,8 +42,9 @@ namespace Aurora.Design.Views.Main
|
||||
BindingContext = new MainViewModel();
|
||||
_viewModels = new Dictionary<int, BaseViewModel>();
|
||||
|
||||
_player = Player;
|
||||
_playerComponent = Player;
|
||||
_viewContent = (ContentPresenter)ContentPage.Content.FindByName("ViewContent");
|
||||
_playerService = PlayerService.Instance;
|
||||
|
||||
MasterPage.ListView.ItemSelected += OnNavItemSelected;
|
||||
|
||||
@ -139,9 +147,8 @@ namespace Aurora.Design.Views.Main
|
||||
/// <param name="vm"></param>
|
||||
private void UnassignPlayerControls(BaseViewModel vm)
|
||||
{
|
||||
vm.SetPlayerMetadata = null;
|
||||
vm.SetPlayerVisible = null;
|
||||
vm.SetIsPlaying = null;
|
||||
vm.ChangePlayerState = null;
|
||||
vm.IsPlaying = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -150,44 +157,72 @@ namespace Aurora.Design.Views.Main
|
||||
/// <param name="vm">BaseViewModel to assign controls to</param>
|
||||
private void AssignPlayerControls(BaseViewModel vm)
|
||||
{
|
||||
_player.PlayButtonCommand = new Command(vm.OnPlayButtonExecute, vm.CanPlayButtonExecute);
|
||||
_player.NextButtonCommand = new Command(vm.OnNextButtonExecute, vm.CanNextButtonExecute);
|
||||
_player.PreviousButtonCommand = new Command(vm.OnPreviousButtonExecute, vm.CanPreviousButtonExecute);
|
||||
_playerComponent.PlayButtonCommand = new Command(vm.OnPlayButtonExecute, vm.CanPlayButtonExecute);
|
||||
_playerComponent.NextButtonCommand = new Command(vm.OnNextButtonExecute, vm.CanNextButtonExecute);
|
||||
_playerComponent.PreviousButtonCommand = new Command(vm.OnPreviousButtonExecute, vm.CanPreviousButtonExecute);
|
||||
|
||||
//Assign SetPlayer delegate
|
||||
|
||||
vm.SetPlayerMetadata = SetPlayer;
|
||||
vm.SetPlayerVisible = SetPlayerVisible;
|
||||
vm.SetIsPlaying = SetIsPlaying;
|
||||
vm.ChangePlayerState = ChangePlayerState;
|
||||
vm.IsPlaying = () =>
|
||||
{
|
||||
return _playerService.PlaybackState == PlaybackState.Playing;
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SetPlayerDelegate implementation
|
||||
/// Delegate handler for a view model controling music playback
|
||||
/// </summary>
|
||||
/// <param name="media"></param>
|
||||
private void SetPlayer(BaseMedia media)
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
private async void ChangePlayerState(BaseMedia media, PlayAction action)
|
||||
{
|
||||
if (media.Metadata is AudioMetadata)
|
||||
if (media != null && media.Metadata is AudioMetadata)
|
||||
{
|
||||
AudioMetadata meta = (AudioMetadata)media.Metadata;
|
||||
_player.ArtistName = meta.Artist;
|
||||
_player.SongTitle = meta.Title;
|
||||
_playerComponent.ArtistName = meta.Artist;
|
||||
_playerComponent.SongTitle = meta.Title;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SetPlayerVisibleDelegate implementation
|
||||
/// </summary>
|
||||
/// <param name="visible"></param>
|
||||
private void SetPlayerVisible(Boolean visible)
|
||||
{
|
||||
_player.IsVisible = visible;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetIsPlaying(bool playing)
|
||||
{
|
||||
_player.IsPlaying = playing;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user