check message.attachmentExpiresUnixTime for download

This commit is contained in:
nixzhu
2016-02-01 17:14:21 +08:00
parent 54e5612555
commit 1702f270c7
3 changed files with 66 additions and 29 deletions
+64 -22
View File
@@ -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()) {
+2 -2
View File
@@ -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)
@@ -253,11 +253,6 @@ class ConversationsViewController: SegueViewController {
}
isFirstAppear = false
// test
refreshAttachmentWithID("e6b3cc469c7888a0f75d7f45ba35f6e8", failureHandler: nil, completion: { newAttachmentInfo in
println("newAttachmentInfo: \(newAttachmentInfo)")
})
}
private func askForNotification() {