Remove Book operation

This commit is contained in:
Chris Li 2016-09-15 14:47:08 -04:00
parent 9884698d2d
commit b4226ff9d1
9 changed files with 44 additions and 22 deletions

View File

@ -33,3 +33,18 @@ class SpaceCautionAlert: AlertOperation<UIViewController> {
preferredAction = actions[0]
}
}
class RemoveBookConfirmationAlert: AlertOperation<UIViewController> {
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]
}
}

View File

@ -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

View File

@ -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]
}

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.580</string>
<string>1.8.622</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.8.583</string>
<string>1.8.625</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -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()
}
}

View File

@ -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)

View File

@ -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;

View File

@ -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) {}
}