diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 4bbb246c..dc1d9f51 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -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"; }; diff --git a/Yep/AppDelegate.swift b/Yep/AppDelegate.swift index 4f9a4ebf..4fe29aaf 100644 --- a/Yep/AppDelegate.swift +++ b/Yep/AppDelegate.swift @@ -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 }) } diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index 22033782..7a31820f 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -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()) { diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 50697fb4..90dad807 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -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 { diff --git a/Yep/Services/YepDownloader.swift b/Yep/Services/YepDownloader.swift index 5a2e5f31..259ea306 100644 --- a/Yep/Services/YepDownloader.swift +++ b/Yep/Services/YepDownloader.swift @@ -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) { diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 57a161d7..9b61e22c 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -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 { diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index 1c565050..e6bad3ee 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -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 }