mirror of
https://github.com/wahyd4/Yep.git
synced 2026-08-19 09:46:56 +10:00
Merge branch 'feature/message_file' into develop
This commit is contained in:
@@ -3015,7 +3015,7 @@
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
OTHER_SWIFT_FLAGS = "-D DEBUG -D STAGING_";
|
||||
OTHER_SWIFT_FLAGS = "-D DEBUG -D STAGING";
|
||||
SDKROOT = iphoneos;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
};
|
||||
|
||||
@@ -43,7 +43,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
let directory: NSURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier(YepConfig.appGroupID)!
|
||||
let realmPath = directory.URLByAppendingPathComponent("db.realm").path!
|
||||
|
||||
return Realm.Configuration(path: realmPath, schemaVersion: 23, migrationBlock: { migration, oldSchemaVersion in
|
||||
return Realm.Configuration(path: realmPath, schemaVersion: 24, migrationBlock: { migration, oldSchemaVersion in
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+82
-36
@@ -181,20 +181,24 @@ class ImageCache {
|
||||
|
||||
if imageDownloadState == MessageDownloadState.Downloaded.rawValue {
|
||||
|
||||
if !fileName.isEmpty {
|
||||
if
|
||||
let imageFileURL = NSFileManager.yepMessageImageURLWithName(fileName),
|
||||
let image = UIImage(contentsOfFile: imageFileURL.path!) {
|
||||
|
||||
let messageImage = image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage()
|
||||
|
||||
self.cache.setObject(messageImage, forKey: imageKey)
|
||||
|
||||
dispatch_async(dispatch_get_main_queue()) {
|
||||
completion(loadingProgress: 1.0, image: messageImage)
|
||||
}
|
||||
|
||||
return
|
||||
if !fileName.isEmpty, let imageFileURL = NSFileManager.yepMessageImageURLWithName(fileName), image = UIImage(contentsOfFile: imageFileURL.path!) {
|
||||
|
||||
let messageImage = image.bubbleImageWithTailDirection(tailDirection, size: size).decodedImage()
|
||||
|
||||
self.cache.setObject(messageImage, forKey: imageKey)
|
||||
|
||||
dispatch_async(dispatch_get_main_queue()) {
|
||||
completion(loadingProgress: 1.0, image: messageImage)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
} else {
|
||||
// 找不到要再给下面的下载机会
|
||||
if let message = messageWithMessageID(messageID, inRealm: realm) {
|
||||
let _ = try? realm.write {
|
||||
message.downloadState = MessageDownloadState.NoDownload.rawValue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,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()) {
|
||||
|
||||
@@ -534,6 +534,8 @@ class Message: Object {
|
||||
dynamic var localAttachmentName: String = ""
|
||||
dynamic var thumbnailURLString: String = ""
|
||||
dynamic var localThumbnailName: String = ""
|
||||
dynamic var attachmentID: String = ""
|
||||
dynamic var attachmentExpiresUnixTime: NSTimeInterval = NSDate().timeIntervalSince1970 + (6 * 60 * 60 * 24) // 6天,过期时间s3为7天,客户端防止误差减去1天
|
||||
|
||||
var nicknameWithTextContent: String {
|
||||
if let nickname = fromFriend?.nickname {
|
||||
|
||||
@@ -18,7 +18,7 @@ class YepDownloader: NSObject {
|
||||
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
|
||||
let session = NSURLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil)
|
||||
return session
|
||||
}()
|
||||
}()
|
||||
|
||||
private class func updateAttachmentOfMessage(message: Message, withAttachmentFileName attachmentFileName: String, inRealm realm: Realm) {
|
||||
|
||||
|
||||
@@ -2004,6 +2004,8 @@ func unreadMessages(failureHandler failureHandler: ((Reason, String?) -> Void)?,
|
||||
|
||||
let parse: JSONDictionary -> [JSONDictionary]? = { data in
|
||||
|
||||
//println("unreadMessages data: \(data)")
|
||||
|
||||
guard let conversationsData = data["conversations"] as? [JSONDictionary] else {
|
||||
return nil
|
||||
}
|
||||
@@ -2746,6 +2748,25 @@ func deleteMessageFromServer(messageID messageID: String, failureHandler: ((Reas
|
||||
}
|
||||
}
|
||||
|
||||
func refreshAttachmentWithID(attachmentID: String, failureHandler: ((Reason, String?) -> Void)?, completion: JSONDictionary -> Void) {
|
||||
|
||||
let requestParameters = [
|
||||
"ids": [attachmentID],
|
||||
]
|
||||
|
||||
let parse: JSONDictionary -> JSONDictionary? = { data in
|
||||
return (data["attachments"] as? [JSONDictionary])?.first
|
||||
}
|
||||
|
||||
let resource = authJsonResource(path: "/v1/attachments/refresh_url", method: .PATCH, requestParameters: requestParameters, parse: parse)
|
||||
|
||||
if let failureHandler = failureHandler {
|
||||
apiRequest({_ in}, baseURL: yepBaseURL, resource: resource, failure: failureHandler, completion: completion)
|
||||
} else {
|
||||
apiRequest({_ in}, baseURL: yepBaseURL, resource: resource, failure: defaultFailureHandler, completion: completion)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Feeds
|
||||
|
||||
enum FeedSortStyle: String {
|
||||
|
||||
@@ -864,20 +864,16 @@ func recordMessageWithMessageID(messageID: String, detailInfo messageInfo: JSOND
|
||||
|
||||
for attachmentInfo in attachments {
|
||||
|
||||
// S3: normal file
|
||||
// if let
|
||||
// normalFileInfo = attachmentInfo["file"] as? JSONDictionary,
|
||||
// fileURLString = normalFileInfo["url"] as? String,
|
||||
// kind = attachmentInfo["kind"] as? String {
|
||||
// if kind == "thumbnail" {
|
||||
// message.thumbnailURLString = fileURLString
|
||||
// } else {
|
||||
// message.attachmentURLString = fileURLString
|
||||
// }
|
||||
// }
|
||||
if let attachmentID = attachmentInfo["id"] as? String {
|
||||
message.attachmentID = attachmentID
|
||||
}
|
||||
|
||||
if let fileInfo = attachmentInfo["file"] as? JSONDictionary {
|
||||
|
||||
if let attachmentExpiresUnixTime = fileInfo["expires_at"] as? NSTimeInterval {
|
||||
message.attachmentExpiresUnixTime = attachmentExpiresUnixTime
|
||||
}
|
||||
|
||||
if let URLString = fileInfo["url"] as? String {
|
||||
message.attachmentURLString = URLString
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user