diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index 3cb03bf2..56aa27d0 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -20,7 +20,7 @@ class ImageCache { let cacheAttachmentQueue = dispatch_queue_create("ImageCacheAttachmentQueue", DISPATCH_QUEUE_SERIAL) // let cacheQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) - func imageOfAttachment(attachment: DiscoveredAttachment, withSize: CGSize?, completion: (url: NSURL, image: UIImage?) -> Void) { + func imageOfAttachment(attachment: DiscoveredAttachment, withSize: CGSize?, completion: (url: NSURL, image: UIImage?, cacheType: CacheType) -> Void) { guard let attachmentURL = NSURL(string: attachment.URLString) else { return @@ -40,15 +40,15 @@ class ImageCache { let attachmentOriginKey = "attachment-0.0-0.0-\(attachmentURL.absoluteString)" let attachmentSizeKey = "attachment-\(cacheSize.width)-\(cacheSize.height)-\(attachmentURL.absoluteString)" - + let OptionsInfos: KingfisherManager.Options = (forceRefresh: false, lowPriority: false, cacheMemoryOnly: false, shouldDecode: false, queue: cacheAttachmentQueue, scale: UIScreen.mainScreen().scale) //查找当前 Size 的 Cache Kingfisher.ImageCache.defaultCache.retrieveImageForKey(attachmentSizeKey, options: OptionsInfos) { (image, type) -> () in - + if let image = image?.decodedImage() { dispatch_async(dispatch_get_main_queue()) { - completion(url: attachmentURL, image: image) + completion(url: attachmentURL, image: image, cacheType: type) } } else { @@ -72,7 +72,7 @@ class ImageCache { } dispatch_async(dispatch_get_main_queue()) { - completion(url: attachmentURL, image: finalImage) + completion(url: attachmentURL, image: finalImage, cacheType: type) } } else { @@ -100,12 +100,12 @@ class ImageCache { println("Image Decode size \(storeImage.size)") dispatch_async(dispatch_get_main_queue()) { - completion(url: attachmentURL, image: finalImage) + completion(url: attachmentURL, image: finalImage, cacheType: .None) } } else { dispatch_async(dispatch_get_main_queue()) { - completion(url: attachmentURL, image: nil) + completion(url: attachmentURL, image: nil, cacheType: .None) } } }) diff --git a/Yep/Extensions/UIImageView+Yep.swift b/Yep/Extensions/UIImageView+Yep.swift index 4c1b6a5d..41297d44 100644 --- a/Yep/Extensions/UIImageView+Yep.swift +++ b/Yep/Extensions/UIImageView+Yep.swift @@ -30,15 +30,22 @@ extension UIImageView { yep_setAttachmentURL(attachmentURL) - ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: size, completion: { [weak self] (url, image) in + ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: size, completion: { [weak self] (url, image, cacheType) in guard let strongSelf = self, yep_attachmentURL = strongSelf.yep_attachmentURL where yep_attachmentURL == url else { return } - UIView.transitionWithView(strongSelf, duration: imageFadeTransitionDuration, options: .TransitionCrossDissolve, animations: { () -> Void in + if cacheType != .Memory { + UIView.transitionWithView(strongSelf, duration: imageFadeTransitionDuration, options: .TransitionCrossDissolve, animations: { () -> Void in + strongSelf.image = image + }, completion: nil) + + } else { strongSelf.image = image - }, completion: nil) + } + + println("imageOfAttachment cacheType: \(cacheType)") }) } diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index f80a88ab..22bcc3a7 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -2860,13 +2860,16 @@ func discoverFeedsWithSortStyle(sortStyle: FeedSortStyle, skill: Skill?, pageInd //let parse = parseFeeds let parse: JSONDictionary -> [DiscoveredFeed]? = { data in - if let realm = try? Realm() { - if let offlineData = try? NSJSONSerialization.dataWithJSONObject(data, options: []) { + // 只离线第一页 + if pageIndex == 1 { + if let realm = try? Realm() { + if let offlineData = try? NSJSONSerialization.dataWithJSONObject(data, options: []) { - let offlineJSON = OfflineJSON(name: OfflineJSONName.Feeds.rawValue, data: offlineData) + let offlineJSON = OfflineJSON(name: OfflineJSONName.Feeds.rawValue, data: offlineData) - let _ = try? realm.write { - realm.add(offlineJSON, update: true) + let _ = try? realm.write { + realm.add(offlineJSON, update: true) + } } } } diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index ac8654be..f2f97989 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -204,7 +204,7 @@ class FeedsViewController: BaseViewController { filterBarItem?.title = feedSortStyle.nameWithArrow updateFeeds() - + YepUserDefaults.feedSortStyle.value = feedSortStyle.rawValue } } @@ -287,6 +287,7 @@ class FeedsViewController: BaseViewController { // 没有 profileUser 才设置 feedSortStyle 以请求服务器 if profileUser == nil { + if let value = YepUserDefaults.feedSortStyle.value, _feedSortStyle = FeedSortStyle(rawValue: value) { @@ -438,16 +439,47 @@ class FeedsViewController: BaseViewController { if let strongSelf = self { + let newFeeds = feeds + let oldFeeds = strongSelf.feeds + + var needReloadData = false + + needReloadData = strongSelf.feeds.isEmpty + if isLoadMore { strongSelf.feeds += feeds + needReloadData = true + } else { strongSelf.feeds = feeds } - // 确保有新的才 reload - if !feeds.isEmpty { - strongSelf.feedsTableView.reloadData() // 服务端有新的排序算法,以及避免刷新后消息数字更新不及时的问题 + if !needReloadData && !newFeeds.isEmpty { + + if newFeeds.count == oldFeeds.count { + + var index = 0 + while index < newFeeds.count { + let newFeed = newFeeds[index] + let oldFeed = oldFeeds[index] + + if newFeed.id != oldFeed.id { + needReloadData = true + break + } + + index += 1 + } + + } else { + needReloadData = true + } + } + + if needReloadData { + println("new feeds, reloadData") + strongSelf.feedsTableView.reloadData() } } } diff --git a/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift b/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift index 1f2d3b20..680b3475 100644 --- a/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift +++ b/Yep/ViewControllers/MediaPreview/MediaPreviewViewController.swift @@ -383,7 +383,7 @@ extension MediaPreviewViewController: UICollectionViewDataSource, UICollectionVi mediaControlView.type = .Image - ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: nil, completion: { (url, image) in + ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: nil, completion: { (url, image, _) in guard url.absoluteString == attachment.URLString else { return } diff --git a/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift b/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift index a76ed143..2fa2f364 100644 --- a/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift +++ b/Yep/ViewControllers/MessageMedia/MessageMediaViewController.swift @@ -240,7 +240,7 @@ extension MessageMediaViewController: UICollectionViewDataSource, UICollectionVi mediaControlView.type = .Image - ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: nil, completion: { (url, image) in + ImageCache.sharedInstance.imageOfAttachment(attachment, withSize: nil, completion: { (url, image, _) in guard url.absoluteString == attachment.URLString else { return } diff --git a/Yep/Views/Cells/FeedSocialWork/FeedSocialWorkCell.swift b/Yep/Views/Cells/FeedSocialWork/FeedSocialWorkCell.swift index 68cabafb..3c1baef4 100644 --- a/Yep/Views/Cells/FeedSocialWork/FeedSocialWorkCell.swift +++ b/Yep/Views/Cells/FeedSocialWork/FeedSocialWorkCell.swift @@ -340,6 +340,7 @@ class FeedSocialWorkCell: FeedBasicCell { locationMapImageView.maskView = socialWorkMaskImageView socialWorkContainerViewHeightConstraint.constant = 110 + contentView.layoutIfNeeded() default: break