From 1702f270c70370e2757dc40eb8edcff4aed25437 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 1 Feb 2016 17:14:21 +0800 Subject: [PATCH] check message.attachmentExpiresUnixTime for download --- Yep/Caches/ImageCache.swift | 86 ++++++++++++++----- Yep/Services/YepService.swift | 4 +- .../ConversationsViewController.swift | 5 -- 3 files changed, 66 insertions(+), 29 deletions(-) diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index 798cf35e..7a31820f 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -215,31 +215,73 @@ class ImageCache { if let message = messageWithMessageID(messageID, inRealm: realm) { - let mediaType = message.mediaType + func doDownloadAttachmentsOfMessage(message: Message) { - YepDownloader.downloadAttachmentsOfMessage(message, reportProgress: { progress, image in - dispatch_async(dispatch_get_main_queue()) { - completion(loadingProgress: progress, image: image) - } + let mediaType = message.mediaType - }, imageTransform: { image in - return image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage() - - }, imageFinished: { image in - - let messageImage = image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage() - - self.cache.setObject(messageImage, forKey: imageKey) - - dispatch_async(dispatch_get_main_queue()) { - if mediaType == MessageMediaType.Image.rawValue { - completion(loadingProgress: 1.0, image: messageImage) - - } else { // 视频的封面图片,要保障设置到 - completion(loadingProgress: 1.5, image: messageImage) + YepDownloader.downloadAttachmentsOfMessage(message, reportProgress: { progress, image in + dispatch_async(dispatch_get_main_queue()) { + completion(loadingProgress: progress, image: image) } - } - }) + + }, imageTransform: { image in + return image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage() + + }, imageFinished: { image in + + let messageImage = image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage() + + self.cache.setObject(messageImage, forKey: imageKey) + + dispatch_async(dispatch_get_main_queue()) { + if mediaType == MessageMediaType.Image.rawValue { + completion(loadingProgress: 1.0, image: messageImage) + + } else { // 视频的封面图片,要保障设置到 + completion(loadingProgress: 1.5, image: messageImage) + } + } + }) + } + + // 若过期了,刷新后再下载。这里减少一天来判断 + if message.attachmentExpiresUnixTime < (NSDate().timeIntervalSince1970 + (60 * 60 * 24)) { + + refreshAttachmentWithID(message.attachmentID, failureHandler: nil, completion: { newAttachmentInfo in + //println("newAttachmentInfo: \(newAttachmentInfo)") + + guard let realm = try? Realm() else { + return + } + + if let message = messageWithMessageID(messageID, inRealm: realm) { + + if let fileInfo = newAttachmentInfo["file"] as? JSONDictionary { + + realm.beginWrite() + + if let attachmentExpiresUnixTime = fileInfo["expires_at"] as? NSTimeInterval { + message.attachmentExpiresUnixTime = attachmentExpiresUnixTime + } + + if let URLString = fileInfo["url"] as? String { + message.attachmentURLString = URLString + } + + if let URLString = fileInfo["thumb_url"] as? String { + message.thumbnailURLString = URLString + } + + let _ = try? realm.commitWrite() + + doDownloadAttachmentsOfMessage(message) + } + } + }) + + } else { + doDownloadAttachmentsOfMessage(message) + } } else { dispatch_async(dispatch_get_main_queue()) { diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index a67141c2..9b61e22c 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -2004,7 +2004,7 @@ func unreadMessages(failureHandler failureHandler: ((Reason, String?) -> Void)?, let parse: JSONDictionary -> [JSONDictionary]? = { data in - println("unreadMessages data: \(data)") + //println("unreadMessages data: \(data)") guard let conversationsData = data["conversations"] as? [JSONDictionary] else { return nil @@ -2755,7 +2755,7 @@ func refreshAttachmentWithID(attachmentID: String, failureHandler: ((Reason, Str ] let parse: JSONDictionary -> JSONDictionary? = { data in - return data + return (data["attachments"] as? [JSONDictionary])?.first } let resource = authJsonResource(path: "/v1/attachments/refresh_url", method: .PATCH, requestParameters: requestParameters, parse: parse) diff --git a/Yep/ViewControllers/Conversations/ConversationsViewController.swift b/Yep/ViewControllers/Conversations/ConversationsViewController.swift index a4563abb..159e986f 100644 --- a/Yep/ViewControllers/Conversations/ConversationsViewController.swift +++ b/Yep/ViewControllers/Conversations/ConversationsViewController.swift @@ -253,11 +253,6 @@ class ConversationsViewController: SegueViewController { } isFirstAppear = false - - // test - refreshAttachmentWithID("e6b3cc469c7888a0f75d7f45ba35f6e8", failureHandler: nil, completion: { newAttachmentInfo in - println("newAttachmentInfo: \(newAttachmentInfo)") - }) } private func askForNotification() {