Moved music playing controls from individual viewmodels to main view
This commit is contained in:
parent
187de97503
commit
3576a906e2
@ -12,20 +12,20 @@
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition
|
||||
Width="150"/>
|
||||
Width="200"/>
|
||||
<ColumnDefinition
|
||||
Width="*"/>
|
||||
<ColumnDefinition
|
||||
Width="100"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<StackLayout
|
||||
Grid.Column="0"
|
||||
Orientation="Horizontal">
|
||||
<BoxView
|
||||
x:Name="AlbumArtBoxView"/>
|
||||
<StackLayout
|
||||
x:Name="MediaInfoContainer"
|
||||
HorizontalOptions="StartAndExpand"
|
||||
Grid.Column="0">
|
||||
HorizontalOptions="StartAndExpand">
|
||||
<Label
|
||||
x:Name="SongTitleLabel"
|
||||
LineBreakMode="TailTruncation"/>
|
||||
|
@ -11,7 +11,6 @@ namespace Aurora.Design.Views
|
||||
|
||||
public class BaseViewModel : INotifyPropertyChanged
|
||||
{
|
||||
private BaseMedia _baseMedia;
|
||||
public BaseViewModel()
|
||||
{
|
||||
}
|
||||
@ -46,39 +45,33 @@ namespace Aurora.Design.Views
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Model for the currently playing music.
|
||||
/// Delegate for interacting with main screen player control
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public BaseMedia Media
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._baseMedia;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value != _baseMedia)
|
||||
{
|
||||
_baseMedia = value;
|
||||
if (this.SetPlayerMetadata != null)
|
||||
{
|
||||
SetPlayerMetadata.Invoke(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public SetPlayerDelegate ChangePlayerState { get; set; }
|
||||
|
||||
public SetPlayerMetadataDelegate SetPlayerMetadata { get; set; }
|
||||
/// <summary>
|
||||
/// Delegate for checking if main screen player control is currently playing
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public GetIsPlayingDelegate IsPlaying { get; set; }
|
||||
|
||||
public SetPlayerVisibleDelegate SetPlayerVisible { get; set; }
|
||||
|
||||
public SetIsPlayingDelegate SetIsPlaying { get; set; }
|
||||
|
||||
#endregion Player
|
||||
|
||||
#region Lifecycle
|
||||
/// <summary>
|
||||
/// Called by main screen on view appearing
|
||||
/// </summary>
|
||||
/// <typeparam name="object"></typeparam>
|
||||
/// <returns></returns>
|
||||
public virtual Task OnActive() { return Task.FromResult<object>(null); }
|
||||
|
||||
/// <summary>
|
||||
/// Called by main screen on view disappearing
|
||||
/// </summary>
|
||||
/// <typeparam name="object"></typeparam>
|
||||
/// <returns></returns>
|
||||
public virtual Task OnInactive() { return Task.FromResult<object>(null); }
|
||||
|
||||
#endregion
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -31,7 +31,6 @@ namespace Aurora.Design.Views.Party
|
||||
private ObservableCollection<PartyMember> _members;
|
||||
private ObservableCollection<BaseMedia> _queue;
|
||||
private BaseMedia _selectedMedia;
|
||||
private PlayerService _player;
|
||||
private ClientService _client;
|
||||
|
||||
public PartyViewModel()
|
||||
@ -43,7 +42,6 @@ namespace Aurora.Design.Views.Party
|
||||
_queue = new ObservableCollection<BaseMedia>();
|
||||
|
||||
SetState(PartyState.SelectingHost);
|
||||
this._player = PlayerService.Instance;
|
||||
|
||||
PlayCommand = new Command(OnDoubleClickExecute, CanDoubleClickExecute);
|
||||
|
||||
@ -296,40 +294,33 @@ namespace Aurora.Design.Views.Party
|
||||
|
||||
public override void OnPlayButtonExecute()
|
||||
{
|
||||
switch (_player.PlaybackState)
|
||||
if (base.IsPlaying())
|
||||
{
|
||||
case PlaybackState.Buffering:
|
||||
{
|
||||
//Fire play resume event
|
||||
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
||||
MediaResumedEvent mediaResumed = new MediaResumedEvent();
|
||||
//Fire play stopped event
|
||||
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
||||
MediaPausedEvent mediaPaused = new MediaPausedEvent();
|
||||
|
||||
EventManager.Instance.FireEvent(new BaseEvent()
|
||||
{
|
||||
MediaResumedEvent = mediaResumed
|
||||
});
|
||||
EventManager.Instance.FireEvent(new BaseEvent()
|
||||
{
|
||||
MediaPausedEvent = mediaPaused
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
//Fire play resume event
|
||||
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
||||
MediaResumedEvent mediaResumed = new MediaResumedEvent();
|
||||
|
||||
break;
|
||||
}
|
||||
case PlaybackState.Playing:
|
||||
{
|
||||
//Fire play stopped event
|
||||
AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata;
|
||||
MediaPausedEvent mediaPaused = new MediaPausedEvent();
|
||||
|
||||
EventManager.Instance.FireEvent(new BaseEvent()
|
||||
{
|
||||
MediaPausedEvent = mediaPaused
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
EventManager.Instance.FireEvent(new BaseEvent()
|
||||
{
|
||||
MediaResumedEvent = mediaResumed
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanPlayButtonExecute()
|
||||
{
|
||||
return this._state == PartyState.Hosting && this._player.IsLoaded;
|
||||
return this._state == PartyState.Hosting;
|
||||
}
|
||||
|
||||
public override bool CanNextButtonExecute()
|
||||
@ -491,21 +482,19 @@ namespace Aurora.Design.Views.Party
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private async void PlayFromBeginning(BaseMedia args)
|
||||
private void PlayFromBeginning(BaseMedia args)
|
||||
{
|
||||
base.Media = args;
|
||||
await _player.LoadMedia(base.Media).ConfigureAwait(true);
|
||||
_player.Play();
|
||||
base.ChangePlayerState(args, Main.PlayAction.Play);
|
||||
}
|
||||
|
||||
private void PlayResume()
|
||||
{
|
||||
_player.Play();
|
||||
base.ChangePlayerState(null, Main.PlayAction.Resume);
|
||||
}
|
||||
|
||||
private void StopPlaying()
|
||||
{
|
||||
_player.Pause();
|
||||
base.ChangePlayerState(null, Main.PlayAction.Pause);
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
|
@ -6,8 +6,8 @@
|
||||
x:Class="Aurora.Design.Views.Songs.SongsView">
|
||||
<ContentView.Content>
|
||||
<library:Library
|
||||
ItemsSource="{Binding SongsList}"
|
||||
SelectedItem="{Binding SelectedSong}"
|
||||
ItemDoubleClicked="{Binding PlayCommand}"/>
|
||||
ItemsSource="{Binding SongsList}"
|
||||
SelectedItem="{Binding SelectedSong}"
|
||||
ItemDoubleClicked="{Binding DoubleClickCommand}"/>
|
||||
</ContentView.Content>
|
||||
</ContentView>
|
@ -1,7 +1,6 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using Aurora.Models.Media;
|
||||
using Aurora.Services;
|
||||
using Aurora.Services.PlayerService;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Aurora.Design.Views.Songs
|
||||
@ -11,16 +10,14 @@ namespace Aurora.Design.Views.Songs
|
||||
#region Fields
|
||||
private ObservableCollection<BaseMedia> _songsList;
|
||||
private BaseMedia _selectedSong;
|
||||
private PlayerService _player;
|
||||
|
||||
#endregion Fields
|
||||
|
||||
#region Constructor
|
||||
public SongsViewModel()
|
||||
{
|
||||
_player = PlayerService.Instance;
|
||||
_songsList = new ObservableCollection<BaseMedia>();
|
||||
PlayCommand = new Command(OnPlayButtonExecute, CanPlayButtonExecute);
|
||||
DoubleClickCommand = new Command(OnDoubleClickExecute, OnDoubleClickCanExecute);
|
||||
|
||||
Initialize();
|
||||
}
|
||||
@ -40,7 +37,7 @@ namespace Aurora.Design.Views.Songs
|
||||
set { SetProperty(ref _selectedSong, value); }
|
||||
}
|
||||
|
||||
public Command PlayCommand { get; private set; }
|
||||
public Command DoubleClickCommand { get; private set; }
|
||||
|
||||
#endregion Properties
|
||||
|
||||
@ -65,57 +62,24 @@ namespace Aurora.Design.Views.Songs
|
||||
|
||||
public override bool CanPlayButtonExecute()
|
||||
{
|
||||
switch (_player.PlaybackState)
|
||||
{
|
||||
case PlaybackState.Buffering:
|
||||
{
|
||||
return true;
|
||||
}
|
||||
case PlaybackState.Playing:
|
||||
{
|
||||
return true;
|
||||
}
|
||||
case PlaybackState.Stopped:
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public async override void OnPlayButtonExecute()
|
||||
public override void OnPlayButtonExecute()
|
||||
{
|
||||
if (_selectedSong == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
base.Media = _selectedSong;
|
||||
if (!_player.IsMediaLoaded(base.Media))
|
||||
if (base.IsPlaying())
|
||||
{
|
||||
await _player.LoadMedia(base.Media).ConfigureAwait(true);
|
||||
base.ChangePlayerState(_selectedSong, Main.PlayAction.Pause);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.ChangePlayerState(_selectedSong, Main.PlayAction.Play);
|
||||
}
|
||||
|
||||
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()
|
||||
@ -128,6 +92,21 @@ namespace Aurora.Design.Views.Songs
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void OnDoubleClickExecute()
|
||||
{
|
||||
if (_selectedSong == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
base.ChangePlayerState(_selectedSong, Main.PlayAction.Play);
|
||||
}
|
||||
|
||||
public bool OnDoubleClickCanExecute()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
#endregion Commands
|
||||
}
|
||||
}
|
||||
|
@ -4,8 +4,6 @@ using System.Threading;
|
||||
using Grpc.Core;
|
||||
using Aurora.Models.Media;
|
||||
using Aurora.Proto.Sync;
|
||||
using Aurora.Proto.Events;
|
||||
using Aurora.Proto.Party;
|
||||
using LibVLCSharp.Shared;
|
||||
|
||||
namespace Aurora.Services.PlayerService
|
||||
@ -234,7 +232,10 @@ namespace Aurora.Services.PlayerService
|
||||
/// <param name="args"></param>
|
||||
private void OnStopped(object sender, EventArgs args)
|
||||
{
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Stopped));
|
||||
if (PlaybackStateChanged != null)
|
||||
{
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Stopped));
|
||||
}
|
||||
_state = PlaybackState.Stopped;
|
||||
this.Unload();
|
||||
}
|
||||
|
Reference in New Issue
Block a user