diff --git a/Kiwix-iOS/Controller/Bookmark/BookmarkController.swift b/Kiwix-iOS/Controller/Bookmark/BookmarkController.swift index 29467a87..1f25f46f 100644 --- a/Kiwix-iOS/Controller/Bookmark/BookmarkController.swift +++ b/Kiwix-iOS/Controller/Bookmark/BookmarkController.swift @@ -7,29 +7,107 @@ // import UIKit +import CoreData -class BookmarkController: UIViewController { +class BookmarkController: UITableViewController, NSFetchedResultsControllerDelegate { + + var book: Book? + + // MARK: - Overrides override func viewDidLoad() { super.viewDidLoad() - - // Do any additional setup after loading the view. - } - - override func didReceiveMemoryWarning() { - super.didReceiveMemoryWarning() - // Dispose of any resources that can be recreated. } - - /* - // MARK: - Navigation - - // In a storyboard-based application, you will often want to do a little preparation before navigation - override func prepare(for segue: UIStoryboardSegue, sender: Any?) { - // Get the new view controller using segue.destinationViewController. - // Pass the selected object to the new view controller. + // MARK: - Table view data source + + override func numberOfSectionsInTableView(tableView: UITableView) -> Int { + return fetchedResultController.sections?.count ?? 0 + } + + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + guard let sectionInfo = fetchedResultController.sections?[section] else {return 0} + return sectionInfo.numberOfObjects + } + + override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + let article = fetchedResultController.objectAtIndexPath(indexPath) as? Article + let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) + cell.textLabel?.text = article?.title + return cell + } + + func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) { + guard let cell = cell as? BookmarkCell else {return} + guard let article = fetchedResultController.objectAtIndexPath(indexPath) as? Article else {return} + + cell.thumbImageView.image = { + guard let data = article.thumbImageData else {return nil} + return UIImage(data: data) + }() + cell.titleLabel.text = article.title + cell.subtitleLabel.text = article.book?.title + } + + // MARK: - Fetched Result Controller Delegate + + let managedObjectContext = NSManagedObjectContext.mainQueueContext + lazy var fetchedResultController: NSFetchedResultsController = { + let fetchRequest = NSFetchRequest(entityName: "Article") + fetchRequest.sortDescriptors = [NSSortDescriptor(key: "bookmarkDate", ascending: false), + NSSortDescriptor(key: "title", ascending: true)] + fetchRequest.predicate = { + if let book = self.book { + return NSPredicate(format: "book = %@ AND isBookmarked = true", book) + } else { + return NSPredicate(format: "isBookmarked = true") + } + }() + let controller = NSFetchedResultsController(fetchRequest: fetchRequest, + managedObjectContext: self.managedObjectContext, + sectionNameKeyPath: nil, + cacheName: self.book == nil ? nil : "BookmarksFRC" + NSBundle.appShortVersion) + controller.delegate = self + controller.performFetch(deleteCache: false) + return controller + }() + + // MARK: - Fetched Result Controller Delegate + + func controllerWillChangeContent(controller: NSFetchedResultsController) { + tableView.beginUpdates() + } + + func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { + switch type { + case .Insert: + tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) + case .Delete: + tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade) + default: + return + } + } + + func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { + switch type { + case .Insert: + guard let newIndexPath = newIndexPath else {return} + tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) + case .Delete: + guard let indexPath = indexPath else {return} + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) + case .Update: + guard let indexPath = indexPath, let cell = tableView.cellForRowAtIndexPath(indexPath) else {return} + configureCell(cell, atIndexPath: indexPath) + case .Move: + guard let indexPath = indexPath, let newIndexPath = newIndexPath else {return} + tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) + tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Fade) + } + } + + func controllerDidChangeContent(controller: NSFetchedResultsController) { + tableView.endUpdates() } - */ - } diff --git a/Kiwix-iOS/Controller/Library/BookDetailController.swift b/Kiwix-iOS/Controller/Library/BookDetailController.swift index 26c295a1..7c0313b8 100644 --- a/Kiwix-iOS/Controller/Library/BookDetailController.swift +++ b/Kiwix-iOS/Controller/Library/BookDetailController.swift @@ -256,6 +256,11 @@ class BookDetailController: UITableViewController, DZNEmptyDataSetSource, DZNEmp case LocalizedStrings.remove: let operation = RemoveBookConfirmationAlert(context: self, bookID: book.id) GlobalQueue.shared.addOperation(operation) + case LocalizedStrings.bookmarks: + guard let controller = UIStoryboard(name: "Bookmark", bundle: nil) + .instantiateViewControllerWithIdentifier("BookmarkController") as? BookmarkController else {return} + controller.book = book + navigationController?.pushViewController(controller, animated: true) case LocalizedStrings.copyURL: guard let url = book.url else {return} UIPasteboard.generalPasteboard().string = url.absoluteString diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index f8ed55a8..17c40b19 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.1185 + 1.8.1211 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOS/Storyboard/Bookmark.storyboard b/Kiwix-iOS/Storyboard/Bookmark.storyboard index 41040d97..c0edf51e 100644 --- a/Kiwix-iOS/Storyboard/Bookmark.storyboard +++ b/Kiwix-iOS/Storyboard/Bookmark.storyboard @@ -1,5 +1,5 @@ - - + + @@ -19,10 +19,10 @@ - + - + - + - +