Click here to Skip to main content
15,894,907 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 125.4K   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.Linq;
using System.Windows.Media.Imaging;
using CBR.Core.Files;
using CBR.Core.Helpers;
using CBR.Core.Models;

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

        public InfoViewModel(Book bk)
		{
            _bookData = bk;

            //register to the mediator for messages
            Mediator.Instance.Register(
                (Object o) =>
                {
                    if (_bookData != o)
                    {
                        _bookData = (Book)o;

                        RaisePropertyChanged("IsEmpty");
                        RaisePropertyChanged("FileName");
                        RaisePropertyChanged("Folder");
                        RaisePropertyChanged("Cover");
                        RaisePropertyChanged("Type");
                        RaisePropertyChanged("Size");
                        RaisePropertyChanged("PageCount");
                        RaisePropertyChanged("IsRead");
                        RaisePropertyChanged("IsSecured");
                        RaisePropertyChanged("Rating");
                        RaisePropertyChanged("KeyValueList");
                    }
                },
                ViewModelMessages.BookSelected);
		}

        private Book _bookData = null;

		#endregion

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

        public bool IsEmpty
        {
            get
            {
                return _bookData == null;
            }
        }

        public string FileName
        {
            get
            {
                if (_bookData != null)
                    return _bookData.FileName;
                else
                    return string.Empty;
            }
        }

        public string Folder
        {
            get
            {
                if (_bookData != null)
                    return _bookData.Folder;
                else
                    return string.Empty;
            }
        }

        public BitmapImage Cover
        {
            get
            {
                if (_bookData != null)
                    return _bookData.Cover;
                else
                    return null;
            }
        }

        public BookType Type
        {
            get
            {
                if (_bookData != null)
                    return _bookData.Type;
                else
                    return BookType.None;
            }
        }

        public long Size
        {
            get
            {
                if (_bookData != null)
                    return _bookData.Size;
                else
                    return 0;
            }
        }

        public int PageCount
        {
            get
            {
                if (_bookData != null)
                    return _bookData.PageCount;
                else
                    return 0;
            }
        }

        public bool IsRead
        {
            get
            {
                if (_bookData != null)
                    return _bookData.IsRead;
                else
                    return false;
            }
        }

        public bool IsSecured
        {
            get
            {
                if (_bookData != null)
                    return _bookData.IsSecured;
                else
                    return false;
            }
        }

        public int Rating
        {
            get
            {
                if (_bookData != null)
                    return _bookData.Rating;
                else return 0;
            }
            set
            {
                if (_bookData.Rating != value)
                    _bookData.Rating = value;
            }
        }

        #endregion

        private List<KeyValueProperty> _KeyValueList = null;
        public List<KeyValueProperty> KeyValueList
        {
            get
            {
                if (_KeyValueList != null)
                {
                    foreach (KeyValueProperty kv in _KeyValueList)
                        kv.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_data_PropertyChanged);
                }

                if (_bookData != null)
                {
                    _KeyValueList = new List<KeyValueProperty>();
                    IDictionary<string, object> dict = _bookData.Dynamics as IDictionary<string, object>;
                    if (dict != null)
                    {
                        foreach (string k in dict.Keys)
                        {
                            KeyValueProperty keyval = new KeyValueProperty(k, dict[k].ToString());
                            keyval.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_data_PropertyChanged);
                            _KeyValueList.Add(keyval);
                        }
                    }

                    return _KeyValueList;
                }
                else
                    return null;
            }
        }

        internal void _data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (_bookData != null)
            {
                KeyValueProperty from = sender as KeyValueProperty;
                KeyValueProperty find = _KeyValueList.Where(p => p.Key == from.Key).First();
                
                IDictionary<string, object> dict = _bookData.Dynamics as IDictionary<string, object>;
                dict[from.Key] = find.Value;

                _bookData.IsDirty = true;
            }
        }
    }
}

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