Skip to content

Instantly share code, notes, and snippets.

@alexandrekarst
Last active November 29, 2021 14:13
Show Gist options
  • Save alexandrekarst/74da3ce5a9eaf68f7bd83eaf77c6d3dc to your computer and use it in GitHub Desktop.
Save alexandrekarst/74da3ce5a9eaf68f7bd83eaf77c6d3dc to your computer and use it in GitHub Desktop.
iAdvize SDK - iOS Custom Chat button
class CustomerClass {
func updateChatButton() {
guard IAdvizeSDK.shared.activationStatus == .activated else {
hideChatButton()
return
}
guard !IAdvizeSDK.shared.conversationController.isChatboxPresented() else {
hideChatButton()
return
}
guard IAdvizeSDK.shared.targetingController.hasOngoingConversation ||
IAdvizeSDK.shared.targetingController.isActiveTargetingRuleAvailable else {
hideChatButton()
return
}
print("Showing Chat button.")
// Keep a track that the button was displayed to display it again when the chatbox
// will be dismissed.
displayChatButton = true
// Be sure that the Chat Button isn't already displayed or,
// if it's the case, be sure that we update his superview.
// Otherwise, do nothing.
guard chatButtonView.superview == nil else {
chatButtonView.superview?.bringSubviewToFront(chatButtonView)
return
}
guard let view = UIWindow.key else {
print("Oops. It seems that we can't find any view to display the chat button. Please specify a view or a window when calling `showChatButton()`")
return
}
chatButtonView.removeFromSuperview()
view.addSubview(chatButtonView)
chatButtonView.installConstraints()
}
func iAdvizeLogout() {
IAdvizeSDK.shared.logout()
updateChatButton()
}
func hideChatButton() {
print("Hiding Chat button.")
// Keep a track that the button was hidden to not display it when the chatbox
// will be dismissed.
displayChatButton = false
// When the button is set to be explicitly hidden we remove it from its superview because
// it can be added to another view when shown later.
chatButtonView.removeFromSuperview()
}
}
extension CustomerClass: TargetingControllerSdkDelegate {
func activeTargetingRuleDidUpdate(with id: UUID?) {
updateChatButton()
}
func activeTargetingRuleAvailabilityDidUpdate(isActiveTargetingRuleAvailable: Bool) {
updateChatButton()
}
}
extension CustomerClass: ConversationControllerSdkDelegate {
func ongoingConversationStatusDidChange(hasOngoingConversation: Bool) {
updateChatButton()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment