public async Task <IBook> AddBookAsync(IStorageFile file) { if (file == null) { throw new ArgumentNullException(nameof(file)); } var document = await DjvuDocument.LoadAsync(file); var bookDto = new EfBookDto { PageCount = document.PageCount, Title = Path.GetFileNameWithoutExtension(file.Name), LastOpeningTime = DateTime.Now }; _context.Books.Add(bookDto); await _context.SaveChangesAsync(); var booksFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Books", CreationCollisionOption.OpenIfExists); var bookFile = await file.CopyAsync(booksFolder, $"{bookDto.Id}.djvu", NameCollisionOption.ReplaceExisting); bookDto.BookPath = bookFile.Path; await _context.SaveChangesAsync(); var book = new EfBook(bookDto, _context, _books); await book.UpdateThumbnailAsync(); _books.Add(book); return(book); }
public async Task UpdateThumbnailAsync() { var bookFile = await StorageFile.GetFileFromPathAsync(BookPath); var document = await DjvuDocument.LoadAsync(bookFile); var thumbnailFile = await SaveThumbnail(_efBookDto.Id, document); ThumbnailPath = thumbnailFile.Path; await SaveChangesAsync(); }
public async void OnNavigatedTo(NavigationEventArgs e) { IsProgressVisible = true; _book = e.Parameter as IBook; if (_book != null) { _file = await StorageFile.GetFileFromPathAsync(_book.BookPath); } else if (e.Parameter is IStorageFile) { _file = (IStorageFile)e.Parameter; } else { throw new Exception("Invalid parameter."); } DjvuDocument document; try { document = await DjvuDocument.LoadAsync(_file); } catch { if (Debugger.IsAttached) { throw; } IsProgressVisible = false; ShowFileOpeningError(); return; } CurrentDocument = document; CurrentPageNumber = _book?.LastOpenedPage ?? 1; TotalPageNumber = document.PageCount; if (_book != null) { _book.LastOpeningTime = DateTime.Now; await _book.SaveChangesAsync(); _bookmarks = _book?.Bookmarks; ((INotifyCollectionChanged)_bookmarks).CollectionChanged += Bookmarks_CollectionChanged; UpdateIsCurrentPageBookmarked(); } _outline = await document.GetOutlineAsync(); IsProgressVisible = false; ShowOutlineCommand.RaiseCanExecuteChanged(); AddBookmarkCommand.RaiseCanExecuteChanged(); RemoveBookmarkCommand.RaiseCanExecuteChanged(); ShowBookmarksCommand.RaiseCanExecuteChanged(); var applicationView = ApplicationView.GetForCurrentView(); applicationView.Title = _book?.Title ?? _file.Name; _dataTransferManager.DataRequested += DataRequestedHandler; CoreApplication.Suspending += ApplicationSuspendingHandler; }