private void UpdateCollectionView(LibraryBookView bookView)
        {
            if (dataSource != null && dataSource.BookList != null && dataSource.BookList.Count > 0)
            {
                try
                {
                    bookView.RemoveFromSuperview();

                    Book book = bookView.LibraryBook.Copy();
                    book.Status = Book.BookStatus.PENDING2DOWNLOAD;

                    // Add the book to the device
                    BooksOnDeviceAccessor.AddBook(book);

                    // Start the download
                    BookUpdater.CheckBooks2Download();

                    // Update available badge
                    UpdateAvailableBadge();

                    // Remove from the list
                    List <Book> newBookList = new List <Book>(dataSource.BookList);
                    int         removeIdx   = dataSource.GetBookIndex(book.ID);
                    newBookList.RemoveAt(removeIdx);

                    collectionView.PerformBatchUpdates(delegate
                    {
                        foreach (Book b in dataSource.BookList)
                        {
                            if (b.ID != book.ID)
                            {
                                NSIndexPath fromIndexPath = dataSource.GetIndexPath(b.ID);
                                int toRow = newBookList.IndexOf(b);
                                if (fromIndexPath != null && fromIndexPath.Row >= 0 && toRow >= 0)
                                {
                                    NSIndexPath toIndexPath = NSIndexPath.FromRowSection(toRow, 0);
                                    collectionView.MoveItem(fromIndexPath, toIndexPath);
                                }
                            }
                        }
                    }, delegate
                    {
                        downloadAnimation = false;

                        RefreshTable();
                    });
                }
                catch (Exception ex)
                {
                    Logger.WriteLineDebugging("LibraryViewController - UpdateCollectionView: {0}", ex.ToString());
                }
            }
        }
