コード例 #1
0
        /// <summary>
        /// This method is looking for a books, adds books to the main page if they are not contains in the database,
        /// deletes books if they don't exist on the user's device. It helps to hold actual data.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">An object that contains the event data.</param>
        private async void OnClickSearchBooksButton(object sender, EventArgs args)
        {
            IFiler filer = DependencyService.Get <IFiler>();
            IEnumerable <string> pathsOfFoundFiles = await filer.GetFilesPathsAsync(FileExtension.EPUB).ConfigureAwait(false);

            List <EpubBook> epubBooks = new List <EpubBook>();

            foreach (var path in pathsOfFoundFiles)
            {
                EpubBook book = await EpubReader.EpubReader.ReadBookAsync(path).ConfigureAwait(false);

                epubBooks.Add(book);
            }

            List <string> pathsOfExistingFiles = this.bookEntities.Select(entity => entity.FilePath).ToList();

            // Try to read not all book information.
            // I need to read only a necessary information e.g. Title, Author, Cover.
            foreach (EpubBook epubBook in epubBooks)
            {
                // If the book entity does not exist.
                // Add a new book info to the main page.
                if (pathsOfExistingFiles.Contains(epubBook.FilePath) == false)
                {
                    BookEntity bookEntity = new BookEntity
                    {
                        Id     = Guid.NewGuid().ToNonDashedString(),
                        Title  = epubBook.Title,
                        Author = epubBook.Author,
                        // It should be changed.
                        // An image might be missed.
                        Cover    = epubBook.Content.Images.FirstOrDefault().Value.Content,
                        FilePath = epubBook.FilePath
                    };

                    SettingsEntity settingsEntity = new SettingsEntity
                    {
                        BookId   = bookEntity.Id,
                        LastPage = 1,
                        FontSize = 14
                    };

                    SQLiteResult result = this.bookRepository.Add(bookEntity);
                    SQLiteResult settingsInsertResult = this.settingsRepository.Add(settingsEntity);

                    // 0 is SQLITE_OK
                    // But returns 1 and entity is successfully saved into database.
                    //if (bookInsertStatusCode == 1)
                    //{
                    this.bookEntities.Add(bookEntity);
                    BookInfoViewModel model = bookEntity.ToBookInfoModelMapper();
                    this.books.Add(model);
                    //}
                }
            }

            // Delete book info models and book entities which do not exist yet.
            foreach (var pathOfExistingFile in pathsOfExistingFiles)
            {
                if (pathsOfFoundFiles.Contains(pathOfExistingFile) == false)
                {
                    // Delete entity.
                    BookEntity deletedBookEntity = this.bookEntities.FirstOrDefault(e => e.FilePath == pathOfExistingFile);
                    this.bookRepository.DeleteById(deletedBookEntity.Id);
                    this.bookEntities.Remove(deletedBookEntity);

                    // Delete book info view model.
                    BookInfoViewModel deletedBookInfoViewModel = this.books.FirstOrDefault(m => m.FilePath == pathOfExistingFile);
                    this.books.Remove(deletedBookInfoViewModel);
                }
            }

            Xamarin.Forms.Device.BeginInvokeOnMainThread(() => this.UpdateBookLibrary(this.books));
        }