mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-09 04:46:35 +10:00
check message.attachmentExpiresUnixTime for download
This commit is contained in:
+64
-22
@@ -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()) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user