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

This commit is contained in:
nixzhu
2015-10-08 16:03:25 +08:00
committed by nixzhu
parent b4087a08e7
commit 32e27d3f00
5 changed files with 94 additions and 28 deletions
+16 -1
View File
@@ -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)")
+15
View File
@@ -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
+17 -8
View File
@@ -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<Bool>(false) { _ in }
@@ -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)
}
@@ -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
}
}
})
}
}
*/
}
}
}