2019-05-20 00:21:54 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2019-07-05 18:17:09 +00:00
|
|
|
|
using Aurora.Models.Media;
|
|
|
|
|
using Aurora.Services;
|
|
|
|
|
using Aurora.Services.PlayerService;
|
2019-05-22 14:30:41 +00:00
|
|
|
|
using Xamarin.Forms;
|
2019-05-19 21:25:42 +00:00
|
|
|
|
|
2019-07-05 18:17:09 +00:00
|
|
|
|
namespace Aurora.Design.Views.Songs
|
2019-05-18 21:25:36 +00:00
|
|
|
|
{
|
2019-05-19 21:25:42 +00:00
|
|
|
|
public class SongsViewModel : BaseViewModel
|
2019-05-18 21:25:36 +00:00
|
|
|
|
{
|
2019-05-19 21:25:42 +00:00
|
|
|
|
#region Fields
|
2019-05-24 14:27:19 +00:00
|
|
|
|
private ObservableCollection<BaseMedia> _songsList;
|
|
|
|
|
private BaseMedia _selectedSong;
|
2019-11-07 03:32:43 +00:00
|
|
|
|
private BaseMedia _playingSong;
|
|
|
|
|
private PlayerService _player;
|
2019-05-19 21:25:42 +00:00
|
|
|
|
|
|
|
|
|
#endregion Fields
|
|
|
|
|
|
|
|
|
|
#region Constructor
|
2019-05-18 21:25:36 +00:00
|
|
|
|
public SongsViewModel()
|
|
|
|
|
{
|
2019-11-07 03:32:43 +00:00
|
|
|
|
_player = PlayerService.Instance;
|
2019-05-24 14:27:19 +00:00
|
|
|
|
_songsList = new ObservableCollection<BaseMedia>();
|
2019-11-07 03:32:43 +00:00
|
|
|
|
PlayCommand = new Command(PlayExecute, PlayCanExecute);
|
|
|
|
|
|
|
|
|
|
_player.PlaybackStateChanged += OnPlaybackStateChanged;
|
|
|
|
|
|
2019-05-19 21:25:42 +00:00
|
|
|
|
Initialize();
|
2019-05-18 21:25:36 +00:00
|
|
|
|
}
|
2019-05-19 21:25:42 +00:00
|
|
|
|
|
|
|
|
|
#endregion Constructor
|
|
|
|
|
|
|
|
|
|
#region Properties
|
2019-05-24 14:27:19 +00:00
|
|
|
|
public ObservableCollection<BaseMedia> SongsList
|
2019-05-19 21:25:42 +00:00
|
|
|
|
{
|
|
|
|
|
get { return _songsList; }
|
2019-05-22 14:30:41 +00:00
|
|
|
|
set { SetProperty(ref _songsList, value); }
|
2019-05-19 21:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-24 14:27:19 +00:00
|
|
|
|
public BaseMedia SelectedSong
|
2019-05-19 21:25:42 +00:00
|
|
|
|
{
|
|
|
|
|
get { return _selectedSong; }
|
2019-05-22 14:30:41 +00:00
|
|
|
|
set { SetProperty(ref _selectedSong, value); }
|
2019-05-19 21:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
|
public BaseMedia PlayingSong
|
|
|
|
|
{
|
|
|
|
|
get { return _playingSong; }
|
|
|
|
|
set { SetProperty(ref _playingSong, value); }
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 14:30:41 +00:00
|
|
|
|
public Command PlayCommand { get; private set; }
|
|
|
|
|
|
2019-05-19 21:25:42 +00:00
|
|
|
|
#endregion Properties
|
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
|
|
|
|
|
2019-05-20 00:21:54 +00:00
|
|
|
|
SongsList = LibraryService.Instance.GetLibrary();
|
2019-05-19 21:25:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
|
#endregion Methods
|
|
|
|
|
|
|
|
|
|
#region Commmands
|
|
|
|
|
public override bool PreviousCanExecute()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public override void PreviousExecute()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool PlayCanExecute()
|
2019-05-22 22:59:15 +00:00
|
|
|
|
{
|
2019-11-07 03:32:43 +00:00
|
|
|
|
switch (_player.PlaybackState)
|
|
|
|
|
{
|
|
|
|
|
case PlaybackState.Buffering:
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case PlaybackState.Playing:
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case PlaybackState.Stopped:
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2019-05-22 22:59:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
|
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");
|
|
|
|
|
}
|
2019-05-19 21:25:42 +00:00
|
|
|
|
|
2019-11-07 03:32:43 +00:00
|
|
|
|
#endregion Events
|
2019-05-18 21:25:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|