Click here to Skip to main content
15,891,204 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 124.8K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using System.Windows.Input;
using CBR.Core.Helpers;
using CBR.Core.Models;
using CBR.Core.Services;
using System.Windows;
using CBR.Views;

namespace CBR.ViewModels
{
    public class ExplorerViewModel : ViewModelBase
    {
        #region ----------------CONSTRUCTOR----------------

        public ExplorerViewModel()
		{
			//register to the mediator for messages
			Mediator.Instance.Register(
				(Object o) =>
				{
					CatalogData = (Catalog)o;
				},
                ViewModelMessages.CatalogChanged);

            Mediator.Instance.Register(
                (Object o) =>
                {
                    Sort( o as string );
                },
                ViewModelMessages.ExplorerSort);

            Mediator.Instance.Register(
                (Object o) =>
                {
                    Group( o as string );
                },
                ViewModelMessages.ExplorerGroup);

            Mediator.Instance.Register(
                (Object o) =>
                {
                    ChangeExplorerView(o as string);
                },
                ViewModelMessages.ExplorerView);
		}

		#endregion

        #region ----------------PROPERTIES----------------

        private Catalog _catalogData = new Catalog();
        /// <summary>
        /// owns a copy from MainViewModel
        /// </summary>
        public Catalog CatalogData
        {
            get { return _catalogData; }
            set
            {
                if (_catalogData != value)
                {
                    _catalogData = value;
                    RaisePropertyChanged("CatalogData");
                    RaisePropertyChanged("Books");
                }
            }
        }

        /// <summary>
        /// the collection for list binding
        /// </summary>
        public ICollectionView Books
        {
            get
            {
                if (CatalogData != null)
                {
                    return CollectionViewSource.GetDefaultView(CatalogData.Books);
                }
                else
                    return null;
            }
        }

        private string _searchedText = string.Empty;
        /// <summary>
        /// filter string from ribbon
        /// </summary>
        public string SearchedText
        {
            get { return _searchedText; }
            set
            {
                _searchedText = value;

                Books.Filter = delegate(object obj)
                {
                    if (String.IsNullOrEmpty(_searchedText))
                        return true;

                    Book bk = obj as Book;
                    if (bk == null)
                        return false;

                    return (bk.FileName.IndexOf(_searchedText, 0, StringComparison.InvariantCultureIgnoreCase) > -1);
                };
            }
        }

        private bool _IsExplorerViewThumb = true;
        /// <summary>
        /// explorer display mode as bool, false = grid view
        /// </summary>
        public bool IsExplorerViewThumb
        {
            get { return _IsExplorerViewThumb; }
            set
            {
                if (_IsExplorerViewThumb != value)
                {
                    _IsExplorerViewThumb = value;
                    RaisePropertyChanged("IsExplorerViewThumb");
                }
            }
        }

        /// <summary>
        /// sort dropdown menu items
        /// </summary>
        public List<PropertyViewModel> SortProperties
        {
            get { return GetSortProperties(); }
        }

        /// <summary>
        /// group dropdown menu items
        /// </summary>
        public List<PropertyViewModel> GroupProperties
        {
            get { return GetGroupProperties(); }
        }

        #endregion

        #region ---------------- COMMANDS ----------------

        #region forward command
        private ICommand forwardCommand;
        public ICommand ForwardCommand
        {
            get
            {
                if (forwardCommand == null)
                    forwardCommand = new DelegateCommand<string>(
                        delegate(string param)
                        {
                            Mediator.Instance.NotifyColleagues(ViewModelMessages.ContextCommand, 
                                new CommandContext() { CommandName = param, CommandParameter = this.Books.CurrentItem } );
                        },
                        delegate(string param)
                        {
                            if (CatalogData != null && Books.CurrentItem != null)
                            {
                                return true;
                            }
                            return false;
                        });
                return forwardCommand;
            }
        }
        #endregion

        /// <summary>
        /// Command like function called from xaml with trigger
        /// </summary>
        public void NotifyColleagues()
        {
            if (Books != null)
                Mediator.Instance.NotifyColleagues(ViewModelMessages.BookSelected, Books.CurrentItem as Book);
        }

        #endregion

        #region ----------------INTERNALS----------------

        private List<PropertyViewModel> _groupProperties = null;
        private List<PropertyViewModel> GetGroupProperties()
        {
            if (_groupProperties == null)
            {
                _groupProperties = new List<PropertyViewModel>();

                _groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "Type", ToDisplay = "Type of" });
                _groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "Folder", ToDisplay = "Folder" });
                _groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "FileExtension", ToDisplay = "File extension" });
                _groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "Rating", ToDisplay = "Rating" });
                _groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "IsSecured", ToDisplay = "Secured" });
                _groupProperties.Add(new PropertyViewModel() { Prefix = "", Name = "IsRead", ToDisplay = "Read" });

                foreach (string dyn in WorkspaceService.Instance.Settings.Dynamics)
                    _groupProperties.Add(new PropertyViewModel() { Prefix = "Dynamics.", Name = dyn, ToDisplay = dyn });
            }
            return _groupProperties;
        }

        private List<PropertyViewModel> _sortProperties = null;
        private List<PropertyViewModel> GetSortProperties()
        {
            if (_sortProperties == null)
            {
                _sortProperties = new List<PropertyViewModel>();

                foreach (PropertyDescriptor inf in TypeDescriptor.GetProperties(new Book()))
                {
                    if (inf.Attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes))
                    {
                        _sortProperties.Add(
                            new PropertyViewModel()
                            {
                                Prefix = "",
                                Name = inf.Name,
                                ToDisplay = (inf.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute).Description
                            });
                    }
                }

                foreach (string dyn in WorkspaceService.Instance.Settings.Dynamics)
                    _sortProperties.Add(new PropertyViewModel() { Prefix = "Dynamics.", Name = dyn, ToDisplay = dyn });
            }
            return _sortProperties;
        }

        internal void ChangeExplorerView(string modeAsParam)
        {
            if (modeAsParam.Equals("Details"))
                IsExplorerViewThumb = false;
            else
                IsExplorerViewThumb = true;
        }

        internal void Sort(string propertyName)
        {
            PropertyViewModel prop = GetSortProperties().Find(p => p.Name == propertyName);

            IEnumerable<SortDescription> result =
                Books.SortDescriptions.Cast<SortDescription>().Where(p => p.PropertyName == prop.Prefix + prop.Name);

            if (result != null && result.Count() == 1)
            {
                Books.SortDescriptions.Remove(result.First());
            }
            else
            {
                Books.SortDescriptions.Add(new SortDescription(prop.Prefix + prop.Name, ListSortDirection.Ascending));
            }

            RaisePropertyChanged("Books");
        }

        internal void Group(string propertyName)
        {
            PropertyViewModel prop = GetGroupProperties().Find(p => p.Name == propertyName);

            IEnumerable<PropertyGroupDescription> result =
                Books.GroupDescriptions.Cast<PropertyGroupDescription>().Where(p => p.PropertyName == prop.Prefix + prop.Name);

            if (result != null && result.Count() == 1)
            {
                Books.GroupDescriptions.Remove(result.First());
            }
            else
            {
                Books.GroupDescriptions.Add(new PropertyGroupDescription(prop.Prefix + prop.Name));
            }

            Messenger.Default.Send<MessageBase>( new MessageBase(this) );

            RaisePropertyChanged("Books");
        }

        #endregion
    }
}

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 GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions