From 776b82ef0c83dcc16a9241df400394a0d86a465a Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 22 May 2014 22:00:50 +0200 Subject: [PATCH] currentplaying wp --- Xbmc2S.Model/MainVm.cs | 15 +++++++- Xbmc2S.Model/Playback/CurrentPlaybackVm.cs | 25 +++++++++--- Xbmc2S.RT.Shared/App.xaml.cs | 13 +++++++ .../Common/BooleanToVisibilityConverter.cs | 10 ++++- Xbmc2S.RT.Shared/Global/HubPage.xaml | 6 +-- Xbmc2S.RT.Shared/Global/HubPage.xaml.cs | 38 +++++++++++++++++++ Xbmc2S.RT.Shared/Xbmc2S.RT.Shared.projitems | 7 ---- .../Common/PlatformStyles.xaml | 1 - .../Global/CurrentPlayingCtrl.xaml.cs | 0 Xbmc2S.RT.WindowsPhone/ProgressIndicator.cs | 10 ++--- .../Xbmc2S.RT.WindowsPhone.csproj | 9 ++++- .../Global/CurrentPlayingCtrl.xaml | 2 +- Xbmc2S.RT/Xbmc2S.RT.csproj | 7 ++++ 13 files changed, 117 insertions(+), 26 deletions(-) rename {Xbmc2S.RT.Shared => Xbmc2S.RT.WindowsPhone}/Global/CurrentPlayingCtrl.xaml.cs (100%) rename {Xbmc2S.RT.Shared => Xbmc2S.RT}/Global/CurrentPlayingCtrl.xaml (97%) diff --git a/Xbmc2S.Model/MainVm.cs b/Xbmc2S.Model/MainVm.cs index 05bb64c..3fdcc6b 100644 --- a/Xbmc2S.Model/MainVm.cs +++ b/Xbmc2S.Model/MainVm.cs @@ -234,10 +234,21 @@ public IItemsSource GetMovieSource(ItemsSourceReference itemsSourceReference = n } return itemsSource; } - + + private CurrentPlaybackVm _currentPlaybackVm; + public async Task GetCurrentPlayingItem() { - return new CurrentPlaybackVm(_appContext); + if (_currentPlaybackVm == null) + { + _currentPlaybackVm = new CurrentPlaybackVm(_appContext); + } + else + { + _currentPlaybackVm.RefreshNow(); + } + + return _currentPlaybackVm; } public PersonVm SearchPerson(string name) diff --git a/Xbmc2S.Model/Playback/CurrentPlaybackVm.cs b/Xbmc2S.Model/Playback/CurrentPlaybackVm.cs index 779411a..ce791ad 100644 --- a/Xbmc2S.Model/Playback/CurrentPlaybackVm.cs +++ b/Xbmc2S.Model/Playback/CurrentPlaybackVm.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; using System.Windows.Input; using Okra.Core; @@ -98,16 +99,26 @@ PlayingXbmcItemVm CurrentItemFactory(XBMCRPC.List.Item.All currentItem, int play } throw new NotSupportedException(); } + + private CancellationTokenSource _delayCancel; + + public void RefreshNow() + { + _delayCancel.Cancel(); + } private async Task CurrentPlayingLoop() { - while (true) { bool failed = false; try { - await GetCurrentPlayingItem(); + _delayCancel= new CancellationTokenSource(); + await GetCurrentPlayingItem(_delayCancel.Token); + } + catch (TaskCanceledException) + { } catch (Exception) { @@ -120,7 +131,7 @@ private async Task CurrentPlayingLoop() } } - private async Task GetCurrentPlayingItem() + private async Task GetCurrentPlayingItem(CancellationToken delayCancel) { var activePlayers = await _appContext.XBMC.Player.GetActivePlayers(); if (activePlayers.Count == 0) @@ -162,7 +173,7 @@ private async Task GetCurrentPlayingItem() IsPlaying = false; CurrentItem = null; } - await Task.Delay(TimeSpan.FromSeconds(10)); + await Task.Delay(TimeSpan.FromSeconds(10),delayCancel); } else { @@ -239,7 +250,11 @@ public bool IsPlaying get { return _isPlaying; } set { - _isPlaying = value; OnPropertyChanged(); } + if (SetProperty(ref _isPlaying, value)) + { + + } + } } public ICommand Pause { get; private set; } diff --git a/Xbmc2S.RT.Shared/App.xaml.cs b/Xbmc2S.RT.Shared/App.xaml.cs index 96053d6..6584e19 100644 --- a/Xbmc2S.RT.Shared/App.xaml.cs +++ b/Xbmc2S.RT.Shared/App.xaml.cs @@ -44,6 +44,19 @@ public App() this.Suspending += OnSuspending; Platform.Current= new Util(); SuspensionManager.KnownTypes.Add(typeof(ItemsSourceReference)); + + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; + UnhandledException += App_UnhandledException; + } + + void App_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + e.Handled = true; + } + + void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + e.SetObserved(); } public static MainVm MainVm diff --git a/Xbmc2S.RT.Shared/Common/BooleanToVisibilityConverter.cs b/Xbmc2S.RT.Shared/Common/BooleanToVisibilityConverter.cs index 429f779..6aa1426 100644 --- a/Xbmc2S.RT.Shared/Common/BooleanToVisibilityConverter.cs +++ b/Xbmc2S.RT.Shared/Common/BooleanToVisibilityConverter.cs @@ -12,7 +12,15 @@ public sealed class BooleanToVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, string language) { - return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; + try + { + return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed; + } + catch (Exception) + { + + throw; + } } public object ConvertBack(object value, Type targetType, object parameter, string language) diff --git a/Xbmc2S.RT.Shared/Global/HubPage.xaml b/Xbmc2S.RT.Shared/Global/HubPage.xaml index 79eb192..4a6c8d3 100644 --- a/Xbmc2S.RT.Shared/Global/HubPage.xaml +++ b/Xbmc2S.RT.Shared/Global/HubPage.xaml @@ -143,7 +143,7 @@ - + @@ -169,9 +169,9 @@ - + - + diff --git a/Xbmc2S.RT.Shared/Global/HubPage.xaml.cs b/Xbmc2S.RT.Shared/Global/HubPage.xaml.cs index c2446ff..84db700 100644 --- a/Xbmc2S.RT.Shared/Global/HubPage.xaml.cs +++ b/Xbmc2S.RT.Shared/Global/HubPage.xaml.cs @@ -54,6 +54,13 @@ public HubPage() this.InitializeComponent(); this.navigationHelper = new NavigationHelper(this); this.navigationHelper.LoadState += navigationHelper_LoadState; + navigationHelper.SaveState += navigationHelper_SaveState; + _id = DateTime.Now.Ticks; + } + + void navigationHelper_SaveState(object sender, SaveStateEventArgs e) + { + _currentPlayingItem.PropertyChanged -= _currentPlayingItem_PropertyChanged; } @@ -73,6 +80,8 @@ private async void navigationHelper_LoadState(object sender, LoadStateEventArgs await ConnectViewModel(); } + private long _id; + private async Task ConnectViewModel() { this.DefaultViewModel["Settings"] = App.MainVm.Settings; @@ -90,9 +99,37 @@ private async Task ConnectViewModel() this.DefaultViewModel["AdvancedSteps"] = advancedSteps; DefaultViewModel["CurrentConnection"] = App.MainVm.CurrentConnection; _currentPlayingItem = await App.MainVm.GetCurrentPlayingItem(); + _currentPlayingItem.PropertyChanged += _currentPlayingItem_PropertyChanged; + RefreshCurrentPlayingVisibility(); DefaultViewModel["CurrentPlayingItem"] = _currentPlayingItem; } + void _currentPlayingItem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) + { + if (e.PropertyName == "IsPlaying") + { + RefreshCurrentPlayingVisibility(); + } + } + + private void RefreshCurrentPlayingVisibility() + { + if (_currentPlayingItem.IsPlaying) + { + if (!Hub.Sections.Contains(CurrentPlayingSection)) + { + Hub.Sections.Insert(0, CurrentPlayingSection); + } + } + else + { + if (Hub.Sections.Contains(CurrentPlayingSection)) + { + Hub.Sections.Remove(CurrentPlayingSection); + } + } + } + private void GotoUserVoice() { Launcher.LaunchUriAsync(new Uri("https://xbmc2screen.uservoice.com/")); @@ -297,6 +334,7 @@ private void ToggleFlyout(object sender, RoutedEventArgs e) { RcButton.Flyout.Hide(); } + } diff --git a/Xbmc2S.RT.Shared/Xbmc2S.RT.Shared.projitems b/Xbmc2S.RT.Shared/Xbmc2S.RT.Shared.projitems index 14ce5cc..6a3026e 100644 --- a/Xbmc2S.RT.Shared/Xbmc2S.RT.Shared.projitems +++ b/Xbmc2S.RT.Shared/Xbmc2S.RT.Shared.projitems @@ -45,9 +45,6 @@ MissingFilesPage.xaml - - CurrentPlayingCtrl.xaml - GeneralDetailsView.xaml @@ -179,10 +176,6 @@ Designer MSBuild:Compile - - Designer - MSBuild:Compile - Designer MSBuild:Compile diff --git a/Xbmc2S.RT.WindowsPhone/Common/PlatformStyles.xaml b/Xbmc2S.RT.WindowsPhone/Common/PlatformStyles.xaml index 86edeb7..99bd9e4 100644 --- a/Xbmc2S.RT.WindowsPhone/Common/PlatformStyles.xaml +++ b/Xbmc2S.RT.WindowsPhone/Common/PlatformStyles.xaml @@ -104,5 +104,4 @@ - diff --git a/Xbmc2S.RT.Shared/Global/CurrentPlayingCtrl.xaml.cs b/Xbmc2S.RT.WindowsPhone/Global/CurrentPlayingCtrl.xaml.cs similarity index 100% rename from Xbmc2S.RT.Shared/Global/CurrentPlayingCtrl.xaml.cs rename to Xbmc2S.RT.WindowsPhone/Global/CurrentPlayingCtrl.xaml.cs diff --git a/Xbmc2S.RT.WindowsPhone/ProgressIndicator.cs b/Xbmc2S.RT.WindowsPhone/ProgressIndicator.cs index f12d63d..c403b38 100644 --- a/Xbmc2S.RT.WindowsPhone/ProgressIndicator.cs +++ b/Xbmc2S.RT.WindowsPhone/ProgressIndicator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using System.Windows.Threading; using Windows.UI.Core; using Windows.UI.ViewManagement; using Windows.UI.Xaml.Controls; @@ -56,7 +57,7 @@ private void RefreshIsActive() if (!_refreshPending) { _refreshPending = true; - DoRefresh(); + SmartDispatcher.RunAsync(DoRefresh); } } @@ -71,14 +72,14 @@ private async void DoRefresh() } else { - _progressIndicator.ProgressValue = null; - await _progressIndicator.ShowAsync(); + _progressIndicator.ProgressValue = null; + await _progressIndicator.ShowAsync(); } } public void SetError(string message) { - DisplayError(message); + SmartDispatcher.RunAsync(()=>DisplayError(message)); } @@ -90,7 +91,6 @@ private async void DisplayError(string message) await _progressIndicator.ShowAsync(); await Task.Delay(4000); _progressIndicator.Text = ""; - } } } diff --git a/Xbmc2S.RT.WindowsPhone/Xbmc2S.RT.WindowsPhone.csproj b/Xbmc2S.RT.WindowsPhone/Xbmc2S.RT.WindowsPhone.csproj index b3b3f8f..7aa43cd 100644 --- a/Xbmc2S.RT.WindowsPhone/Xbmc2S.RT.WindowsPhone.csproj +++ b/Xbmc2S.RT.WindowsPhone/Xbmc2S.RT.WindowsPhone.csproj @@ -24,7 +24,7 @@ full false bin\Debug\ - DEBUG;TRACE;NETFX_CORE;WINDOWS_PHONE_APP + TRACE;DEBUG;NETFX_CORE;WINDOWS_PHONE_APP;DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION prompt 4 @@ -113,6 +113,9 @@ + + CurrentPlayingCtrl.xaml + @@ -138,6 +141,10 @@ MSBuild:Compile Designer + + MSBuild:Compile + Designer + MSBuild:Compile Designer diff --git a/Xbmc2S.RT.Shared/Global/CurrentPlayingCtrl.xaml b/Xbmc2S.RT/Global/CurrentPlayingCtrl.xaml similarity index 97% rename from Xbmc2S.RT.Shared/Global/CurrentPlayingCtrl.xaml rename to Xbmc2S.RT/Global/CurrentPlayingCtrl.xaml index b66d68b..3e90416 100644 --- a/Xbmc2S.RT.Shared/Global/CurrentPlayingCtrl.xaml +++ b/Xbmc2S.RT/Global/CurrentPlayingCtrl.xaml @@ -20,7 +20,7 @@ - + diff --git a/Xbmc2S.RT/Xbmc2S.RT.csproj b/Xbmc2S.RT/Xbmc2S.RT.csproj index 30831c0..1d1356c 100644 --- a/Xbmc2S.RT/Xbmc2S.RT.csproj +++ b/Xbmc2S.RT/Xbmc2S.RT.csproj @@ -117,6 +117,9 @@ + + CurrentPlayingCtrl.xaml + ProgressIndicator.xaml @@ -151,6 +154,10 @@ Designer PreserveNewest + + MSBuild:Compile + Designer + Designer MSBuild:Compile