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 _songsList; private BaseMedia _selectedSong; private PlayerService _player; #endregion Fields #region Constructor public SongsViewModel() { _player = PlayerService.Instance; _songsList = new ObservableCollection(); PlayCommand = new Command(OnPlayButtonExecute, CanPlayButtonExecute); Initialize(); } #endregion Constructor #region Properties public ObservableCollection SongsList { get { return _songsList; } set { SetProperty(ref _songsList, value); } } public BaseMedia SelectedSong { get { return _selectedSong; } set { SetProperty(ref _selectedSong, 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 CanPreviousButtonExecute() { return true; } public override void OnPreviousButtonExecute() { } public override bool CanPlayButtonExecute() { switch (_player.PlaybackState) { case PlaybackState.Buffering: { return true; } case PlaybackState.Playing: { return true; } case PlaybackState.Stopped: { return true; } } return false; } public async override void OnPlayButtonExecute() { if (_selectedSong == null) { return; } base.Media = _selectedSong; if (!_player.IsMediaLoaded(base.Media)) { await _player.LoadMedia(base.Media).ConfigureAwait(true); } switch (_player.PlaybackState) { case PlaybackState.Buffering: { _player.Play(); SetIsPlaying(true); break; } case PlaybackState.Playing: { _player.Pause(); SetIsPlaying(false); break; } case PlaybackState.Stopped: { _player.Play(); SetIsPlaying(true); break; } } } public override bool CanNextButtonExecute() { return true; } public override void OnNextButtonExecute() { } #endregion Commands } }