Skip to content

Commit

Permalink
Fixes bug with GridView reloading unexpectedly.
Browse files Browse the repository at this point in the history
  • Loading branch information
mluisbrown committed May 24, 2016
1 parent a0829d8 commit 13007d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
27 changes: 12 additions & 15 deletions Memories/Grid View/GridViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ import PHAssetHelper

class GridViewModel {
private let assetHelper = PHAssetHelper()
private var assetFetchResults = [PHFetchResult]() {
didSet {
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
self.onDataChanged(self.date.value)
}
}
}
private var assetFetchResults = [PHFetchResult]()

let date : Dynamic<NSDate>
let onDataChanged: (NSDate) -> ()
Expand All @@ -33,24 +27,27 @@ class GridViewModel {
self.onDataChanged = onDataChanged
self.date = Dynamic(NSDate())
self.date.bind {
let date = $0
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in
self.assetFetchResults = self.assetHelper.fetchResultsForDateInAllYears(date)
}
self.fetchDataAndNotify($0)
}
}

init(date: NSDate, onDataChanged: (date: NSDate) -> ()) {
self.onDataChanged = onDataChanged
self.date = Dynamic(date)
self.date.bindAndFire {
let date = $0
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in
self.assetFetchResults = self.assetHelper.fetchResultsForDateInAllYears(date)
}
self.fetchDataAndNotify($0)
}
}

private func fetchDataAndNotify(date: NSDate) {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in
self.assetFetchResults = self.assetHelper.fetchResultsForDateInAllYears(date)
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
self.onDataChanged(self.date.value)
}
}
}

// MARK: - API
func goToNextDay() {
date.value = date.value.nextDay()
Expand Down
26 changes: 22 additions & 4 deletions Memories/Photo View/PhotoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl
@IBAction func close(sender: UIButton) {
if let navController = presentingViewController as? UINavigationController,
gridViewController = navController.topViewController as? GridViewController {
cancelAllImageRequests()
PHPhotoLibrary.sharedPhotoLibrary().unregisterChangeObserver(self)

gridViewController.setSelectedIndex(model.selectedIndex)

if traitCollection.verticalSizeClass == .Regular {
hideStatusBar(false)
}
Expand Down Expand Up @@ -268,7 +270,7 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl
options.deliveryMode = .HighQualityFormat
options.synchronous = false

let requestId = PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: options) { (result, userInfo) -> Void in
pageView.imageRequestId = PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: options) { (result, userInfo) -> Void in
if let image = result {
UpgradeManager.highQualityViewCount += 1
pageView.image = image
Expand All @@ -289,8 +291,6 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl
pageView.fullImageUnavailable = true
}
}

pageView.imageRequestId = requestId
}

func purgePage(page: Int) {
Expand All @@ -302,13 +302,27 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl
if let pageView = pageViews[page] {
if let requestId = pageView.imageRequestId {
PHImageManager.defaultManager().cancelImageRequest(requestId)
pageView.imageRequestId = nil
}
pageView.image = nil
pageView.removeFromSuperview()
pageViews[page] = nil
}
}

func cancelPageImageRequest(page: Int) {
guard page >= 0 && page < pageViews.count else {
return
}

if let pageView = pageViews[page] {
if let requestId = pageView.imageRequestId {
PHImageManager.defaultManager().cancelImageRequest(requestId)
pageView.imageRequestId = nil
}
}
}

func loadVisiblePages() {
let initialLoad = !initialOffsetSet

Expand Down Expand Up @@ -343,6 +357,10 @@ class PhotoViewController: UIViewController, UIScrollViewDelegate, UIViewControl
model.assets.count.stride(to: lastPage, by: -1).forEach(purgePage)
}

func cancelAllImageRequests() {
pageViews.indices.forEach(cancelPageImageRequest)
}

func purgeAllViews() {
pageViews.indices.forEach(purgePage)
}
Expand Down

0 comments on commit 13007d0

Please sign in to comment.