diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 71887f1d..c2274b02 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -1456,7 +1456,6 @@ func joinGroup(groupID groupID: String, failureHandler: ((Reason, String?) -> Vo } } - func leaveGroup(groupID groupID: String, failureHandler: ((Reason, String?) -> Void)?, completion: () -> Void) { let parse: JSONDictionary -> Void? = { data in @@ -1472,6 +1471,25 @@ func leaveGroup(groupID groupID: String, failureHandler: ((Reason, String?) -> V } } +func meIsMemberOfGroup(groupID groupID: String, failureHandler: ((Reason, String?) -> Void)?, completion: (Bool) -> Void) { + + let parse: JSONDictionary -> Bool? = { data in + + guard let isMember = data["exist"] as? Bool else { + return nil + } + + return isMember + } + + let resource = authJsonResource(path: "/api/v2/circles/\(groupID)/check_me_exist", 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 headGroups(failureHandler failureHandler: ((Reason, String?) -> Void)?, completion: JSONDictionary -> Void) { let requestParameters = [ diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index 9769040d..1a8a0afb 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -798,55 +798,63 @@ class ConversationViewController: BaseViewController { break } - delay(1) { [weak self] in + if let group = conversation.withGroup { - guard self?.conversation.withGroup != nil else { - return - } + let groupID = group.groupID - self?.subscribeView.subscribeAction = { [weak self] in - if let groupID = self?.conversation.withGroup?.groupID { - joinGroup(groupID: groupID, failureHandler: nil, completion: { - println("subscribe OK") + meIsMemberOfGroup(groupID: groupID, failureHandler: nil, completion: { meIsMember in - dispatch_async(dispatch_get_main_queue()) { [weak self] in - if let strongSelf = self { - let _ = try? strongSelf.realm.write { - strongSelf.conversation.withGroup?.includeMe = true + println("meIsMember: \(meIsMember)") + + guard !meIsMember else { + return + } + + delay(1) { [weak self] in + + self?.subscribeView.subscribeAction = { [weak self] in + joinGroup(groupID: groupID, failureHandler: nil, completion: { + println("subscribe OK") + + dispatch_async(dispatch_get_main_queue()) { [weak self] in + if let strongSelf = self { + let _ = try? strongSelf.realm.write { + strongSelf.conversation.withGroup?.includeMe = true + } } } + }) + } + + self?.subscribeView.showWithChangeAction = { [weak self] in + if let strongSelf = self { + + let bottom = strongSelf.view.bounds.height - strongSelf.messageToolbar.frame.origin.y + SubscribeView.height + + let newContentOffsetY = strongSelf.conversationCollectionView.contentSize.height - strongSelf.messageToolbar.frame.origin.y + SubscribeView.height + + self?.tryUpdateConversationCollectionViewWith(newContentInsetBottom: bottom, newContentOffsetY: newContentOffsetY) + + self?.isSubscribeViewShowing = true } - }) + } + + self?.subscribeView.hideWithChangeAction = { [weak self] in + if let strongSelf = self { + + let bottom = strongSelf.view.bounds.height - strongSelf.messageToolbar.frame.origin.y + + let newContentOffsetY = strongSelf.conversationCollectionView.contentSize.height - strongSelf.messageToolbar.frame.origin.y + + self?.tryUpdateConversationCollectionViewWith(newContentInsetBottom: bottom, newContentOffsetY: newContentOffsetY) + + self?.isSubscribeViewShowing = false + } + } + + self?.subscribeView.show() } - } - - self?.subscribeView.showWithChangeAction = { [weak self] in - if let strongSelf = self { - - let bottom = strongSelf.view.bounds.height - strongSelf.messageToolbar.frame.origin.y + SubscribeView.height - - let newContentOffsetY = strongSelf.conversationCollectionView.contentSize.height - strongSelf.messageToolbar.frame.origin.y + SubscribeView.height - - self?.tryUpdateConversationCollectionViewWith(newContentInsetBottom: bottom, newContentOffsetY: newContentOffsetY) - - self?.isSubscribeViewShowing = true - } - } - - self?.subscribeView.hideWithChangeAction = { [weak self] in - if let strongSelf = self { - - let bottom = strongSelf.view.bounds.height - strongSelf.messageToolbar.frame.origin.y - - let newContentOffsetY = strongSelf.conversationCollectionView.contentSize.height - strongSelf.messageToolbar.frame.origin.y - - self?.tryUpdateConversationCollectionViewWith(newContentInsetBottom: bottom, newContentOffsetY: newContentOffsetY) - - self?.isSubscribeViewShowing = false - } - } - - self?.subscribeView.show() + }) } }