Skip to content

Commit 26e7fa3

Browse files
authored
Merge pull request #506 from Kommunicate-io/CM-2452
[CM-2452] Handle Logout, Login, AppID change | iOS SDK
2 parents 934bb26 + b826a97 commit 26e7fa3

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Sources/Kommunicate/Classes/Kommunicate.swift

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ open class Kommunicate: NSObject, Localizable {
137137
case clientConversationIdNotPresent
138138
case conversationOpenFailed
139139
case zendeskKeyNotPresent
140+
case logoutUserFailed
141+
case loginUserFailed
142+
case appIDIsMissing
140143
}
141144

142145
// MARK: - Private properties
@@ -528,6 +531,87 @@ open class Kommunicate: NSObject, Localizable {
528531
}
529532
}
530533

534+
/// This is a Universal function handle login -> conversation building -> conversation launching
535+
/// - Parameter appID: `AppID` is compulsory when the `KMUser` is not passed or If `kmUser` `applicationId` is not present.
536+
/// - Parameter kmUser : A `KMUser` object which contains user details. If `kmUser` is not passed then it will create new Visitor Login everytime.
537+
/// - Parameter viewController: `ViewController` from which the group chat will be launched.
538+
/// - Parameter conversation: An instance of `KMConversation` object.
539+
/// - Parameter shouldMaintainSession: Determines whether to maintain the session when `kmUser` is not provided and a random (visitor) user is used for login.
540+
/// - Parameter completion: If successful the success callback will have a conversationId else it will be `KommunicateError` on failure.
541+
open class func launchConversationWithUser(
542+
appID: String?,
543+
kmUser: KMUser?,
544+
from viewController: UIViewController,
545+
conversation: KMConversation = KMConversationBuilder().build(),
546+
shouldMaintainSession: Bool = true,
547+
completion: @escaping (Result<String, KommunicateError>) -> Void
548+
) {
549+
let user = kmUser ?? createVisitorUser()
550+
let isVisitorUser = (kmUser == nil)
551+
var isAppIDChanged = false
552+
553+
// Derive and validate Application ID
554+
guard let rawAppId = appID ?? user.applicationId,
555+
!rawAppId.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
556+
completion(.failure(.appIDIsMissing))
557+
return
558+
}
559+
560+
let applicationID = rawAppId.trimmingCharacters(in: .whitespacesAndNewlines)
561+
562+
// Setup Application ID if missing or mismatched
563+
if KMUserDefaultHandler.isAppIdEmpty || !KMUserDefaultHandler.matchesCurrentAppId(applicationID) {
564+
KMUserDefaultHandler.setApplicationKey(applicationID)
565+
isAppIDChanged = true
566+
}
567+
setup(applicationId: applicationID)
568+
569+
/// This code will handle the create conversation functionality.
570+
func proceedAfterLogin() {
571+
createConversation(conversation: conversation) { result in
572+
switch result {
573+
case .success(let conversationId):
574+
showConversationWith(groupId: conversationId, from: viewController) { success in
575+
success
576+
? completion(.success(conversationId))
577+
: completion(.failure(.conversationOpenFailed))
578+
}
579+
case .failure:
580+
completion(.failure(.conversationCreateFailed))
581+
}
582+
}
583+
}
584+
585+
/// This code will handle the Login Flow.
586+
func loginAndProceed() {
587+
if isVisitorUser {
588+
Kommunicate.registerUserAsVisitor { _, error in
589+
error != nil
590+
? completion(.failure(.loginUserFailed))
591+
: proceedAfterLogin()
592+
}
593+
} else {
594+
Kommunicate.registerUser(user) { _, error in
595+
error != nil
596+
? completion(.failure(.loginUserFailed))
597+
: proceedAfterLogin()
598+
}
599+
}
600+
}
601+
602+
/// Here the main code execute :
603+
let isSameUser = KMUserDefaultHandler.getUserId() == user.userId
604+
let shouldProceedWithoutLogin = isLoggedIn &&
605+
(isAppIDChanged == false) &&
606+
(isSameUser || (shouldMaintainSession && isVisitorUser))
607+
608+
if shouldProceedWithoutLogin {
609+
proceedAfterLogin()
610+
} else {
611+
loginAndProceed()
612+
}
613+
}
614+
531615
/// Creates a new conversation with the details passed.
532616
/// - Parameter conversation: An instance of `KMConversation` object.
533617
/// - Parameter completion: If successful the success callback will have a conversationId else it will be KMConversationError on failure.

0 commit comments

Comments
 (0)