From 32e27d3f00a2556012378d421be1f63ed2e68166 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 8 Oct 2015 16:03:25 +0800 Subject: [PATCH] add groupWithGroupID; saveFeedWithFeedData when syncGroupsAndDoFurtherAction; sync full groupInfo when group with feed; now can show group.owner's avatar in ConversationCell if it with group --- Yep/Realm/Models.swift | 17 +++++- Yep/Services/YepService.swift | 15 +++++ Yep/Services/YepServiceSync.swift | 25 ++++++--- .../Feeds/FeedsViewController.swift | 9 --- .../Cells/Conversation/ConversationCell.swift | 56 +++++++++++++++---- 5 files changed, 94 insertions(+), 28 deletions(-) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 672dfb21..c3a51d26 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -545,7 +545,22 @@ func countOfUnreadMessagesInConversation(conversation: Conversation) -> Int { } func saveFeedWithFeedData(feedData: DiscoveredFeed, group: Group, inRealm realm: Realm) { - + + // try sync group first + + groupWithGroupID(groupID: group.groupID, failureHandler: nil, completion: { groupInfo in + + guard let realm = try? Realm() else { + return + } + + println("feed groupInfo: \(groupInfo)") + + syncGroupWithGroupInfo(groupInfo, inRealm: realm) + }) + + // save feed + if let feed = feedWithFeedID(feedData.id, inRealm: realm) { print("Join Feed \(feed.feedID)") diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index de79e943..c808b6ba 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -1258,6 +1258,21 @@ func friendships(completion completion: [JSONDictionary] -> Void) { // MARK: - Groups +func groupWithGroupID(groupID groupID: String, failureHandler: ((Reason, String?) -> Void)?, completion: JSONDictionary -> Void) { + + let parse: JSONDictionary -> JSONDictionary? = { data in + return data + } + + let resource = authJsonResource(path: "/api/v1/circles/\(groupID)", method: .GET, requestParameters: [:], parse: parse) + + if let failureHandler = failureHandler { + apiRequest({_ in}, baseURL: baseURL, resource: resource, failure: failureHandler, completion: completion) + } else { + apiRequest({_ in}, baseURL: baseURL, resource: resource, failure: defaultFailureHandler, completion: completion) + } +} + func joinGroup(groupID groupID: String, failureHandler: ((Reason, String?) -> Void)?, completion: JSONDictionary -> Void) { let parse: JSONDictionary -> JSONDictionary? = { data in diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index c5594a11..92f98a7c 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -519,7 +519,17 @@ func syncGroupsAndDoFurtherAction(furtherAction: () -> Void) { // 增加本地没有的 Group for groupInfo in allGroups { - syncGroupWithGroupInfo(groupInfo, inRealm: realm) + + let group = syncGroupWithGroupInfo(groupInfo, inRealm: realm) + + //Sync Feed + + if let + topic = groupInfo["topic"] as? JSONDictionary, + feedData = DiscoveredFeed.fromJSONDictionary(topic), + group = group { + saveFeedWithFeedData(feedData, group: group, inRealm: realm) + } } // do further action @@ -529,7 +539,8 @@ func syncGroupsAndDoFurtherAction(furtherAction: () -> Void) { } } -private func syncGroupWithGroupInfo(groupInfo: JSONDictionary, inRealm realm: Realm) { +func syncGroupWithGroupInfo(groupInfo: JSONDictionary, inRealm realm: Realm) -> Group? { + if let groupID = groupInfo["id"] as? String { var group = groupWithGroupID(groupID, inRealm: realm) @@ -604,12 +615,6 @@ private func syncGroupWithGroupInfo(groupInfo: JSONDictionary, inRealm realm: Re } } } - - //Sync Feed - - if let topic = groupInfo["topic"] as? JSONDictionary, feedData = DiscoveredFeed.fromJSONDictionary(topic) { - saveFeedWithFeedData(feedData, group: group, inRealm: realm) - } // 同步 Group 的成员 @@ -683,7 +688,11 @@ private func syncGroupWithGroupInfo(groupInfo: JSONDictionary, inRealm realm: Re } } } + + return group } + + return nil } var isFetchingUnreadMessages = Listenable(false) { _ in } diff --git a/Yep/ViewControllers/Feeds/FeedsViewController.swift b/Yep/ViewControllers/Feeds/FeedsViewController.swift index 03cdef22..fc9493b5 100644 --- a/Yep/ViewControllers/Feeds/FeedsViewController.swift +++ b/Yep/ViewControllers/Feeds/FeedsViewController.swift @@ -106,15 +106,6 @@ class FeedsViewController: UIViewController { let newGroup = Group() newGroup.groupID = groupID - // TOOD: newGroup of Feed - /* - if let groupInfo = messageInfo["circle"] as? JSONDictionary { - if let groupName = groupInfo["name"] as? String { - newGroup.groupName = groupName - } - } - */ - realm.write { realm.add(newGroup) } diff --git a/Yep/Views/Cells/Conversation/ConversationCell.swift b/Yep/Views/Cells/Conversation/ConversationCell.swift index fc311dfc..bec2bd4f 100644 --- a/Yep/Views/Cells/Conversation/ConversationCell.swift +++ b/Yep/Views/Cells/Conversation/ConversationCell.swift @@ -113,28 +113,64 @@ class ConversationCell: UITableViewCell { self.chatLabel.text = NSLocalizedString("No messages yet.", comment: "") self.timeAgoLabel.text = NSDate(timeIntervalSince1970: group.createdUnixTime).timeAgo } - + + if let user = group.owner { + AvatarCache.sharedInstance.roundAvatarOfUser(user, withRadius: radius, completion: {[weak self] image in + dispatch_async(dispatch_get_main_queue()) { + if let _ = tableView.cellForRowAtIndexPath(indexPath) { + self?.avatarImageView.image = image + } + } + }) + } + + /* if let feed = group.withFeed { - + + if let user = feed.creator { + AvatarCache.sharedInstance.roundAvatarOfUser(user, withRadius: radius, completion: {[weak self] image in + dispatch_async(dispatch_get_main_queue()) { + if let _ = tableView.cellForRowAtIndexPath(indexPath) { + self?.avatarImageView.image = image + } + } + }) + } + + /* if let URL = feed.attachments.first?.URLString { - AvatarCache.sharedInstance.roundAvatarWithAvatarURLString(URL, withRadius: 30.0, completion: {[weak self] (image) -> Void in - self?.avatarImageView.image = image + AvatarCache.sharedInstance.roundAvatarWithAvatarURLString(URL, withRadius: radius, completion: {[weak self] (image) -> Void in + dispatch_async(dispatch_get_main_queue()) { + if let _ = tableView.cellForRowAtIndexPath(indexPath) { + self?.avatarImageView.image = image + } + } }) } else { - if let avatarURL = feed.creator?.avatarURLString { - AvatarCache.sharedInstance.roundAvatarWithAvatarURLString(avatarURL, withRadius: 30.0, completion: {[weak self] (image) -> Void in - self?.avatarImageView.image = image + if let user = feed.creator { + AvatarCache.sharedInstance.roundAvatarOfUser(user, withRadius: radius, completion: {[weak self] image in + dispatch_async(dispatch_get_main_queue()) { + if let _ = tableView.cellForRowAtIndexPath(indexPath) { + self?.avatarImageView.image = image + } + } }) } } - + */ + } else { if let avatarURL = group.owner?.avatarURLString { - AvatarCache.sharedInstance.roundAvatarWithAvatarURLString(avatarURL, withRadius: 30.0, completion: {[weak self] (image) -> Void in - self?.avatarImageView.image = image + AvatarCache.sharedInstance.roundAvatarWithAvatarURLString(avatarURL, withRadius: radius, completion: {[weak self] image in + dispatch_async(dispatch_get_main_queue()) { + if let _ = tableView.cellForRowAtIndexPath(indexPath) { + self?.avatarImageView.image = image + } + } }) } } + */ } } }