Click here to Skip to main content
15,896,912 members
Articles / Multimedia / Audio

Build a Pandora clone with Silverlight 4

Rate me:
Please Sign up or sign in to vote.
4.94/5 (51 votes)
23 Oct 2012Ms-PL23 min read 193.6K   1   78  
Exercising Silverlight 4 to build a fun, real-world app
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using JudahHimango.Chavah.MessianicMusicService;
using System.Diagnostics.Contracts;
using JudahHimango.Chavah.Infrastructure;
using JudahHimango.Chavah.Infrastructure.Messages;

namespace JudahHimango.Chavah.ViewModels
{
    public class SongViewModel : INotifyPropertyChanged
    {
        private readonly Song song;
        private readonly IMessageBus messageBus;
        private int rank;
        private string rankDescription;

        public event PropertyChangedEventHandler PropertyChanged;

        public SongViewModel()
        {
            // For design-time purposes only.
            if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
            {
                throw new NotSupportedException("The default constructor is for designer aid only.");
            }
        }

        public SongViewModel(Song song, IMessageBus messageBus)
        {
            Contract.Requires(song != null);
            Contract.Requires(messageBus != null);

            this.song = song;
            this.messageBus = messageBus;
            this.Name = song.Name;
            this.Artist = song.Artist;
            this.Album = song.Album;
            this.LikeStatus = song.SongLike;
            this.Rank = song.CommunityRank;
            this.RankDescription = GetRankDescription();
        }

        public string Name { get; set; }
        public string Artist { get; set; }
        public string Album { get; set; }
        public SongLike LikeStatus { get; set; }
        public int Rank
        {
            get { return this.rank; }
            set
            {
                if (this.rank != value)
                {
                    this.rank = value;
                    OnPropertyChanged("Rank");
                    this.RankDescription = GetRankDescription();
                }
            }
        }

        private string GetRankDescription()
        {
            if (rank >= 10)
            {
                return "Oh yes! A minyan of Messianics love this tune.";
            }
            if (rank >= 5)
            {
                return string.Format("Great song! {0} Messianics love it!", rank);
            }
            if (rank == 1)
            {
                return string.Format("At least one Messianic music maestro likes this song.");
            }
            if (rank > 0)
            {
                return string.Format("{0} Messianics agree - this song is a great tune for the Lord!", rank);
            }
            if (rank < 0)
            {
                return string.Format("{0} Messianics don't care for this song.", Math.Abs(rank));
            }
            return "The Messianic community doesn't have much to say about this song. Give it a thumb up if you like it.";
        }

        public string RankDescription
        {
            get { return this.rankDescription; }
            set
            {
                if (this.rankDescription != value)
                {
                    this.rankDescription = value;
                    OnPropertyChanged("RankDescription");
                }
            }
        }

        public ICommand ThumbUpCommand
        {
            get
            {
                return new DelegateCommand(ThumbUp);
            }
        }

        public ICommand ThumbDownCommand
        {
            get
            {
                return new DelegateCommand(ThumbDown);
            }
        }

        private void ThumbUp()
        {
            if (song != null)
            {
                if (song.SongLike != SongLike.Like)
                {
                    this.messageBus.Publish(new LikeSongEventArgs(song));
                }

                var incrementAmount = song.SongLike != SongLike.Like ? 1 : 0;
                this.Rank += incrementAmount;

                song.SongLike = SongLike.Like;
            }
        }

        private void ThumbDown()
        {
            if (song != null)
            {
                if (song.SongLike != SongLike.Dislike)
                {
                    this.messageBus.Publish(new DislikeSongEventArgs(song));
                }

                var decrementAmount = song.SongLike != SongLike.Dislike ? 1 : 0;
                this.Rank -= decrementAmount;

                song.SongLike = SongLike.Dislike;
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                var args = new PropertyChangedEventArgs(propertyName);
                this.PropertyChanged(this, args);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer 3M
United States United States
Front end developer, RavenDB enthusiast, blogger, musician, husband, and father of 3.

Comments and Discussions