From b4226ff9d163525f0860fca359686aee297bfdc6 Mon Sep 17 00:00:00 2001 From: Chris Li Date: Thu, 15 Sep 2016 14:47:08 -0400 Subject: [PATCH] Remove Book operation --- Kiwix-iOS/Controller/Alerts.swift | 15 +++++++++++++++ .../Library/BookDetailController.swift | 3 +++ .../Library/LocalBooksController.swift | 19 ++++--------------- Kiwix-iOS/Info.plist | 2 +- Kiwix-iOSWidgets/Bookmarks/Info.plist | 2 +- Kiwix/Operations/BookOperation.swift | 17 ++++++++++++++++- Kiwix/Operations/ScanLocalBookOperation.swift | 1 - Kiwix/libkiwix/ZimReader.h | 3 ++- Kiwix/libkiwix/ZimReader.mm | 4 ++-- 9 files changed, 44 insertions(+), 22 deletions(-) diff --git a/Kiwix-iOS/Controller/Alerts.swift b/Kiwix-iOS/Controller/Alerts.swift index c044f2c9..a1f3509a 100644 --- a/Kiwix-iOS/Controller/Alerts.swift +++ b/Kiwix-iOS/Controller/Alerts.swift @@ -33,3 +33,18 @@ class SpaceCautionAlert: AlertOperation { preferredAction = actions[0] } } + +class RemoveBookConfirmationAlert: AlertOperation { + init(context: UIViewController, bookID: String) { + super.init(presentAlertFrom: context) + + title = NSLocalizedString("Remove this book?", comment: "Library, Delete Alert") + message = NSLocalizedString("This operation is not recoverable. All bookmarks in this book will also be removed!", comment: "Library, Delete Alert") + addActionWithTitle(LocalizedStrings.remove, style: .Destructive) { _ in + let operation = RemoveBookOperation(bookID: bookID) + GlobalQueue.shared.addOperation(operation) + } + addActionWithTitle(LocalizedStrings.cancel) + preferredAction = actions[0] + } +} diff --git a/Kiwix-iOS/Controller/Library/BookDetailController.swift b/Kiwix-iOS/Controller/Library/BookDetailController.swift index 23bbf830..08da0b98 100644 --- a/Kiwix-iOS/Controller/Library/BookDetailController.swift +++ b/Kiwix-iOS/Controller/Library/BookDetailController.swift @@ -222,6 +222,9 @@ class BookDetailController: UITableViewController, DZNEmptyDataSetSource, DZNEmp guard let download = DownloadBookOperation(bookID: book.id) else {return} Network.shared.queue.addOperation(download) } + case Strings.remove: + let operation = RemoveBookConfirmationAlert(context: self, bookID: book.id) + GlobalQueue.shared.addOperation(operation) case Strings.copyURL: guard let url = book.url else {return} UIPasteboard.generalPasteboard().string = url.absoluteString diff --git a/Kiwix-iOS/Controller/Library/LocalBooksController.swift b/Kiwix-iOS/Controller/Library/LocalBooksController.swift index 27f107d0..cadffb62 100644 --- a/Kiwix-iOS/Controller/Library/LocalBooksController.swift +++ b/Kiwix-iOS/Controller/Library/LocalBooksController.swift @@ -129,22 +129,11 @@ class LocalBooksController: UIViewController, UITableViewDelegate, UITableViewDa func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { - let delete = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.delete) { (action, indexPath) -> Void in + let delete = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.remove) { (action, indexPath) -> Void in guard let book = self.fetchedResultController.objectAtIndexPath(indexPath) as? Book else {return} - self.managedObjectContext.performBlock({ () -> Void in - if let zimURL = ZimMultiReader.shared.readers[book.id]?.fileURL { - FileManager.removeItem(atURL: zimURL) - - let indexFolderURL = zimURL.URLByAppendingPathExtension("idx") - FileManager.removeItem(atURL: indexFolderURL!) - } - - if let _ = book.url { - book.isLocal = false - } else { - self.managedObjectContext.deleteObject(book) - } - }) + let operation = RemoveBookConfirmationAlert(context: self, bookID: book.id) + GlobalQueue.shared.addOperation(operation) + self.tableView.setEditing(false, animated: true) } return [delete] } diff --git a/Kiwix-iOS/Info.plist b/Kiwix-iOS/Info.plist index 60fdf892..28010753 100644 --- a/Kiwix-iOS/Info.plist +++ b/Kiwix-iOS/Info.plist @@ -49,7 +49,7 @@ CFBundleVersion - 1.8.580 + 1.8.622 ITSAppUsesNonExemptEncryption LSRequiresIPhoneOS diff --git a/Kiwix-iOSWidgets/Bookmarks/Info.plist b/Kiwix-iOSWidgets/Bookmarks/Info.plist index ec95c813..db0fe6bf 100644 --- a/Kiwix-iOSWidgets/Bookmarks/Info.plist +++ b/Kiwix-iOSWidgets/Bookmarks/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.8.583 + 1.8.625 NSExtension NSExtensionMainStoryboard diff --git a/Kiwix/Operations/BookOperation.swift b/Kiwix/Operations/BookOperation.swift index 9d6ef6b2..fcb69b2e 100644 --- a/Kiwix/Operations/BookOperation.swift +++ b/Kiwix/Operations/BookOperation.swift @@ -46,7 +46,7 @@ class DownloadBookOperation: URLSessionDownloadTaskOperation { } } -class DeleteBookOperation: Operation { +class RemoveBookOperation: Operation { let bookID: String @@ -55,6 +55,21 @@ class DeleteBookOperation: Operation { super.init() } + override func execute() { + let context = NSManagedObjectContext.mainQueueContext + context.performBlockAndWait { + guard let zimFileURL = ZimMultiReader.shared.readers[self.bookID]?.fileURL else {return} + _ = try? NSFileManager.defaultManager().removeItemAtURL(zimFileURL) + + // Core data is updated by scan book operation + // Article removal is handled by cascade relationship + + guard let idxFolderURL = ZimMultiReader.shared.readers[self.bookID]?.idxFolderURL else {return} + _ = try? NSFileManager.defaultManager().removeItemAtURL(idxFolderURL) + } + finish() + } + } diff --git a/Kiwix/Operations/ScanLocalBookOperation.swift b/Kiwix/Operations/ScanLocalBookOperation.swift index 2d82b9c3..82fb2ddf 100644 --- a/Kiwix/Operations/ScanLocalBookOperation.swift +++ b/Kiwix/Operations/ScanLocalBookOperation.swift @@ -74,7 +74,6 @@ class ScanLocalBookOperation: Operation { for id in removedZimFileIDs { guard let book = localBooks[id] else {continue} if let _ = book.meta4URL { - print(book.isLocal) book.isLocal = false } else { context.deleteObject(book) diff --git a/Kiwix/libkiwix/ZimReader.h b/Kiwix/libkiwix/ZimReader.h index d0081936..588f52fe 100755 --- a/Kiwix/libkiwix/ZimReader.h +++ b/Kiwix/libkiwix/ZimReader.h @@ -12,7 +12,8 @@ @interface ZimReader : NSObject - (instancetype)initWithZIMFileURL:(NSURL *)url; -@property NSURL *fileURL; +@property (strong, nonatomic) NSURL *fileURL; +@property (strong, nonatomic) NSURL *idxFolderURL; #pragma mark - index - (BOOL)hasIndex; diff --git a/Kiwix/libkiwix/ZimReader.mm b/Kiwix/libkiwix/ZimReader.mm index 8ac820ec..e09f4059 100755 --- a/Kiwix/libkiwix/ZimReader.mm +++ b/Kiwix/libkiwix/ZimReader.mm @@ -46,8 +46,8 @@ try { NSString *zimPath = [url absoluteString]; zimPath = [zimPath stringByReplacingOccurrencesOfString:@".zimaa" withString:@".zim"]; - NSURL *xapianURl = [[NSURL fileURLWithPath:zimPath] URLByAppendingPathExtension:@"idx"]; - _db = new Xapian::Database([xapianURl fileSystemRepresentation]); + self.idxFolderURL = [[NSURL fileURLWithPath:zimPath] URLByAppendingPathExtension:@"idx"]; + _db = new Xapian::Database([self.idxFolderURL fileSystemRepresentation]); } catch (const Xapian::DatabaseOpeningError &e) {} }