public IObservable <PortableUnit> SaveAsync(T item) { if (item == null) { return(ObservableEx.SingleUnit()); } return(Observable.CreateWithDisposable <PortableUnit>( observer => Scheduler.ThreadPool.Schedule(() => { try { var key = this.getKeyFromItem(item); var filePath = this.GetFilePath(key); using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) { this.EnsureDirectoryExistsAsync(storage); using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Create, storage)) { serializer.WriteObject(stream, item); stream.Flush(); stream.Close(); } } observer.OnNext(ObservableEx.Unit); observer.OnCompleted(); } catch (IOException error) { observer.OnError(error); } })).Wait(this.syncLock, TimeSpan.FromSeconds(5))); }
/// <summary> /// </summary> /// <param name="player"> /// </param> /// <returns> /// </returns> private IObservable <PortableUnit> StopPlayingAsync(BackgroundAudioPlayer player) { Debug.WriteLine("Player: StopPlayingAsync"); return(this.NowPlaying.StopAsync(player) .Catch <PortableUnit, ServiceException>(ex => ObservableEx.SingleUnit()) .Catch <PortableUnit, WebException>(ex => ObservableEx.SingleUnit()) .ObserveOn(Scheduler.CurrentThread) .Do(_ => this.StopPlayingMix(player), ex => this.StopPlayingMix(player))); }
public static IObservable <PortableUnit> StopAsync(this PlayingMixContract nowPlaying, TimeSpan timePlayed) { ResetNowPlayingTile(); Storage.Delete(NowPlayingFilePath); if (nowPlaying == null) { return(ObservableEx.SingleUnit()); } return(AddToMixTrackHistoryAsync(nowPlaying, timePlayed)); }
/// <summary> /// </summary> /// <param name = "url"> /// </param> /// <param name = "fileName"> /// </param> /// <param name="overwrite">A value indicating whether to overwrite any existing file</param> /// <returns> /// </returns> public IObservable <PortableUnit> GetAndSaveFileAsync(Uri url, string fileName, bool overwrite) { if (!overwrite && this.storage.Exists(fileName)) { return(ObservableEx.SingleUnit()); } return(this.GetStreamAsync(url, false).TrySelect( stream => { using (stream) { this.storage.Save(fileName, stream); } return ObservableEx.Unit; })); }
public IObservable <PortableUnit> DeleteAsync(T item) { return(Observable.Defer( () => { var key = this.getKeyFromItem(item); var filePath = this.GetFilePath(key); using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) { this.EnsureDirectoryExistsAsync(storage); if (storage.FileExists(filePath)) { storage.DeleteFile(filePath); } } return ObservableEx.SingleUnit(); }).Wait(this.syncLock, TimeSpan.FromSeconds(5))); }
public static IObservable <PortableUnit> AddToMixTrackHistoryAsync(this PlayingMixContract playing, TimeSpan timePlayed) { // If play duration was more than 30 seconds, post the report to Pay The Man if (timePlayed < TimeSpan.FromSeconds(30)) { return(AddToMixesPlayedTracks(playing.MixId, new List <TrackContract>() { playing.Set.Track }).ToUnit().Catch <PortableUnit, Exception>(ex => ObservableEx.SingleUnit())); } var payment = from response in Downloader.GetDeserializedAsync <ResponseContract>(ApiUrl.ReportTrack(playing.PlayToken, playing.MixId, playing.Set.Track.Id)) from mixTrackList in AddToMixesPlayedTracks(playing.MixId, new List <TrackContract>() { playing.Set.Track }) select new PortableUnit(); return(payment.Catch <PortableUnit, Exception>(ex => ObservableEx.SingleUnit())); }
/// <summary> /// </summary> /// <param name="player"> /// </param> private IObservable <PortableUnit> PlayTrackAsync(BackgroundAudioPlayer player) { if (this.UserSettings.PlayOverWifiOnly && NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.Wireless80211) { return(this.StopPlayingAsync(player)); } if (this.NowPlaying == null || this.NowPlaying.Set == null) { Debug.WriteLine("Player: PlayTrackAsync (Now Playing not set)"); // Reset as we don't know what we're playing anymore. return(this.StopPlayingAsync(player)); } try { if (player.PlayerState == PlayState.Paused && player.Track.Tag.StartsWith(this.NowPlaying.MixId + "|")) { Debug.WriteLine("Player: PlayTrackAsync (Resume from Paused)"); // If we're paused, we already have // the track set, so just resume playing. player.Volume = 1; player.Play(); return(ObservableEx.SingleUnit()); } } catch (InvalidOperationException) { // Background audio resources not available return(ObservableEx.SingleUnit()); } if (this.NowPlaying.Set.Track == null || this.NowPlaying.Set.Track.TrackUrl == null) { return(this.StopPlayingAsync(player)); } Debug.WriteLine("Player: PlayTrackAsync (Playing)"); // Set which track to play. When the TrackReady state is received // in the OnPlayStateChanged handler, call player.Play(). return(from trackAddress in PlayerService.GetTrackAddressAsync(this.NowPlaying.Set.Track).ObserveOn(Scheduler.CurrentThread).Do( trackUrl => { var coverUrl = this.NowPlaying.Cover.ThumbnailUrl; var playControls = !this.NowPlaying.Set.IsLastTrack && this.NowPlaying.Set.SkipAllowed ? EnabledPlayerControls.Pause | EnabledPlayerControls.SkipNext | EnabledPlayerControls.FastForward : EnabledPlayerControls.Pause; var track = new AudioTrack( trackUrl, this.NowPlaying.Set.Track.Name, this.NowPlaying.Set.Track.Artist, this.NowPlaying.MixName, coverUrl, this.NowPlaying.MixId + "|" + this.NowPlaying.Set.Track.Id, playControls); player.Track = track; player.Volume = 1; }) select ObservableEx.Unit); }