PodcastEpisodeListingItem.cs 4.45 KB
Newer Older
Axelander's avatar
Axelander committed
1 2 3 4 5 6 7 8
using SonicStreamer.Common.System;
using SonicStreamer.Subsonic.Data;
using SonicStreamer.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
9
using System.Xml.Serialization;
Axelander's avatar
Axelander committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
using Windows.UI.Xaml;

namespace SonicStreamer.ViewModelItems
{
    public class PodcastEpisodeListingItem : INotifyPropertyChanged
    {
        #region Properties

        private PodcastEpisode _episode;

        public PodcastEpisode Episode
        {
            get { return _episode; }
            set { Set(ref _episode, value); }
        }

        private bool _isDescriptionVisible;

        public bool IsDescriptionVisible
        {
            get { return _isDescriptionVisible; }
            set
            {
                Set(ref _isDescriptionVisible, value);
                // muss außerhalb sein, damit bei der Initialisierung auch das Symbol gesetzt wird
                ExpandButtonSymbol = value ? "\uE010" : "\uE011";
            }
        }

        private string _expandButtonSymbol;
40
        [XmlIgnore]
Axelander's avatar
Axelander committed
41 42 43 44 45 46 47 48
        public string ExpandButtonSymbol
        {
            get { return _expandButtonSymbol; }
            private set { Set(ref _expandButtonSymbol, value); }
        }

        #endregion

49 50 51
        public PodcastEpisodeListingItem() { }

        public PodcastEpisodeListingItem(PodcastEpisode episode) : this(episode, false) { }
Axelander's avatar
Axelander committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

        public PodcastEpisodeListingItem(PodcastEpisode episode, bool isExpand)
        {
            Episode = episode;
            IsDescriptionVisible = isExpand;
            Episode.CheckLocalFile();
        }

        #region Setter Implementation

        protected bool Set<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
        {
            if (EqualityComparer<T>.Default.Equals(field, value))
            {
                return false;
            }
            field = value;
            NotifyPropertyChanged(propertyName);
            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region Binding Methods

        /// <summary>
        /// Bindable Methode um die Description der Episode ein- oder auszublenden
        /// </summary>
        public void ExpandCollapseButtonClick()
        {
            IsDescriptionVisible = !IsDescriptionVisible;
        }

        /// <summary>
        /// Bindable Methode um die Episode abzuspielen. Die existierende Wiedergabe wird ersetzt
        /// </summary>
        public async void PlayEpisodeClick()
        {
            Microsoft.HockeyApp.HockeyClient.Current.TrackEvent(string.Format("{0} - {1}", GetType().Name,
                "PlayEpisodeClick"));
            await PlaybackService.Current.AddToPlaybackAsync(Episode, true);
        }

        /// <summary>
        /// Bindable Methode um eine Epsiode zur Wiedergabe hinzuzufügen.
        /// </summary>
        public async void AddEpisodeClick()
        {
            Microsoft.HockeyApp.HockeyClient.Current.TrackEvent(string.Format("{0} - {1}", GetType().Name,
                "AddEpisodeClick"));
            await PlaybackService.Current.AddToPlaybackAsync(Episode, false);
        }

        /// <summary>
        /// Bindable Methode um die Episode lokal herunterzuladen
        /// </summary>
        public async void DownloadEpisodeClick()
        {
            Microsoft.HockeyApp.HockeyClient.Current.TrackEvent(string.Format("{0} - {1}", GetType().Name,
                "DownloadEpisodeClick"));
            await Episode.DownloadAsync();
        }

        /// <summary>
        /// Bindable Methode um die Episode auf den Server herunterzuladen
        /// </summary>
        public async void DownloadEpisodeToServerClick()
        {
            Microsoft.HockeyApp.HockeyClient.Current.TrackEvent(string.Format("{0} - {1}", GetType().Name,
                "DownloadEpisodeToServerClick"));
            await SubsonicConnector.Current.CurrentConnection.DownloadEpisodeToServerAsync(Episode.EpisodeId);
            await Task.Delay(TimeSpan.FromSeconds(1));
            var podcastVm = Application.Current.Resources[Constants.ViewModelPodcast] as PodcastViewModel;
            if (podcastVm != null) await podcastVm.LoadSelectedPodcastTracksAsync();
        }

        #endregion
    }
}