check if meIsMemberOfGroup to show subscribeView

This commit is contained in:
nixzhu
2015-12-01 17:45:03 +08:00
parent c75059dd62
commit 6c70ba865c
2 changed files with 69 additions and 43 deletions
+19 -1
View File
@@ -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 = [
@@ -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()
})
}
}