Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

(Hybrid Smart Client) RSS Media Player

, 9 Mar 2009
A WPF application for viewing RSS video feeds. Built using the MVVM pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RSSVideoPlayer.Common;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.Specialized;

namespace RSSVideoPlayer.ViewModel 
{
    class MainWindowViewModel : TabViewModel
    {
        RelayCommand _cmdLibrary;
        RelayCommand _cmdRSS;
        RelayCommand _cmdExit;
        ObservableCollection<TabViewModel> _tabs;

     public MainWindowViewModel()
     {
         CreateCommands();
         _tabs = new ObservableCollection<TabViewModel>();
         _tabs.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(OnWorkspacesChanged);
         ShowRSS(); ShowLibrary(); Expanded = true;
     }

 

        #region "Main Window Commands"

        void CreateCommands()
        {

            if (_cmdLibrary == null)
                _cmdLibrary = new RelayCommand(param => this.ShowLibrary());

            if (_cmdRSS == null)
                _cmdRSS = new RelayCommand(param => this.ShowRSS());

            if (_cmdExit == null)
                _cmdExit = new RelayCommand(param => this.Exit());
        }

        public ICommand cmdLibrary
        {
            get { return _cmdLibrary; }
        }
        public ICommand cmdRSS
        {
            get { return _cmdRSS; }
        }
        public ICommand cmdExit
        {
            get { return _cmdExit; }
        }

        void ShowLibrary()
        {
            LibraryViewModel libraryvm = new LibraryViewModel(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
            AddTab<LibraryViewModel>(libraryvm);
            libraryvm.MediaChanged +=new EventHandler<MediaChangedEventArgs>(libraryvm_MediaChanged);
        }

        void libraryvm_MediaChanged(object sender, MediaChangedEventArgs e)
        {
            Source = e.NewMedia; 
            Expanded = false;
            OnPropertyChanged("Source");
        }
        void ShowRSS()
        {
            RSSViewModel RSSVM = new RSSViewModel();
            AddTab<RSSViewModel>(RSSVM);
            RSSVM.NewMediaItem += new EventHandler<MediaChangedEventArgs>(RSSVM_NewMediaItem);
        }

        void RSSVM_NewMediaItem(object sender, MediaChangedEventArgs e)
        {
            Source = e.NewMedia;
            Expanded = false;
            OnPropertyChanged("Source");
        }
        #endregion

        #region "Tabs"

        public ObservableCollection<TabViewModel> Tabs
        {
            get { return _tabs; }
        }

       private void AddTab<T>(TabViewModel tab)
        {
           if (_tabs == null)
               _tabs = new ObservableCollection<TabViewModel>();

          var newtabitem = this._tabs.FirstOrDefault(vm => vm is T);

           if (newtabitem == null)
           {
               this._tabs.Add(tab);
               SetActiveWorkspace(tab);
               return;
           }
           OnPropertyChanged("Tabs");
           SetActiveWorkspace(newtabitem);
        }

       void SetActiveWorkspace(TabViewModel tab)
       {
           ICollectionView collectionView = CollectionViewSource.GetDefaultView(this._tabs);
           if (collectionView != null)
               collectionView.MoveCurrentTo(tab);
       }

       void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
       {
           if (e.NewItems != null && e.NewItems.Count != 0)
               foreach (TabViewModel tab in e.NewItems)
                   tab.RequestClose += this.OnWorkspaceRequestClose;

           if (e.OldItems != null && e.OldItems.Count != 0)
               foreach (TabViewModel tab in e.OldItems)
                   tab.RequestClose -= this.OnWorkspaceRequestClose;

           base.OnPropertyChanged("Tabs");
       }

       void OnWorkspaceRequestClose(object sender, EventArgs e)
       {
           TabViewModel tab = sender as TabViewModel;
           tab.Dispose();
           this._tabs.Remove(tab);
       }

        #endregion

       #region "Properties"

       private string _MediaSource = "";
       public  string Source
       {
           get { return _MediaSource; }
           set
           {
               _MediaSource = value;
               base.OnPropertyChanged("Source");
           }
       }

       private bool _expanded;
       public bool Expanded
       {
           get { return _expanded; }
           set {
               if (_expanded != value)
               {
                   _expanded = value;
                   OnPropertyChanged("Expanded");
               }
           }
       }

       #endregion

       void Exit()
       {
           base.OnRequestClose();
       }

    }
}

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 Code Project Open License (CPOL)

Share

About the Author

Paul_Wade
Software Developer (Senior) Anytime Fitness
United States United States
No Biography provided
Follow on   Twitter

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 9 Mar 2009
Article Copyright 2009 by Paul_Wade
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid