109 lines
2.8 KiB
C#
Raw Normal View History

using System;
using Xamarin.Forms;
using Aurora.Frontend.Views;
using Aurora.Backend.Services.PlayerService;
namespace Aurora.Frontend.Components.MediaPlayer
{
public class PlayerViewModel : BaseViewModel
{
PlayerService _playerService;
public PlayerViewModel()
{
_playerService = PlayerService.Instance;
_playerService.PlaybackStateChanged += OnPlaybackStateChanged;
PlayCommand = new Command(OnPlayExecute, CanPlayExecute);
PreviousCommand = new Command(OnPreviousExecute, CanPreviousExecute);
NextCommand = new Command(OnNextExecute, CanNextExecute);
}
~PlayerViewModel()
{
}
#region Public Properties
public Command PlayCommand { get; private set; }
public Command NextCommand { get; private set; }
public Command PreviousCommand { get; private set; }
public string PlayButtonText
{
get { return PlayerService.Instance.PlaybackState == PlaybackState.Buffering ? "Play" : "Pause"; }
}
#endregion Public Properties
#region Public Methods
public bool CanPreviousExecute()
{
return true;
}
public void OnPreviousExecute()
{
}
public bool CanPlayExecute()
{
switch (_playerService.PlaybackState)
{
case PlaybackState.Buffering:
{
return true;
}
case PlaybackState.Playing:
{
return true;
}
case PlaybackState.Stopped:
{
return false;
}
}
return false;
}
public void OnPlayExecute()
{
switch (_playerService.PlaybackState)
{
case PlaybackState.Buffering:
{
_playerService.Play();
break;
}
case PlaybackState.Playing:
{
_playerService.Pause();
break;
}
}
}
public bool CanNextExecute()
{
return true;
}
public void OnNextExecute()
{
}
#endregion public Methods
#region EventHandlers
public void OnPlaybackStateChanged(object sender, PlaybackStateChangedEventArgs args)
{
OnPropertyChanged("PlayButtonText");
PlayCommand.ChangeCanExecute();
NextCommand.ChangeCanExecute();
PreviousCommand.ChangeCanExecute();
}
#endregion EventHandlers
}
}