DirectoryMonitor

This commit is contained in:
Chris Li 2016-11-10 09:57:37 -05:00
parent 1d357c03d5
commit 5115071aaf
5 changed files with 55 additions and 68 deletions

View File

@ -108,6 +108,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
})
})
operation.add(observer: DidFinishObserver { (operation, errors) in
guard let operation = operation as? RefreshLibraryOperation else {return}
OperationQueue.main.addOperation({
defer {
self.refreshControl?.endRefreshing()
@ -115,42 +116,19 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
self.tableView.reloadEmptyDataSet()
}
// handle error
// show reachibility alert
// show lang filter alert
if let error = errors.first {
// handle error [network, xmlparse]
} else {
if operation.firstTime {
//self.showLanguageFilterAlert()
//self.configureNavBarButtons()
} else {
self.showRefreshSuccessMessage()
}
}
})
})
GlobalQueue.shared.add(operation: operation)
// let operation = RefreshLibraryOperation()
// operation.add(WillExecuteObserver { (operation) in
// })
//
// operation.add(DidFinishObserver { (operation, errors) in
// guard let operation = operation as? RefreshLibraryOperation else {return}
// NSOperationQueue.mainQueue().addOperationWithBlock({
// defer {
// self.refreshControl?.endRefreshing()
// self.isRefreshing = false
// self.tableView.reloadEmptyDataSet()
// }
//
// if errors.count > 0 {
// if let error = errors.first as? ReachabilityCondition.Error, error == ReachabilityCondition.Error.NotReachable && invokedByUser == true {
// self.showReachibilityAlert()
// }
// } else{
// if operation.firstTime {
// self.showLanguageFilterAlert()
// self.configureNavBarButtons()
// } else {
// self.showRefreshSuccessMessage()
// }
// }
// })
// })
// GlobalQueue.shared.add(operation)
}
func showRefreshSuccessMessage() {
@ -292,32 +270,31 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {}
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?{
// guard let book = fetchedResultController.object(at: indexPath) as? Book else {return nil}
// switch book.spaceState {
// case .enough:
// let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in
let book = fetchedResultController.object(at: indexPath)
switch book.spaceState {
case .enough:
let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in
// guard let download = DownloadBookOperation(bookID: book.id) else {return}
// Network.shared.queue.addOperation(download)
// })
// action.backgroundColor = UIColor.defaultTint
// return [action]
// case .caution:
// let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in
})
action.backgroundColor = UIColor.defaultTint
return [action]
case .caution:
let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.download, handler: { _ in
// let alert = SpaceCautionAlert(context: self, bookID: book.id)
// GlobalQueue.shared.addOperation(alert)
// self.tableView.setEditing(false, animated: true)
// })
// action.backgroundColor = UIColor.orange
// return [action]
// case .notEnough:
// let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.spaceNotEnough, handler: { _ in
self.tableView.setEditing(false, animated: true)
})
action.backgroundColor = UIColor.orange
return [action]
case .notEnough:
let action = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: LocalizedStrings.spaceNotEnough, handler: { _ in
// let alert = SpaceNotEnoughAlert(context: self)
// GlobalQueue.shared.addOperation(alert)
// self.tableView.setEditing(false, animated: true)
// })
// return [action]
// }
return []
self.tableView.setEditing(false, animated: true)
})
return [action]
}
}
// MARK: - Fetched Results Controller
@ -342,7 +319,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
tableView.reloadData()
}
fileprivate var langPredicate: NSPredicate {
private var langPredicate: NSPredicate {
let displayedLanguages = Language.fetch(displayed: true, context: managedObjectContext)
if displayedLanguages.count > 0 {
return NSPredicate(format: "language IN %@", displayedLanguages)
@ -351,7 +328,7 @@ class CloudBooksController: UITableViewController, NSFetchedResultsControllerDel
}
}
fileprivate var onlineCompoundPredicate: NSCompoundPredicate {
private var onlineCompoundPredicate: NSCompoundPredicate {
let isCloudPredicate = NSPredicate(format: "stateRaw == 0")
return NSCompoundPredicate(andPredicateWithSubpredicates: [langPredicate, isCloudPredicate])
}

View File

@ -19,6 +19,16 @@ class LibrarySplitViewController: UISplitViewController, UISplitViewControllerDe
configureDismissButton()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
guard traitCollection != previousTraitCollection else {return}
let controller: UITableViewController? = {
let nav = viewControllers.first as? UINavigationController
let tab = nav?.topViewController as? UITabBarController
return tab?.selectedViewController as? UITableViewController
}()
controller?.tableView.reloadData()
}
func configureDismissButton() {
guard let master = viewControllers.first as? UINavigationController else {return}
let barButtonItem = UIBarButtonItem(image: UIImage(named: "Cross"), style: .plain, target: self, action: #selector(dismiss(sender:)))

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.2256</string>
<string>1.8.2274</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.2256</string>
<string>1.8.2274</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>

View File

@ -78,19 +78,19 @@ class DirectoryMonitor {
// MARK: - Custom Methods
var isCheckingChanges = false
var previousDirectoryHash: [String]? = nil
var currentDirectoryHash: [String]? = nil
var hashEqualCheck = 0
private var isCheckingChanges = false
private var previousDirectoryHash: [String]? = nil
private var currentDirectoryHash: [String]? = nil
private var hashEqualCheck = 0
fileprivate func directoryContentDidChange() {
private func directoryContentDidChange() {
hashEqualCheck = 0
if isCheckingChanges == false {
checkDirectoryChanges()
}
}
fileprivate func checkDirectoryChanges() {
private func checkDirectoryChanges() {
isCheckingChanges = true
previousDirectoryHash = currentDirectoryHash
@ -116,13 +116,13 @@ class DirectoryMonitor {
}
}
fileprivate func directoryDidReachStasis() {
private func directoryDidReachStasis() {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(NSEC_PER_SEC/10)) / Double(NSEC_PER_SEC) , execute: { () -> Void in
self.delegate?.directoryMonitorDidObserveChange()
})
}
fileprivate func waitAndCheckAgain() {
private func waitAndCheckAgain() {
directoryMonitorQueue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(NSEC_PER_SEC/2)) / Double(NSEC_PER_SEC) , execute: { () -> Void in
self.checkDirectoryChanges()
})
@ -130,7 +130,7 @@ class DirectoryMonitor {
// MARK: - Generate directory file info array
fileprivate func directoryHashes() -> [String] {
private func directoryHashes() -> [String] {
var hashes = [String]()
do {
let contents = try FileManager.default.contentsOfDirectory(atPath: url.path)
@ -146,7 +146,7 @@ class DirectoryMonitor {
return hashes
}
fileprivate func fileHash(_ fileName: String) -> String? {
private func fileHash(_ fileName: String) -> String? {
if let fileSize = fileSize(fileName) {
return fileName + "_\(fileSize)"
} else {
@ -154,7 +154,7 @@ class DirectoryMonitor {
}
}
fileprivate func fileSize(_ fileName: String) -> Int64? {
private func fileSize(_ fileName: String) -> Int64? {
let path = self.url.appendingPathComponent(fileName).path
if FileManager.default.fileExists(atPath: path) {
do {