Core data book entity update

This commit is contained in:
Chris Li 2016-09-06 15:47:44 -04:00
parent 44d33f36ce
commit 8cb6f20c2c
10 changed files with 21 additions and 26 deletions

View File

@ -101,8 +101,7 @@ class BookDetailController: UITableViewController, CenterButtonCellDelegate, DZN
switch title { switch title {
case Strings.downloadNow: case Strings.downloadNow:
func startDownload() { func startDownload() {
guard let bookID = book.id, guard let download = DownloadBookOperation(bookID: book.id) else {return}
let download = DownloadBookOperation(bookID: bookID) else {return}
Network.shared.queue.addOperation(download) Network.shared.queue.addOperation(download)
} }

View File

@ -107,13 +107,13 @@ class DownloadTasksController: UIViewController, UITableViewDelegate, UITableVie
func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath, animated: Bool = false) { func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath, animated: Bool = false) {
guard let downloadTask = fetchedResultController.objectAtIndexPath(indexPath) as? DownloadTask, guard let downloadTask = fetchedResultController.objectAtIndexPath(indexPath) as? DownloadTask,
let book = downloadTask.book, let id = book.id, let book = downloadTask.book,
let cell = cell as? DownloadBookCell else {return} let cell = cell as? DownloadBookCell else {return}
cell.titleLabel.text = book.title cell.titleLabel.text = book.title
cell.favIcon.image = UIImage(data: book.favIcon ?? NSData()) cell.favIcon.image = UIImage(data: book.favIcon ?? NSData())
if let progress = Network.shared.operations[id]?.progress { if let progress = Network.shared.operations[book.id]?.progress {
cell.progressLabel.text = progress.fractionCompletedDescription cell.progressLabel.text = progress.fractionCompletedDescription
cell.progressView.setProgress(Float(progress.fractionCompleted), animated: animated) cell.progressView.setProgress(Float(progress.fractionCompleted), animated: animated)
cell.detailLabel.text = { cell.detailLabel.text = {
@ -195,8 +195,8 @@ class DownloadTasksController: UIViewController, UITableViewDelegate, UITableVie
actions.insert(pause, atIndex: 0) actions.insert(pause, atIndex: 0)
case .Paused: case .Paused:
if let book = downloadTask.book, let bookID = book.id, if let book = downloadTask.book,
let resumeData = Preference.resumeData[bookID] { let resumeData = Preference.resumeData[book.id] {
let resume = UITableViewRowAction(style: .Normal, title: "Resume") { (action, indexPath) in let resume = UITableViewRowAction(style: .Normal, title: "Resume") { (action, indexPath) in
let task = Network.shared.session.downloadTaskWithResumeData(resumeData) let task = Network.shared.session.downloadTaskWithResumeData(resumeData)
let operation = DownloadBookOperation(downloadTask: task) let operation = DownloadBookOperation(downloadTask: task)

View File

@ -133,7 +133,7 @@ class LocalBooksController: UIViewController, UITableViewDelegate, UITableViewDa
let delete = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.delete) { (action, indexPath) -> Void in let delete = UITableViewRowAction(style: .Destructive, title: LocalizedStrings.delete) { (action, indexPath) -> Void in
guard let book = self.fetchedResultController.objectAtIndexPath(indexPath) as? Book else {return} guard let book = self.fetchedResultController.objectAtIndexPath(indexPath) as? Book else {return}
self.managedObjectContext.performBlock({ () -> Void in self.managedObjectContext.performBlock({ () -> Void in
if let id = book.id, let zimURL = ZimMultiReader.sharedInstance.readers[id]?.fileURL { if let zimURL = ZimMultiReader.sharedInstance.readers[book.id]?.fileURL {
FileManager.removeItem(atURL: zimURL) FileManager.removeItem(atURL: zimURL)
let indexFolderURL = zimURL.URLByAppendingPathExtension("idx") let indexFolderURL = zimURL.URLByAppendingPathExtension("idx")

View File

@ -143,10 +143,9 @@ class SearchBooksVC: UIViewController, UITableViewDelegate, UITableViewDataSourc
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
guard let mainVC = parentViewController?.parentViewController as? MainController, guard let mainVC = parentViewController?.parentViewController as? MainController,
let book = fetchedResultController.objectAtIndexPath(indexPath) as? Book, let book = fetchedResultController.objectAtIndexPath(indexPath) as? Book else {return}
let bookID = book.id else {return}
mainVC.hideSearch(animated: true) mainVC.hideSearch(animated: true)
mainVC.loadMainPage(bookID) mainVC.loadMainPage(book.id)
tableView.deselectRowAtIndexPath(indexPath, animated: true) tableView.deselectRowAtIndexPath(indexPath, animated: true)
} }

View File

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

View File

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

View File

@ -21,7 +21,7 @@ extension Book {
@NSManaged var favIcon: NSData? @NSManaged var favIcon: NSData?
@NSManaged var fileSize: Int64 @NSManaged var fileSize: Int64
@NSManaged var globalCount: Int64 @NSManaged var globalCount: Int64
@NSManaged var id: String? @NSManaged var id: String
@NSManaged var isLocal: NSNumber? @NSManaged var isLocal: NSNumber?
@NSManaged var hasPic: Bool @NSManaged var hasPic: Bool
@NSManaged var hasIndex: Bool @NSManaged var hasIndex: Bool

View File

@ -19,9 +19,10 @@ class Book: NSManagedObject {
// MARK: - Add Book // MARK: - Add Book
class func add(metadata: [String: AnyObject], context: NSManagedObjectContext) -> Book? { class func add(metadata: [String: AnyObject], context: NSManagedObjectContext) -> Book? {
guard let id = metadata["id"] as? String else {return nil}
guard let book = insert(Book.self, context: context) else {return nil} guard let book = insert(Book.self, context: context) else {return nil}
book.id = metadata["id"] as? String book.id = id
book.title = metadata["title"] as? String book.title = metadata["title"] as? String
book.creator = metadata["creator"] as? String book.creator = metadata["creator"] as? String
book.publisher = metadata["publisher"] as? String book.publisher = metadata["publisher"] as? String
@ -75,8 +76,7 @@ class Book: NSManagedObject {
} }
var resumeDataURL: NSURL? { var resumeDataURL: NSURL? {
guard let id = id, guard let folderURL = NSURL(fileURLWithPath: NSFileManager.libDirURL.path!).URLByAppendingPathComponent("DownloadTemp", isDirectory: true),
let folderURL = NSURL(fileURLWithPath: NSFileManager.libDirURL.path!).URLByAppendingPathComponent("DownloadTemp", isDirectory: true),
let folderPath = folderURL.path else {return nil} let folderPath = folderURL.path else {return nil}
if !NSFileManager.defaultManager().fileExistsAtPath(folderPath) { if !NSFileManager.defaultManager().fileExistsAtPath(folderPath) {
_ = try? NSFileManager.defaultManager().createDirectoryAtURL(folderURL, withIntermediateDirectories: true, attributes: [NSURLIsExcludedFromBackupKey: true]) _ = try? NSFileManager.defaultManager().createDirectoryAtURL(folderURL, withIntermediateDirectories: true, attributes: [NSURLIsExcludedFromBackupKey: true])
@ -99,8 +99,7 @@ class Book: NSManagedObject {
var books = [ZimID: Book]() var books = [ZimID: Book]()
for book in localBooks { for book in localBooks {
guard let id = book.id else {continue} books[book.id] = book
books[id] = book
} }
return books return books
} }
@ -114,7 +113,6 @@ class Book: NSManagedObject {
// MARK: - Manage // MARK: - Manage
func removeResumeData() { func removeResumeData() {
guard let id = id else {return}
Preference.resumeData[id] = nil Preference.resumeData[id] = nil
} }

View File

@ -21,10 +21,10 @@
<attribute name="favIcon" optional="YES" attributeType="Binary" syncable="YES"/> <attribute name="favIcon" optional="YES" attributeType="Binary" syncable="YES"/>
<attribute name="fileSize" optional="YES" attributeType="Integer 64" minValueString="0" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/> <attribute name="fileSize" optional="YES" attributeType="Integer 64" minValueString="0" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="globalCount" optional="YES" attributeType="Integer 64" minValueString="0" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/> <attribute name="globalCount" optional="YES" attributeType="Integer 64" minValueString="0" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="hasIndex" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/> <attribute name="hasIndex" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/>
<attribute name="hasPic" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/> <attribute name="hasPic" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES" syncable="YES"/>
<attribute name="id" attributeType="String" defaultValueString="Unknown" syncable="YES"/> <attribute name="id" attributeType="String" defaultValueString="Unknown" syncable="YES"/>
<attribute name="includeInSearch" optional="YES" attributeType="Boolean" defaultValueString="YES" usesScalarValueType="YES" syncable="YES"/> <attribute name="includeInSearch" attributeType="Boolean" defaultValueString="YES" usesScalarValueType="YES" syncable="YES"/>
<attribute name="isLocal" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="NO" syncable="YES"/> <attribute name="isLocal" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="NO" syncable="YES"/>
<attribute name="mediaCount" optional="YES" attributeType="Integer 64" minValueString="0" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/> <attribute name="mediaCount" optional="YES" attributeType="Integer 64" minValueString="0" defaultValueString="0" usesScalarValueType="YES" syncable="YES"/>
<attribute name="meta4URL" optional="YES" attributeType="String" syncable="YES"/> <attribute name="meta4URL" optional="YES" attributeType="String" syncable="YES"/>
@ -79,10 +79,10 @@
<fetchRequest name="AllArticlesBookmarked" entity="Article" predicateString="isBookmarked == 1 AND book.downloadStateRaw == 1"/> <fetchRequest name="AllArticlesBookmarked" entity="Article" predicateString="isBookmarked == 1 AND book.downloadStateRaw == 1"/>
<elements> <elements>
<element name="Article" positionX="-108" positionY="-136" width="128" height="210"/> <element name="Article" positionX="-108" positionY="-136" width="128" height="210"/>
<element name="Book" positionX="-405" positionY="-129" width="128" height="343"/> <element name="Book" positionX="-405" positionY="-129" width="128" height="345"/>
<element name="Collection" positionX="-144" positionY="63" width="128" height="75"/>
<element name="DownloadTask" positionX="-108" positionY="84" width="128" height="105"/> <element name="DownloadTask" positionX="-108" positionY="84" width="128" height="105"/>
<element name="Language" positionX="-110" positionY="199" width="128" height="103"/> <element name="Language" positionX="-110" positionY="199" width="128" height="103"/>
<element name="Tag" positionX="124" positionY="-126" width="128" height="73"/> <element name="Tag" positionX="124" positionY="-126" width="128" height="73"/>
<element name="Collection" positionX="-144" positionY="63" width="128" height="75"/>
</elements> </elements>
</model> </model>

View File

@ -13,8 +13,7 @@ class FileManager {
class func move(book: Book, fromURL: NSURL, suggestedFileName: String?) { class func move(book: Book, fromURL: NSURL, suggestedFileName: String?) {
let fileName: String = { let fileName: String = {
if let suggestedFileName = suggestedFileName {return suggestedFileName} if let suggestedFileName = suggestedFileName {return suggestedFileName}
if let id = book.id {return "\(id).zim"} return book.id
return NSDate().description + ".zim"
}() }()
let directory = NSFileManager.docDirURL let directory = NSFileManager.docDirURL
createDirectory(directory, includeInICloudBackup: false) createDirectory(directory, includeInICloudBackup: false)