Example #2
0
        static void HandleGetMyBooksEvent(List <Book> sBooks)
        {
            try
            {
                SaveMyStuff.GetMyBooksEvent -= HandleGetMyBooksEvent;

                if (sBooks == null)
                {
                    receiveBookmarks = receiveNotes = receiveAnnotations = true;

                    CheckReceiveDone();
                }
                else
                {
                    List <Book> dBooks = BooksOnDeviceAccessor.GetBooks();
                    if (dBooks != null && dBooks.Count > 0)
                    {
                        foreach (Book dBook in dBooks)
                        {
                            if (sBooks != null && sBooks.Count > 0)
                            {
                                foreach (Book sBook in sBooks)
                                {
                                    if (dBook.ID == sBook.ID)
                                    {
                                        if (dBook.UserModifiedDate < sBook.UserModifiedDate)
                                        {
                                            if (sBook.Removed)
                                            {
                                                // Remove book if book on the cloud has 'Removed' checked
                                                BookRemover.RemoveBook(dBook);
                                            }
                                            else
                                            {
                                                // Update book if book on the cloud has the latest ModifiedUtc
                                                dBook.Version          = sBook.Version;
                                                dBook.IsFavorite       = sBook.IsFavorite;
                                                dBook.UserModifiedDate = sBook.UserModifiedDate;
                                                dBook.UserAddedDate    = DateTime.UtcNow;
                                                dBook.New = true;

                                                BooksOnDeviceAccessor.UpdateBook(dBook);
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    // Add book if the book is not on the device
                    if (sBooks != null && sBooks.Count > 0)
                    {
                        foreach (Book sBook in sBooks)
                        {
                            if (!sBook.Removed)
                            {
                                if (BooksOnDeviceAccessor.GetBook(sBook.ID) == null)
                                {
                                    BooksOnDeviceAccessor.AddBook(sBook);
                                }
                            }
                        }
                    }

                    // Add book details for new books
                    BookDownloader bd = new BookDownloader();
                    bd.DownloadedEvent += (List <Book> bookList) =>
                    {
                        if (bookList != null)
                        {
                            dBooks = BooksOnDeviceAccessor.GetBooks();
                            if (dBooks != null)
                            {
                                foreach (Book dBook in dBooks)
                                {
                                    if (dBook.Status == Book.BookStatus.NONE)
                                    {
                                        foreach (Book nBook in bookList)
                                        {
                                            if (dBook.ID == nBook.ID)
                                            {
                                                dBook.Title              = nBook.Title;
                                                dBook.Description        = nBook.Description;
                                                dBook.ChapterCount       = nBook.ChapterCount;
                                                dBook.PageCount          = nBook.PageCount;
                                                dBook.SmallImageURL      = nBook.SmallImageURL;
                                                dBook.LargeImageURL      = nBook.LargeImageURL;
                                                dBook.ImageVersion       = nBook.ImageVersion;
                                                dBook.ServerAddedDate    = nBook.ServerAddedDate;
                                                dBook.ServerModifiedDate = nBook.ServerModifiedDate;
                                                dBook.UserAddedDate      = nBook.UserAddedDate;
                                                dBook.UserModifiedDate   = nBook.UserModifiedDate;
                                                dBook.Status             = Book.BookStatus.PENDING2DOWNLOAD;
                                                dBook.Removed            = false;
                                                dBook.New    = true;
                                                dBook.Viewed = false;

                                                BooksOnDeviceAccessor.UpdateBook(dBook);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (currentSyncType == SyncType.PULL)
                        {
                            receiveBookmarks = receiveNotes = receiveAnnotations = true;

                            CheckReceiveDone();
                        }
                        else
                        {
                            // Receive Bookmarks, Notes, and Annotations
                            if (sBooks != null)
                            {
                                SaveMyStuff.GetMyBookmarksEvent   += HandleGetMyBookmarksEvent;
                                SaveMyStuff.GetMyNotesEvent       += HandleGetMyNotesEvent;
                                SaveMyStuff.GetMyAnnotationsEvent += HandleGetMyAnnotationsEvent;

                                for (Int32 i = 0; i < sBooks.Count; i++)
                                {
                                    bool lastItem = false;
                                    if (i == sBooks.Count - 1)
                                    {
                                        lastItem = true;
                                    }

                                    SaveMyStuff.GetMyBookmarks(sBooks[i].ID, lastItem);
                                    SaveMyStuff.GetMyNotes(sBooks[i].ID, lastItem);
                                    SaveMyStuff.GetMyAnnotations(sBooks[i].ID, lastItem);
                                }
                            }
                        }
                    };
                    bd.StartDownload();
                }
            }
            catch (Exception ex)
            {
                SetReceive(true);

                CheckReceiveDone();

                Logger.WriteLineDebugging("CloudSync - HandleGetMyBooksEvent: {0}", ex.ToString());
            }
        }
Example #3
0
        private static void PullMyBooksWork()
        {
            try
            {
                List <Book> sBooks = SaveMyStuff.GetMyBooks();
                if (sBooks != null && sBooks.Count > 0)
                {
                    foreach (var sBook in sBooks)
                    {
                        Book dBook = BooksOnDeviceAccessor.GetBook(sBook.ID);
                        if (dBook == null)
                        {
                            // This is a new book from the server
                            BooksOnDeviceAccessor.AddBook(sBook);
                        }
                        else
                        {
                            if (dBook.ServerModifiedDate <= sBook.ServerModifiedDate)
                            {
                                if (sBook.Removed)
                                {
                                    // Remove bookmark if the bookmark on the cloud has 'Removed' checked
                                    BookRemover.RemoveBook(sBook);
                                }
                                else
                                {
                                    sBook.UserAddedDate = DateTime.UtcNow;
                                    sBook.New           = true;

                                    BooksOnDeviceAccessor.UpdateBook(sBook);
                                }
                            }
                        }
                    }
                }

                // Download detail information about these books
                List <Book> bookList = eBriefingService.StartDownloadBooks();
                if (bookList != null && bookList.Count > 0)
                {
                    List <Book> dBooks = BooksOnDeviceAccessor.GetBooks();
                    if (dBooks != null && dBooks.Count > 0)
                    {
                        foreach (var book in bookList)
                        {
                            var item = dBooks.Where(i => i.ID == book.ID && i.Status == Book.BookStatus.NONE).FirstOrDefault();
                            if (item != null)
                            {
                                book.Status = Book.BookStatus.PENDING2DOWNLOAD;
                                book.New    = true;
                                BooksOnDeviceAccessor.UpdateBook(book);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLineDebugging("CloudSync - PullMyBooksWork: {0}", ex.ToString());
            }
        }
Example #4
0
        private static void UpdateDatabase(Book book)
        {
            try
            {
                List <Chapter> chapterList = BooksOnServerAccessor.GetChapters(book.ID);
                List <Page>    pageList    = BooksOnServerAccessor.GetPages(book.ID);

                // Update notes
                if (BooksOnDeviceAccessor.GetNotes(book.ID) != null)
                {
                    // Remove orphans
                    BooksOnDeviceAccessor.RemoveOrphanNotes(book.ID, pageList);
                }

                // Update bookmarks
                if (BooksOnDeviceAccessor.GetBookmarks(book.ID) != null)
                {
                    // Remove orphans
                    BooksOnDeviceAccessor.RemoveOrphanBookmarks(book.ID, pageList);
                }

                // Update new chapters
                if (BooksOnDeviceAccessor.GetChapters(book.ID) == null)
                {
                    BooksOnDeviceAccessor.AddChapters(book.ID, chapterList);
                }
                else
                {
                    BooksOnDeviceAccessor.UpdateChapters(book.ID, chapterList);
                }

                BooksOnDeviceAccessor.MapPagesToChapter(chapterList, pageList); // ML: 4/9/2013 We need to map them after each update

                // Update new pages
                if (BooksOnDeviceAccessor.GetPages(book.ID) == null)
                {
                    BooksOnDeviceAccessor.AddPages(book.ID, pageList);
                }
                else
                {
                    BooksOnDeviceAccessor.UpdatePages(book.ID, pageList);
                }

                // Remove chapters and pages list from BooksOnServer because they now exist on the device
                BooksOnServerAccessor.RemoveChapters(book.ID);
                BooksOnServerAccessor.RemovePages(book.ID);

                // Update new book
                book.New           = true;
                book.Status        = Book.BookStatus.DOWNLOADED;
                book.UserAddedDate = DateTime.UtcNow;

                if (BooksOnDeviceAccessor.GetBook(book.ID) == null)
                {
                    BooksOnDeviceAccessor.AddBook(book);
                }
                else
                {
                    if (BooksDataAccessor.IsFavorite(book.ID))
                    {
                        book.IsFavorite = true;
                    }
                    BooksOnDeviceAccessor.UpdateBook(book);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLineDebugging("BookUpdater - UpdateDatabase: {0}", ex.ToString());
            }
        }