Ensure gif cells reload when app becomes active or network becomes available.

// FREEBIE
This commit is contained in:
Matthew Chen 2017-09-30 22:03:55 -04:00
parent 5b70116209
commit e4556967b6
4 changed files with 59 additions and 3 deletions

View File

@ -49,6 +49,7 @@
#import <JSQMessagesViewController/UIColor+JSQMessages.h>
#import <JSQSystemSoundPlayer.h>
#import <PureLayout/PureLayout.h>
#import <Reachability/Reachability.h>
#import <SignalServiceKit/AppVersion.h>
#import <SignalServiceKit/Asserts.h>
#import <SignalServiceKit/Contact.h>

View File

@ -77,7 +77,7 @@ class GifPickerCell: UICollectionViewCell {
clearFullAssetRequest()
}
private func ensureCellState() {
public func ensureCellState() {
guard isCellVisible else {
// Cancel any outstanding requests.
clearAssetRequests()

View File

@ -27,6 +27,8 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
var imageInfos = [GiphyImageInfo]()
var reachability: Reachability?
private let kCellReuseIdentifier = "kCellReuseIdentifier"
// MARK: Initializers
@ -57,6 +59,38 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
self.layout.delegate = self
}
deinit {
NotificationCenter.default.removeObserver(self)
}
func didBecomeActive() {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
// Prod cells to try to load when app becomes active.
ensureCellState()
}
func reachabilityChanged() {
AssertIsOnMainThread()
Logger.info("\(self.TAG) \(#function)")
// Prod cells to try to load when connectivity changes.
ensureCellState()
}
func ensureCellState() {
for cell in self.collectionView.visibleCells {
guard let cell = cell as? GifPickerCell else {
owsFail("\(TAG) unexpected cell.")
return
}
cell.ensureCellState()
}
}
// MARK: View Lifecycle
override func viewDidLoad() {
@ -71,6 +105,16 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect
comment: "Title for the 'gif picker' dialog.")
createViews()
reachability = Reachability.forInternetConnection()
NotificationCenter.default.addObserver(self,
selector:#selector(reachabilityChanged),
name:NSNotification.Name.reachabilityChanged,
object:nil)
NotificationCenter.default.addObserver(self,
selector:#selector(didBecomeActive),
name:NSNotification.Name.UIApplicationDidBecomeActive,
object:nil)
}
override func viewDidAppear(_ animated: Bool) {

View File

@ -269,11 +269,18 @@ extension URLSessionTask {
self.startRequestIfNecessary()
return
}
guard UIApplication.shared.applicationState == .active else {
// If app is not active, fail the asset request.
self.assetRequestDidFail(assetRequest:assetRequest)
self.startRequestIfNecessary()
return
}
self.activeAssetRequests.insert(assetRequest)
if let asset = self.assetMap.get(key:assetRequest.rendition.url) {
// Deferred cache hit, avoids re-downloading assets already in the
// asset cache.
// Deferred cache hit, avoids re-downloading assets that were
// downloaded while this request was queued.
self.assetRequestDidSucceed(assetRequest : assetRequest, asset: asset)
return
@ -297,6 +304,10 @@ extension URLSessionTask {
// Prefer the first "high" priority request,
// fall back to the first "low" priority request.
//
// TODO: We could refine this logic to defer requests if
// there is already an active asset request with the
// same URL.
for priority in [GiphyRequestPriority.high, GiphyRequestPriority.low] {
for (assetRequestIndex, assetRequest) in assetRequestQueue.enumerated() {
if assetRequest.priority == priority {