794b4739b1
This gives view models more freedom in how play events from the player are handled
144 lines
3.6 KiB
C#
144 lines
3.6 KiB
C#
using System.Collections.ObjectModel;
|
|
using Aurora.Models.Media;
|
|
using Aurora.Services;
|
|
using Aurora.Services.PlayerService;
|
|
using Xamarin.Forms;
|
|
|
|
namespace Aurora.Design.Views.Songs
|
|
{
|
|
public class SongsViewModel : BaseViewModel
|
|
{
|
|
#region Fields
|
|
private ObservableCollection<BaseMedia> _songsList;
|
|
private BaseMedia _selectedSong;
|
|
private BaseMedia _playingSong;
|
|
private PlayerService _player;
|
|
|
|
#endregion Fields
|
|
|
|
#region Constructor
|
|
public SongsViewModel()
|
|
{
|
|
_player = PlayerService.Instance;
|
|
_songsList = new ObservableCollection<BaseMedia>();
|
|
PlayCommand = new Command(PlayExecute, PlayCanExecute);
|
|
|
|
_player.PlaybackStateChanged += OnPlaybackStateChanged;
|
|
|
|
Initialize();
|
|
}
|
|
|
|
#endregion Constructor
|
|
|
|
#region Properties
|
|
public ObservableCollection<BaseMedia> SongsList
|
|
{
|
|
get { return _songsList; }
|
|
set { SetProperty(ref _songsList, value); }
|
|
}
|
|
|
|
public BaseMedia SelectedSong
|
|
{
|
|
get { return _selectedSong; }
|
|
set { SetProperty(ref _selectedSong, value); }
|
|
}
|
|
|
|
public BaseMedia PlayingSong
|
|
{
|
|
get { return _playingSong; }
|
|
set { SetProperty(ref _playingSong, value); }
|
|
}
|
|
|
|
public Command PlayCommand { get; private set; }
|
|
|
|
#endregion Properties
|
|
|
|
#region Methods
|
|
public void Initialize()
|
|
{
|
|
|
|
SongsList = LibraryService.Instance.GetLibrary();
|
|
}
|
|
|
|
#endregion Methods
|
|
|
|
#region Commmands
|
|
public override bool PreviousCanExecute()
|
|
{
|
|
return true;
|
|
}
|
|
public override void PreviousExecute()
|
|
{
|
|
|
|
}
|
|
|
|
public override bool PlayCanExecute()
|
|
{
|
|
switch (_player.PlaybackState)
|
|
{
|
|
case PlaybackState.Buffering:
|
|
{
|
|
return true;
|
|
}
|
|
case PlaybackState.Playing:
|
|
{
|
|
return true;
|
|
}
|
|
case PlaybackState.Stopped:
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async override void PlayExecute()
|
|
{
|
|
if (!_player.IsMediaLoaded(_selectedSong))
|
|
{
|
|
await _player.LoadMedia(_selectedSong).ConfigureAwait(true);
|
|
}
|
|
|
|
_player.Play();
|
|
switch (_player.PlaybackState)
|
|
{
|
|
case PlaybackState.Buffering:
|
|
{
|
|
_player.Play();
|
|
break;
|
|
}
|
|
case PlaybackState.Playing:
|
|
{
|
|
_player.Pause();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override bool NextCanExecute()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public override void NextExecute()
|
|
{
|
|
|
|
}
|
|
|
|
#endregion Commands
|
|
|
|
#region Events
|
|
/// <summary>
|
|
/// PlayerService playback state changed event handler.
|
|
/// </summary>
|
|
/// <param name="sender">The sending object.</param>
|
|
/// <param name="args">Event arguments.</param>
|
|
public void OnPlaybackStateChanged(object sender, PlaybackStateChangedEventArgs args)
|
|
{
|
|
OnPropertyChanged("PlayButtonText");
|
|
}
|
|
|
|
#endregion Events
|
|
}
|
|
}
|