public static IObservable <PlayingMixContract> StartPlayingAsync(this MixContract mix)
        {
            var playingMix = from playToken in GetOrCreatePlayTokenAsync()
                             from response in Downloader.GetDeserializedAsync <PlayResponseContract>(ApiUrl.Play(playToken, mix.Id))
                             .Repeat(2)
                             .Log("StartPlayingAsync: Attempting")
                             .TakeFirst(ValidResponse)
                             .Log("StartPlayingAsync: Valid Response")
                             from added in AddToRecentlyPlayedAsync(mix).Log("AddToRecentlyPlayedAsync: Done")

                             select new PlayingMixContract
            {
                PlayToken = playToken,
                MixId     = mix.Id,
                MixName   = mix.Name,
                Cover     = mix.Cover,
                Set       = response.Set
            };

            return(from playing in playingMix
                   from tile in SetNowPlayingTileAsync(
                       playing,
                       DataStrings.Title_ApplicationName,
                       DataStrings.Title_NowPlaying)
                   .Log("SetNowPlayingTileAsync: Done")
                   select playing);
        }
        /// <summary>
        /// </summary>
        /// <param name="loadMix">
        /// </param>
        /// <returns>
        /// </returns>
        public IObservable <Unit> LoadAsync(MixContract loadMix)
        {
            this.currentMix = loadMix;
            this.InitializeBackgroundAudioPlayer();

            if (this.isDataLoaded)
            {
                this.UpdatePlayerState();
                return(this.RefreshPlayedTracksAsync(this.currentMix));
            }

            this.isDataLoaded = true;
            this.LoadNowPlaying();
            this.UpdatePlayerState();

            if (this.PlayOnLoad)
            {
                this.PlayOnLoad = false;
                if (!this.IsPlayingTrackForThisMix)
                {
                    this.StartPlayingMixFromBeginning();
                }
            }

            return(this.RefreshPlayedTracksAsync(this.currentMix));
        }
Beispiel #3
0
        public void Load(MixContract mix, bool censor)
        {
            this.IsExplicit = mix.IsExplicit && censor;
            this.MixName    = censor ? Censorship.Censor(mix.Name) : mix.Name;
            var lines =
                mix.Description.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim())
                .Where(t => !string.IsNullOrWhiteSpace(t));

            this.Description   = censor ? Censorship.Censor(string.Join(Environment.NewLine, lines)) : string.Join(Environment.NewLine, lines);
            this.ThumbnailUrl  = this.IsExplicit ? this.AddQuery(mix.Cover.ThumbnailUrl, "nsfw") : mix.Cover.ThumbnailUrl;
            this.ImageUrl      = this.IsExplicit ? this.AddQuery(mix.Cover.OriginalUrl, "nsfw") : mix.Cover.OriginalUrl;
            this.TileTitle     = this.MixName.Replace(" ", Environment.NewLine);
            this.MixId         = mix.Id;
            this.NavigationUrl = PageUrl.Play(this.MixId, false, this.MixName);


            this.TrackCount = mix.TrackCount;
            var duration = TimeSpan.FromSeconds(mix.DurationSeconds);

            this.TotalDuration = "(" + duration.ToFormattedString() + ")";
            if (mix.RestUrl != null)
            {
                this.LinkUrl = new Uri(mix.RestUrl, UriKind.RelativeOrAbsolute);
            }

            this.Liked              = mix.Liked;
            this.CreatedBy          = mix.User.Name;
            this.CreatedByAvatarUrl = new Uri(mix.User.Avatar.ImageUrl, UriKind.RelativeOrAbsolute);
            this.Created            = mix.Created.ParseToLocalDateTimeEnsurePast();
            this.PlaysCount         = mix.PlaysCount;
            this.LikesCount         = mix.LikesCount;
            this.Tags = mix.Tags;
        }
        public static void UnpinFromStart(MixContract mix)
        {
            var tile = ShellTile.ActiveTiles.FirstOrDefault(t => t.NavigationUri.ToString() == GetPlayPageUrl(mix).ToString());

            if (tile != null)
            {
                tile.Delete();
            }
        }
        protected static Uri GetPlayPageUrl(MixContract mix)
        {
            if (mix == null || mix.Id == null)
            {
                return(null);
            }

            return(new Uri("/PlayPage.xaml?mix=" + mix.Id + "&title=" + Uri.EscapeDataString(mix.Name ?? string.Empty), UriKind.Relative));
        }
        public static bool IsPinned(MixContract mix)
        {
            if (mix == null)
            {
                return(false);
            }

            return(ShellTile.ActiveTiles.Any(t => t.NavigationUri == GetPlayPageUrl(mix)));
        }
        private IObservable <Unit> RefreshPlayedTracksAsync(MixContract loadMix)
        {
            this.Tracks.Clear();
            this.ShowProgress(StringResources.Progress_Loading);
            var tracks = from response in loadMix.PlayedTracksAsync()
                         where response != null && response.Tracks != null
                         from track in response.Tracks.ToObservable()
                         select new TrackViewModel(track);

            return(tracks.ObserveOnDispatcher().Do(this.AddToTrackToList, this.UpdateMessage).FinallySelect(() => new Unit()).Finally(this.HideProgress));
        }
        public static IObservable <PlayedTracksResponseContract> PlayedTracksAsync(this MixContract mix)
        {
            var playedTracks = from playToken in GetOrCreatePlayTokenAsync()
                               from response in Downloader.GetDeserializedAsync <PlayedTracksResponseContract>(ApiUrl.PlayedTracks(playToken, mix.Id))
                               .Coalesce(() => new PlayedTracksResponseContract
            {
                Tracks = new List <TrackContract>()
            })
                               from updatedList in AddToMixesPlayedTracks(mix.Id, response.Tracks)
                               select updatedList;

            return(playedTracks);
        }
        /// <summary>
        /// The load mix.
        /// </summary>
        /// <param name="loadMix">
        /// The load mix.
        /// </param>
        private void LoadMix(MixContract loadMix)
        {
            this.mixData         = loadMix;
            this.CreatedByUserId = loadMix.User.Id;
            this.Mix             = new MixViewModel(loadMix, this.censor);
            this.OnPropertyChanged("MixName");
            this.UpdateLikedState();

            this.UpdatePinnedState();
            this.ReviewMixCommand.RaiseCanExecuteChanged();
            this.ShareCommand.RaiseCanExecuteChanged();
            this.EmailCommand.RaiseCanExecuteChanged();
        }
Beispiel #10
0
        public static IObservable <PortableUnit> AddToRecentlyPlayedAsync(MixContract mix)
        {
            string imageFilePath = "/Shared/Media/" + mix.Id + "-Original.jpg";

            return(from recentlyPlayed in RecentlyPlayedAsync().Do(
                       m =>
            {
                var duplicates = m.Mixes.Where(d => d.Id == mix.Id).ToList();
                foreach (var duplicate in duplicates)
                {
                    m.Mixes.Remove(duplicate);
                }

                m.Mixes.Insert(0, mix);

                if (m.Mixes.Count > 10)
                {
                    m.Mixes.Remove(m.Mixes.Last());
                }
            })
                   from mixes in Storage.SaveJsonAsync(RecentlyPlayedFilePath, recentlyPlayed)
                   from save in Downloader.GetAndSaveFileAsync(mix.Cover.ThumbnailUrl, imageFilePath, false).Do(d =>
            {
                try
                {
                    using (var stream = Storage.ReadStream(imageFilePath))
                    {
                        if (stream.Length >= 76800)
                        {
                            // TODO: Something else..?
                            return;
                        }

                        var mediaHistoryItem = new MediaHistoryItem
                        {
                            Title = mix.Name,
                            ImageStream = stream
                        };
                        mediaHistoryItem.PlayerContext.Add("MixId", mix.Id);
                        MediaHistory.Instance.NowPlaying = mediaHistoryItem;
                        stream.Close();
                    }

                    using (var secondStream = Storage.ReadStream(imageFilePath))
                    {
                        if (secondStream.Length >= 76800)
                        {
                            // TODO: Something else..?
                            return;
                        }

                        var item = new MediaHistoryItem {
                            Title = mix.Name, ImageStream = secondStream
                        };
                        item.PlayerContext.Add("MixId", mix.Id);
                        MediaHistory.Instance.WriteRecentPlay(item);
                        secondStream.Close();
                    }
                }
                catch (COMException)
                {
                    // No COM you can't stop the music.
                }
            })
                   select ObservableEx.Unit);
        }
