Exemple #1
0
        private MediaOptions BindPlaylist(PlaylistOptions options)
        {
            options.NotNull(nameof(options));

            if (options.Medias.IsEmpty())
            {
                Snackbar.MessageQueue.Enqueue("Playlist medias is empty.");
                return(null);
            }

            if (_lastRecord.TotalPlaybackDuration.IsNotEmpty() && _lastRecord.TotalPlaybackDuration != TimeSpanEmpty)
            {
                _viewModel.PlayerWrapper.TotalPlaybackDuration = TimeSpan.Parse(_lastRecord.TotalPlaybackDuration);
                _isPlaylistFirstPlay = true;
            }

            var defaultSelectedIndex = 0;

            if (_lastRecord.LastMediaId > 0)
            {
                var media = options.Medias.SingleOrDefault(m => m.Id == _lastRecord.LastMediaId);
                if (media.IsNotNull())
                {
                    defaultSelectedIndex = options.Medias.IndexOf(media);
                }
            }
            else if (_viewModel.PlayerWrapper.State == PlayState.Playing)
            {
                defaultSelectedIndex = options.Medias.IndexOf(_viewModel.PlayerWrapper.Media);
            }

            if (defaultSelectedIndex < 0)
            {
                defaultSelectedIndex = 0;
            }

            lbxPlaylist.Items.Clear();

            var mediasCountLength = options.Medias.Count.ToString().Length;

            options.Medias.ForEach(media => AddPlaylist(media));

            _lastPlaylistItem = lbxPlaylist.Items[defaultSelectedIndex] as TextBlock;
            ChangeLastPlaylistItemState();

            tbkPlaylistTitle.Text  = options.Title;
            tbkPlaylistAuthor.Text = $"by {options.Author}";
            tbkPlaylistDescr.Text  = options.Descr;

            cbxLoopingState.SelectedIndex = PlaylistHelper.GetLoopingStatesIndex(options.Looping);

            tbxSinglePlayDurationTime.Text = options.SinglePlaybackDuration;
            tbxSinglePlayIntervalTime.Text = options.SinglePlaybackInterval;
            tbxTotalPlayDurationTime.Text  = options.StopPlaybackDuration;

            if (options.SinglePlaybackDuration.IsNotEmpty() && options.SinglePlaybackDuration != TimeSpanEmpty)
            {
                _viewModel.PlayerWrapper.SinglePlaybackDuration = TimeSpan.Parse(options.SinglePlaybackDuration);
            }

            if (options.SinglePlaybackInterval.IsNotEmpty() && options.SinglePlaybackInterval != TimeSpanEmpty)
            {
                _viewModel.PlayerWrapper.SinglePlaybackInterval = TimeSpan.Parse(options.SinglePlaybackInterval);
            }

            if (options.StopPlaybackDuration.IsNotEmpty() && options.StopPlaybackDuration != TimeSpanEmpty)
            {
                _viewModel.PlayerWrapper.StopPlaybackDuration = TimeSpan.Parse(options.StopPlaybackDuration);
            }

            return(_lastPlaylistItem.Tag as MediaOptions);


            void AddPlaylist(MediaOptions media)
            {
                if (media.Title.IsNull())
                {
                    media.Title = media.Source?.ToString().SplitPairByLastIndexOf('/').Value.Trim();

                    if (!btnSave.IsEnabled)
                    {
                        btnSave.IsEnabled = true;
                    }
                }

                var item = new TextBlock();

                item.Text = $"{media.Id.FormatString(mediasCountLength)}. {media.Title} [{media.Volume}]";
                item.Tag  = media;

                lbxPlaylist.Items.Add(item);
            }
        }