From 3d9b298879ad843a890633d5719e173f694f61e3 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 1 Feb 2016 14:43:23 +0800 Subject: [PATCH 1/5] better check & reset for message.downloadState --- Yep/Caches/ImageCache.swift | 32 ++++++++++++++++++-------------- Yep/Services/YepDownloader.swift | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index 22033782..798cf35e 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 + } } } } 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) { From 9be3da67204a859de1cd7a5e8e757b2f06bb223d Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 1 Feb 2016 15:32:25 +0800 Subject: [PATCH 2/5] staging; add attachmentExpiresUnixTime for Message --- Yep.xcodeproj/project.pbxproj | 2 +- Yep/AppDelegate.swift | 2 +- Yep/Realm/Models.swift | 1 + Yep/Services/YepService.swift | 2 ++ 4 files changed, 5 insertions(+), 2 deletions(-) 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/Realm/Models.swift b/Yep/Realm/Models.swift index 50697fb4..7a5cca47 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -534,6 +534,7 @@ class Message: Object { dynamic var localAttachmentName: String = "" dynamic var thumbnailURLString: String = "" dynamic var localThumbnailName: 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/YepService.swift b/Yep/Services/YepService.swift index 57a161d7..c769b3fb 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 } From 70b67595b15850ec9051f6c0f37df2658f97dd9b Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 1 Feb 2016 15:40:26 +0800 Subject: [PATCH 3/5] record attachmentID & attachmentExpiresUnixTime --- Yep/Realm/Models.swift | 1 + Yep/Services/YepServiceSync.swift | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 7a5cca47..90dad807 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -534,6 +534,7 @@ 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 { diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index 1c565050..5a860102 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -876,8 +876,16 @@ func recordMessageWithMessageID(messageID: String, detailInfo messageInfo: JSOND // } // } + 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 } From 54e561255541bf84cd44e0eca635c4c8232c0e66 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 1 Feb 2016 16:54:36 +0800 Subject: [PATCH 4/5] add refreshAttachmentWithID --- Yep/Services/YepService.swift | 19 +++++++++++++++++++ Yep/Services/YepServiceSync.swift | 12 ------------ .../ConversationsViewController.swift | 5 +++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index c769b3fb..a67141c2 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -2748,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 + } + + 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 5a860102..e6bad3ee 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -864,18 +864,6 @@ 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 } diff --git a/Yep/ViewControllers/Conversations/ConversationsViewController.swift b/Yep/ViewControllers/Conversations/ConversationsViewController.swift index 159e986f..a4563abb 100644 --- a/Yep/ViewControllers/Conversations/ConversationsViewController.swift +++ b/Yep/ViewControllers/Conversations/ConversationsViewController.swift @@ -253,6 +253,11 @@ class ConversationsViewController: SegueViewController { } isFirstAppear = false + + // test + refreshAttachmentWithID("e6b3cc469c7888a0f75d7f45ba35f6e8", failureHandler: nil, completion: { newAttachmentInfo in + println("newAttachmentInfo: \(newAttachmentInfo)") + }) } private func askForNotification() { From 1702f270c70370e2757dc40eb8edcff4aed25437 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Mon, 1 Feb 2016 17:14:21 +0800 Subject: [PATCH 5/5] check message.attachmentExpiresUnixTime for download --- Yep/Caches/ImageCache.swift | 86 ++++++++++++++----- Yep/Services/YepService.swift | 4 +- .../ConversationsViewController.swift | 5 -- 3 files changed, 66 insertions(+), 29 deletions(-) diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index 798cf35e..7a31820f 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -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()) { diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index a67141c2..9b61e22c 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -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) diff --git a/Yep/ViewControllers/Conversations/ConversationsViewController.swift b/Yep/ViewControllers/Conversations/ConversationsViewController.swift index a4563abb..159e986f 100644 --- a/Yep/ViewControllers/Conversations/ConversationsViewController.swift +++ b/Yep/ViewControllers/Conversations/ConversationsViewController.swift @@ -253,11 +253,6 @@ class ConversationsViewController: SegueViewController { } isFirstAppear = false - - // test - refreshAttachmentWithID("e6b3cc469c7888a0f75d7f45ba35f6e8", failureHandler: nil, completion: { newAttachmentInfo in - println("newAttachmentInfo: \(newAttachmentInfo)") - }) } private func askForNotification() {