Click here to Skip to main content
15,884,298 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.1K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using CBR.Core.Helpers;
using CBR.Core.Models;
using CBR.Core.Services;
using System.IO;

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

        public RecentFileViewModel()
		{
			//register to the mediator for messages
			Mediator.Instance.RegisterHandler(ViewModelMessages.RecentFileChanged,
				(Object o) =>
				{
                    foreach (RecentFileInfoViewModel cat in Catalogs)
                        if (cat.Data == ((RecentFileInfo)o) ) Catalogs = null;

                    foreach( RecentFileInfoViewModel bk in Books )
                        if (bk.Data == ((RecentFileInfo)o)) Books = null;

				} );

            Mediator.Instance.RegisterHandler(ViewModelMessages.RecentListChanged,
                (Object o) =>
                {
                    if (WorkspaceService.Instance.Settings.RecentCatalogList == (List<RecentFileInfo>)o)
                        Catalogs = null;

                    if (WorkspaceService.Instance.Settings.RecentFileList == (List<RecentFileInfo>)o)
                        Books = null;

                } );
		}
        #endregion

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

        private ObservableCollection<RecentFileInfoViewModel> _Catalogs = null;
        /// <summary>
        /// Book object list in the catalog
        /// </summary>
        public ObservableCollection<RecentFileInfoViewModel> Catalogs
        {
            get
            {
                if (_Catalogs == null)
                {
                    _Catalogs = new ObservableCollection<RecentFileInfoViewModel>();
                    List<RecentFileInfo> temp = new List<RecentFileInfo>();

                    foreach (RecentFileInfo rfi in WorkspaceService.Instance.Settings.RecentCatalogList)
                    {
                        if (File.Exists(Path.Combine(rfi.FilePath, rfi.FileName)))
                            _Catalogs.Add(new RecentFileInfoViewModel(rfi));
                        else
                            temp.Add(rfi);
                    }

                    foreach (RecentFileInfo rfi in temp)
                        WorkspaceService.Instance.Settings.RecentCatalogList.Remove(rfi);
                }
                return _Catalogs;
            }
            set
            {
                _Catalogs = value;
                RaisePropertyChanged("RecentCatalogs");
            }
        }

        public ICollectionView RecentCatalogs
        {
            get
            {
                if (Catalogs != null)
                {
                    ICollectionView view = CollectionViewSource.GetDefaultView(Catalogs);
                    if (view.SortDescriptions.Count == 0)
                    {
                        view.GroupDescriptions.Add(new PropertyGroupDescription("IsPined"));
                        view.SortDescriptions.Add(new SortDescription("IsPined", ListSortDirection.Descending));
                        view.SortDescriptions.Add(new SortDescription("FileName", ListSortDirection.Ascending));
                    }
                    return view;
                }
                else
                    return null;
            }
        }

        private ObservableCollection<RecentFileInfoViewModel> _Books = null;
        /// <summary>
        /// Book object list in the catalog
        /// </summary>
        public ObservableCollection<RecentFileInfoViewModel> Books
        {
            get
            {
                if (_Books == null)
                {
                    _Books = new ObservableCollection<RecentFileInfoViewModel>();
                    List<RecentFileInfo> temp = new List<RecentFileInfo>();

                    foreach (RecentFileInfo rfi in WorkspaceService.Instance.Settings.RecentFileList)
                    {
                        if (File.Exists(Path.Combine(rfi.FilePath, rfi.FileName)))
                            _Books.Add(new RecentFileInfoViewModel(rfi));
                        else
                            temp.Add(rfi);
                    }

                    foreach (RecentFileInfo rfi in temp)
                        WorkspaceService.Instance.Settings.RecentFileList.Remove(rfi);

                }
                return _Books;
            }
            set
            {
                _Books = value;
                RaisePropertyChanged("RecentBooks");
            }
        }

        public ICollectionView RecentBooks
        {
            get
            {
                if (Books != null)
                {
                    ICollectionView view = CollectionViewSource.GetDefaultView(Books);
                    if (view.SortDescriptions.Count == 0)
                    {
                        view.GroupDescriptions.Add(new PropertyGroupDescription("IsPined"));
                        view.SortDescriptions.Add(new SortDescription("IsPined", ListSortDirection.Descending));
                        view.SortDescriptions.Add(new SortDescription("FileName", ListSortDirection.Ascending));
                    }
                    return view;
                }
                else
                    return null;
            }
        }

        #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