Beispiel #11
0
 /// <summary>
 /// </summary>
 /// <param name="mix">
 /// </param>
 /// <param name="censor"></param>
 public MixViewModel(MixContract mix, bool censor) : this()
 {
     this.Load(mix, censor);
 }
 /// <summary>
 /// Initializes a new instance of the RecentMixViewModel class.
 /// </summary>
 public RecentMixViewModel(MixContract mix, bool censor)
     : base(mix, censor)
 {
 }
Beispiel #13
0
        public static void PinToStart(MixContract mix)
        {
            if (!BackgroundPinService.IsPinned(mix))
            {
                var tileUrl = GetPlayPageUrl(mix);
                if (tileUrl == null)
                {
                    return;
                }

                if (PlatformHelper.IsWindowsPhone78OrLater)
                {
                    // Get the new FlipTileData type.
                    Type flipTileDataType = Type.GetType("Microsoft.Phone.Shell.FlipTileData, Microsoft.Phone");

                    // Get the ShellTile type so we can call the new version of "Update" that takes the new Tile templates.
                    Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone");
                    if (flipTileDataType == null || shellTileType == null)
                    {
                        return;
                    }

                    // Get the constructor for the new FlipTileData class and assign it to our variable to hold the Tile properties.
                    var c = flipTileDataType.GetConstructor(new Type[] { });
                    if (c == null)
                    {
                        return;
                    }

                    var updateTileData = c.Invoke(null);
                    // UpdateFlipTile(mix.Name, mix.Name, mix.Description, mix.Description, mix.TrackCount, tileUrl, mix.Cover.ThumbnailUrl, mix.Cover.ThumbnailUrl, null, mix.Cover.OriginalUrl, null, false);


                    // Set the properties.
                    SetProperty(updateTileData, "Title", mix.Name ?? string.Empty);
                    SetProperty(updateTileData, "Count", mix.TrackCount);
                    SetProperty(updateTileData, "BackTitle", mix.Name ?? string.Empty);
                    SetProperty(updateTileData, "BackContent", mix.Description ?? string.Empty);
                    SetProperty(updateTileData, "SmallBackgroundImage", mix.Cover.ThumbnailUrl ?? ResetUrl);
                    SetProperty(updateTileData, "BackgroundImage", mix.Cover.ThumbnailUrl ?? ResetUrl);
                    SetProperty(updateTileData, "BackBackgroundImage", ResetUrl);
                    SetProperty(updateTileData, "WideBackgroundImage", mix.Cover.OriginalUrl ?? ResetUrl);
                    SetProperty(updateTileData, "WideBackBackgroundImage", ResetUrl);
                    SetProperty(updateTileData, "WideBackContent", mix.Description ?? string.Empty);

                    // Invoke the new version of ShellTile.Update.
                    var createMethod = shellTileType.GetMethods().FirstOrDefault(m => m.Name == "Create" && m.GetParameters().Length == 3);
                    if (createMethod != null)
                    {
                        createMethod.Invoke(null, new[] { tileUrl, updateTileData, true });
                        return;
                    }
                }

                // Fallback to the WinPhone 7.0 way
                ShellTile.Create(
                    tileUrl,
                    new StandardTileData
                {
                    Title               = mix.Name ?? string.Empty,
                    BackContent         = mix.Description ?? string.Empty,
                    BackgroundImage     = mix.Cover.ThumbnailUrl ?? ResetUrl,
                    BackTitle           = mix.Name ?? string.Empty,
                    BackBackgroundImage = ResetUrl,
                    Count               = mix.TrackCount
                });
            }
        }