Merge branch 'feature/message_file' into develop

This commit is contained in:
nixzhu
2016-02-01 17:15:04 +08:00
7 changed files with 115 additions and 50 deletions
+1 -1
View File
@@ -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";
};
+1 -1
View File
@@ -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
View File
@@ -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()) {
+2
View File
@@ -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) // 6s371
var nicknameWithTextContent: String {
if let nickname = fromFriend?.nickname {
+1 -1
View File
@@ -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) {
+21
View File
@@ -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 {
+7 -11
View File
@@ -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
}