using System.Collections.ObjectModel; using System.IO.Compression; using System.Linq; using System.Threading.Tasks; using System.Windows.Input; using VersOne.Epub.WpfDemo.Models; using VersOne.Epub.WpfDemo.Utils; namespace VersOne.Epub.WpfDemo.ViewModels { /// /// View model for the . /// public class BookViewModel : ViewModel { private readonly BookModel bookModel; private bool isLoading; private ObservableCollection navigation; private ObservableCollection readingOrder; private ZipArchive currentEpubArchive; private HtmlContentFileViewModel currentHtmlContentFile; private HtmlContentFileViewModel previousHtmlContentFile; private HtmlContentFileViewModel nextHtmlContentFile; private string currentAnchor; private Command navigateCommand; private Command previousCommand; private Command nextCommand; /// /// Initializes a new instance of the class. /// /// The unique identifier of the book which should contain the same value as . public BookViewModel(int bookId) { bookModel = new BookModel(); isLoading = true; currentEpubArchive = null; currentHtmlContentFile = null; previousHtmlContentFile = null; nextHtmlContentFile = null; currentAnchor = null; navigateCommand = null; previousCommand = null; nextCommand = null; bookModel.OpenBookAsync(bookId).ContinueWith(epubBook => BookOpened(epubBook), TaskScheduler.FromCurrentSynchronizationContext()); } /// /// Gets a collection of navigation items which are used to render the navigation tree in the . /// public ObservableCollection Navigation { get { return navigation; } private set { navigation = value; NotifyPropertyChanged(); } } /// /// Gets a collection of HTML content files which determine the order in which they are rendered in the . /// public ObservableCollection ReadingOrder { get { return readingOrder; } private set { readingOrder = value; NotifyPropertyChanged(); } } /// /// Gets a value indicating whether the book is still loading or not. /// public bool IsLoading { get { return isLoading; } private set { isLoading = value; NotifyPropertyChanged(); } } /// /// Gets or sets the reference to the EPUB book file currently opened in the . /// public ZipArchive CurrentEpubArchive { get { return currentEpubArchive; } set { currentEpubArchive = value; NotifyPropertyChanged(); } } /// /// Gets or sets the reference to a view model of the HTML content file from currently rendered in the . /// public HtmlContentFileViewModel CurrentHtmlContentFile { get { return currentHtmlContentFile; } set { currentHtmlContentFile = value; NotifyPropertyChanged(); } } /// /// Gets a value indicating whether the button to navigate to the previous content file in the reading order should be visible or not. /// public bool IsPreviousButtonVisible { get { return previousHtmlContentFile != null; } } /// /// Gets a value indicating whether the button to navigate to the next content file in the reading order should be visible or not. /// public bool IsNextButtonVisible { get { return nextHtmlContentFile != null; } } /// /// Gets or sets the name of the anchor in the which is used to determine the place /// where the content should be scrolled to once it is fully loaded. /// public string CurrentAnchor { get { return currentAnchor; } set { currentAnchor = value; NotifyPropertyChanged(); } } /// /// Gets the command which is executed when the user clicks on a navigation link in the navigation tree in the . /// public ICommand NavigateCommand { get { if (navigateCommand == null) { navigateCommand = new Command(param => Navigate(param as NavigationItemViewModel)); } return navigateCommand; } } /// /// Gets the command which is executed when the user clicks on the '< PREVIOUS' button in the . /// public ICommand PreviousCommand { get { if (previousCommand == null) { previousCommand = new Command(NavigateToPreviousItemInReadingOrder); } return previousCommand; } } /// /// Gets the command which is executed when the user clicks on the 'NEXT >' button in the . /// public ICommand NextCommand { get { if (nextCommand == null) { nextCommand = new Command(NavigateToNextItemInReadingOrder); } return nextCommand; } } private void BookOpened(Task task) { EpubBook epubBook = task.Result; currentEpubArchive?.Dispose(); CurrentEpubArchive = ZipFile.OpenRead(epubBook.FilePath); Navigation = new ObservableCollection(bookModel.GetNavigation(epubBook)); ReadingOrder = new ObservableCollection(bookModel.GetReadingOrder(epubBook)); if (ReadingOrder.Any()) { CurrentHtmlContentFile = ReadingOrder[0]; if (ReadingOrder.Count > 1) { nextHtmlContentFile = ReadingOrder[1]; } } IsLoading = false; NotifyPropertyChanged(nameof(IsPreviousButtonVisible)); NotifyPropertyChanged(nameof(IsNextButtonVisible)); } private void Navigate(NavigationItemViewModel navigationItemViewModel) { navigationItemViewModel.IsTreeItemExpanded = true; if (navigationItemViewModel.IsLink) { Navigate(ReadingOrder.FirstOrDefault(htmlContentFile => htmlContentFile.HtmlFilePathInEpubArchive == navigationItemViewModel.FilePathInEpubArchive)); CurrentAnchor = navigationItemViewModel.Anchor; } } private void Navigate(HtmlContentFileViewModel targetHtmlContentFileViewModel) { if (targetHtmlContentFileViewModel == null) { CurrentHtmlContentFile = null; previousHtmlContentFile = null; nextHtmlContentFile = null; } else if (CurrentHtmlContentFile != targetHtmlContentFileViewModel) { CurrentHtmlContentFile = targetHtmlContentFileViewModel; int currentReadingOrderItemIndex = ReadingOrder.IndexOf(CurrentHtmlContentFile); if (currentReadingOrderItemIndex != -1) { if (currentReadingOrderItemIndex > 0) { previousHtmlContentFile = ReadingOrder[currentReadingOrderItemIndex - 1]; } else { previousHtmlContentFile = null; } if (currentReadingOrderItemIndex < ReadingOrder.Count - 1) { nextHtmlContentFile = ReadingOrder[currentReadingOrderItemIndex + 1]; } else { nextHtmlContentFile = null; } } else { previousHtmlContentFile = null; nextHtmlContentFile = null; } } NotifyPropertyChanged(nameof(IsPreviousButtonVisible)); NotifyPropertyChanged(nameof(IsNextButtonVisible)); } private void NavigateToPreviousItemInReadingOrder() { if (previousHtmlContentFile != null) { Navigate(previousHtmlContentFile); } } private void NavigateToNextItemInReadingOrder() { if (nextHtmlContentFile != null) { Navigate(nextHtmlContentFile); } } } }