Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
yamadapc committed Apr 22, 2016
1 parent 03fd93c commit 946b0ab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 49 deletions.
4 changes: 2 additions & 2 deletions iBooks Export/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="9531"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10116"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down
107 changes: 60 additions & 47 deletions iBooks Export/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,7 @@ class ViewController: NSViewController, NSTableViewDataSource {
}

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
let filemanager = NSFileManager()
var error: NSError?

let isDir = UnsafeMutablePointer<ObjCBool>.alloc(1)
let exists = filemanager.fileExistsAtPath(exportDir!, isDirectory: isDir)
if !exists && isDir.memory {
do {
try filemanager.createDirectoryAtPath(exportDir!, withIntermediateDirectories: false, attributes: nil)
} catch let error1 as NSError {
error = error1
} catch {
fatalError()
}
}

if error != nil {
print(error)
}

for book in self.books {
if let targetPath = self.targetPath(book) {
if filemanager.fileExistsAtPath(exportDir! + "/" + targetPath) {
self.progressIndicator.incrementBy(1)
continue
}

do {
try filemanager.copyItemAtPath(book.path, toPath: exportDir! + "/" + targetPath)
} catch let error1 as NSError {
error = error1
} catch {
fatalError()
}
if error != nil {
print(error)
}
}


dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) {
self.progressIndicator.incrementBy(1)
}
}

dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) {
self.progressIndicator.stopAnimation(self)
}
self.runExport(exportDir!)
}
}

Expand All @@ -95,6 +49,65 @@ class ViewController: NSViewController, NSTableViewDataSource {
// Update the view, if already loaded.
}
}

func runExport(exportDir: String) {
let filemanager = NSFileManager()
var error: NSError?

let isDir = UnsafeMutablePointer<ObjCBool>.alloc(1)
let exists = filemanager.fileExistsAtPath(exportDir, isDirectory: isDir)
if !exists && isDir.memory {
do {
try filemanager.createDirectoryAtPath(exportDir, withIntermediateDirectories: false, attributes: nil)
} catch let error1 as NSError {
error = error1
} catch {
fatalError()
}
}

if error != nil {
print(error)
}

for book in self.books {
self.exportBook(filemanager, exportDir: exportDir, book: book)
}

self.onExport()
}

func exportBook(filemanager: NSFileManager, exportDir: String, book: Book) {
if let targetPath = self.targetPath(book) {
if filemanager.fileExistsAtPath(exportDir + "/" + targetPath) {
self.progressIndicator.incrementBy(1)
return
}

var error: NSError?
do {
try filemanager.copyItemAtPath(book.path, toPath: exportDir + "/" + targetPath)
} catch let error1 as NSError {
error = error1
} catch {
fatalError()
}
if error != nil {
print(error)
}
}


dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) {
self.progressIndicator.incrementBy(1)
}
}

func onExport() {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)) {
self.progressIndicator.stopAnimation(self)
}
}

func numberOfRowsInTableView(tableView: NSTableView) -> Int {
if tableView != self.tableView {
Expand Down

0 comments on commit 946b0ab

Please sign in to comment.