From ffe152c4ee57dad411235ee9774d33d39097a116 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 7 Dec 2015 14:57:30 +0800 Subject: [PATCH 1/7] cacheType for imageOfAttachment --- Yep/Caches/ImageCache.swift | 10 +++++----- Yep/Extensions/UIImageView+Yep.swift | 15 ++++++++++++--- .../Feeds/FeedsViewController.swift | 3 ++- .../MediaPreview/MediaPreviewViewController.swift | 2 +- .../MessageMedia/MessageMediaViewController.swift | 2 +- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index 3cb03bf2..ec80c4ac 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 @@ -48,7 +48,7 @@ class ImageCache { 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..5a315b73 100644 --- a/Yep/Extensions/UIImageView+Yep.swift +++ b/Yep/Extensions/UIImageView+Yep.swift @@ -30,15 +30,24 @@ 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) + + println("cacheType != .Memory") + + } else { strongSelf.image = image - }, completion: nil) + + println("cacheType == .Memory") + } }) } diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index ac8654be..8d6970cd 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -198,7 +198,7 @@ class FeedsViewController: BaseViewController { var feedSortStyle: FeedSortStyle = .Match { didSet { - feeds = [] + //feeds = [] feedsTableView.reloadData() filterBarItem?.title = feedSortStyle.nameWithArrow @@ -447,6 +447,7 @@ class FeedsViewController: BaseViewController { // 确保有新的才 reload if !feeds.isEmpty { + 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 } From d12ccea3f9a2ca34a10d0a5df9224610349b6cc5 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 7 Dec 2015 15:38:57 +0800 Subject: [PATCH 2/7] storeImage --- Yep/Caches/ImageCache.swift | 4 +++- Yep/Extensions/UIImageView+Yep.swift | 6 ++---- Yep/ViewControllers/Feeds/FeedsViewController.swift | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index ec80c4ac..bbc388fa 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -45,10 +45,12 @@ class ImageCache { //查找当前 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, cacheType: type) + + Kingfisher.ImageCache.defaultCache.storeImage(image, forKey: attachmentSizeKey, toDisk: false, completionHandler: nil) } } else { diff --git a/Yep/Extensions/UIImageView+Yep.swift b/Yep/Extensions/UIImageView+Yep.swift index 5a315b73..41297d44 100644 --- a/Yep/Extensions/UIImageView+Yep.swift +++ b/Yep/Extensions/UIImageView+Yep.swift @@ -41,13 +41,11 @@ extension UIImageView { strongSelf.image = image }, completion: nil) - println("cacheType != .Memory") - } else { strongSelf.image = image - - println("cacheType == .Memory") } + + println("imageOfAttachment cacheType: \(cacheType)") }) } diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index 8d6970cd..957ab72b 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -198,13 +198,13 @@ class FeedsViewController: BaseViewController { var feedSortStyle: FeedSortStyle = .Match { didSet { - //feeds = [] + feeds = [] feedsTableView.reloadData() 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) { From 5d3fe3124d84c5056fdce812fdffcba13723a54f Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 7 Dec 2015 16:16:43 +0800 Subject: [PATCH 3/7] make sure needReloadData after updateFeeds --- Yep/Caches/ImageCache.swift | 4 +-- .../Feeds/FeedsViewController.swift | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index bbc388fa..56aa27d0 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -40,7 +40,7 @@ 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 @@ -49,8 +49,6 @@ class ImageCache { if let image = image?.decodedImage() { dispatch_async(dispatch_get_main_queue()) { completion(url: attachmentURL, image: image, cacheType: type) - - Kingfisher.ImageCache.defaultCache.storeImage(image, forKey: attachmentSizeKey, toDisk: false, completionHandler: nil) } } else { diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index 957ab72b..44eba909 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -439,6 +439,12 @@ class FeedsViewController: BaseViewController { if let strongSelf = self { + let newFeeds = feeds + + var needReloadData = false + + needReloadData = strongSelf.feeds.isEmpty + if isLoadMore { strongSelf.feeds += feeds @@ -446,10 +452,30 @@ class FeedsViewController: BaseViewController { strongSelf.feeds = feeds } - // 确保有新的才 reload if !feeds.isEmpty { + if newFeeds.count == strongSelf.feeds.count { + + var index = 0 + while index < newFeeds.count { + let newFeed = newFeeds[index] + let oldFeed = strongSelf.feeds[index] + + if newFeed.id != oldFeed.id { + needReloadData = true + break + } + + index += 1 + } + + } else { + needReloadData = true + } + } + + if needReloadData { println("new feeds, reloadData") - strongSelf.feedsTableView.reloadData() // 服务端有新的排序算法,以及避免刷新后消息数字更新不及时的问题 + strongSelf.feedsTableView.reloadData() } } } From 7304b802adad041d15587c83d5565fc66dc772ef Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 7 Dec 2015 16:42:10 +0800 Subject: [PATCH 4/7] better cal needReloadData --- Yep/ViewControllers/Feeds/FeedsViewController.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index 44eba909..b357f1eb 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -452,7 +452,8 @@ class FeedsViewController: BaseViewController { strongSelf.feeds = feeds } - if !feeds.isEmpty { + if !needReloadData && !feeds.isEmpty { + if newFeeds.count == strongSelf.feeds.count { var index = 0 From 36d528cfefc5e616372faeeae1ea7739b9050f7b Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 7 Dec 2015 17:04:36 +0800 Subject: [PATCH 5/7] fix show map image --- Yep/Views/Cells/FeedSocialWork/FeedSocialWorkCell.swift | 1 + 1 file changed, 1 insertion(+) 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 From 2c3553e37cd31bd8a440115e12ecd70676a7e1e0 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 7 Dec 2015 17:09:05 +0800 Subject: [PATCH 6/7] better logic for cal needReloadData --- Yep/ViewControllers/Feeds/FeedsViewController.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index b357f1eb..f2f97989 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -440,6 +440,7 @@ class FeedsViewController: BaseViewController { if let strongSelf = self { let newFeeds = feeds + let oldFeeds = strongSelf.feeds var needReloadData = false @@ -448,18 +449,20 @@ class FeedsViewController: BaseViewController { if isLoadMore { strongSelf.feeds += feeds + needReloadData = true + } else { strongSelf.feeds = feeds } - if !needReloadData && !feeds.isEmpty { + if !needReloadData && !newFeeds.isEmpty { - if newFeeds.count == strongSelf.feeds.count { + if newFeeds.count == oldFeeds.count { var index = 0 while index < newFeeds.count { let newFeed = newFeeds[index] - let oldFeed = strongSelf.feeds[index] + let oldFeed = oldFeeds[index] if newFeed.id != oldFeed.id { needReloadData = true From be82fdbe071dfa3170bafb0c3f3cebba5586932f Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 7 Dec 2015 17:13:26 +0800 Subject: [PATCH 7/7] only save offline feeds in first page --- Yep/Services/YepService.swift | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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) + } } } }