File size: 4,115 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Collections.ObjectModel;
using System.Windows.Input;
using VersOne.Epub.WpfDemo.Models;
using VersOne.Epub.WpfDemo.Utils;
using VersOne.Epub.WpfDemo.WpfEnvironment;

namespace VersOne.Epub.WpfDemo.ViewModels
{
    /// <summary>
    /// View model for the <see cref="Views.LibraryView" />.
    /// </summary>
    public class LibraryViewModel : ViewModel
    {
        private readonly IWindowManager windowManager;
        private readonly LibraryModel libraryModel;

        private Command addBookCommand;
        private Command removeFromLibraryCommand;
        private Command openBookCommand;

        /// <summary>
        /// Initializes a new instance of the <see cref="LibraryViewModel" /> class.
        /// </summary>
        public LibraryViewModel()
        {
            windowManager = WindowManager.Instance;
            libraryModel = new LibraryModel();
            addBookCommand = null;
            removeFromLibraryCommand = null;
            openBookCommand = null;
            RefreshLibrary();
        }

        /// <summary>
        /// Gets a collection of library items (i.e. books shown in the library view).
        /// </summary>
        public ObservableCollection<LibraryItemViewModel> Books { get; private set; }

        /// <summary>
        /// Gets the command which is executed when the user clicks on the 'Add book' button in the library view.
        /// </summary>
        public ICommand AddBookCommand
        {
            get
            {
                if (addBookCommand == null)
                {
                    addBookCommand = new Command(AddBook);
                }
                return addBookCommand;
            }
        }

        /// <summary>
        /// Gets the command which is executed when the user clicks on the 'Remove from library' context menu item of a book in the library view.
        /// </summary>
        public ICommand RemoveFromLibraryCommand
        {
            get
            {
                if (removeFromLibraryCommand == null)
                {
                    removeFromLibraryCommand = new Command(param => RemoveBookFromLibrary(param as LibraryItemViewModel));
                }
                return removeFromLibraryCommand;
            }
        }

        /// <summary>
        /// Gets the command which is executed when the user clicks on a book in the library view.
        /// </summary>
        public ICommand OpenBookCommand
        {
            get
            {
                if (openBookCommand == null)
                {
                    openBookCommand = new Command(param => OpenBook(param as LibraryItemViewModel));
                }
                return openBookCommand;
            }
        }

        private void RefreshLibrary()
        {
            Books = new ObservableCollection<LibraryItemViewModel>(libraryModel.GetLibraryItems());
            NotifyPropertyChanged(nameof(Books));
        }

        private void AddBook()
        {
            OpenFileDialogParameters openFileDialogParameters = new OpenFileDialogParameters
            {
                Filter = "EPUB files (*.epub)|*.epub|All files (*.*)|*.*",
                Multiselect = true
            };
            OpenFileDialogResult openDialogResult = windowManager.ShowOpenFileDialog(openFileDialogParameters);
            if (openDialogResult.DialogResult)
            {
                foreach (string selectedFilePath in openDialogResult.SelectedFilePaths)
                {
                    libraryModel.AddBookToLibrary(selectedFilePath);
                }
                RefreshLibrary();
            }
        }

        private void RemoveBookFromLibrary(LibraryItemViewModel book)
        {
            libraryModel.RemoveBookFromLibrary(book);
            RefreshLibrary();
        }

        private void OpenBook(LibraryItemViewModel book)
        {
            BookViewModel bookViewModel = new BookViewModel(book.Id);
            IWindowContext bookWindowContext = windowManager.CreateWindow(bookViewModel);
            bookWindowContext.ShowDialog();
        }
    }
}