Skip to content

Commit

Permalink
Merge pull request #10 from Teknasyon-Teknoloji/session-timeout
Browse files Browse the repository at this point in the history
[fix] Add session timeout control
  • Loading branch information
osmanyildirim authored Oct 13, 2022
2 parents 50c7297 + 9ffc612 commit b40fec9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Configs/Desk360LiveChat.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.1</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Desk360LiveChat.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Desk360LiveChat"
s.version = "1.0.6"
s.version = "1.0.7"
s.summary = "Desk360 Live Chat provides simplicity and usability in one place. With this feature, you can provide live support to your customers directly within your app just by writing a few lines of code."
s.homepage = "https://github.com/Teknasyon-Teknoloji/desk360-livechat-ios-sdk"
s.license = { :type => "Commercial", :file => "LICENSE" }
Expand Down
4 changes: 4 additions & 0 deletions Sources/Assets/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,8 @@ struct Strings {
lang?.resolve(keyPath: "confirmation_end_conversation_title", orDefault: "") ?? ""
}

static var session_expired: String {
lang?.resolve(keyPath: "session_expired", orDefault: "") ?? ""
}

}
23 changes: 23 additions & 0 deletions Sources/Scenes/Chat/ViewControllers/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ final class ChatViewController: BaseViewController, Layouting, ViewModelIntializ
} failure: { error in
self.layoutableView.agentView.optionsButton.isEnabled = true
Logger.logError(error)
self.viewModel.terminate()
}
}

Expand Down Expand Up @@ -411,6 +412,10 @@ private extension ChatViewController {

@objc func addFile() {
layoutableView.endEditing(true)

if viewModel.getSessionId().isEmpty {
showSessionTimeoutPopup()
} else {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

let showImagePicker = UIAlertAction(title: "\(Strings.sdk_image)/ \(Strings.sdk_video)", style: .default) { _ in
Expand Down Expand Up @@ -446,6 +451,7 @@ private extension ChatViewController {
present(alert, animated: true)
}
}
}

extension String {
var isEmoji: Bool {
Expand Down Expand Up @@ -518,7 +524,24 @@ extension ChatViewController: GrowingTextViewDelegate {
}

func textViewDidChange(_ textView: UITextView) {
if viewModel.getSessionId().isEmpty {
textView.text.removeAll()
resignKeyboard()
showSessionTimeoutPopup()
} else {
chatView.sendButton.isEnabled = isSendButtonEnabled
viewModel.sendTypingEvents()
}
}
}

extension ChatViewController {
private func showSessionTimeoutPopup() {
PopupManager.shared.show(popupType: .info, on: self, title: "", message: Strings.session_expired) { [weak self] in
guard let self = self else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
self.viewModel.terminate()
}
}
}
}
30 changes: 23 additions & 7 deletions Sources/Scenes/Chat/ViewModels/ChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,21 @@ class ChatViewModel {
}

func endChat() -> Future<Void, Error> {
let sessionId = Session.ID
return sessionProvider
.terminate(sessionID: sessionId)
.observe(on: .main)
.map({ _ in
Session.terminate(forceDeleteCreds: false)
self.router?.trigger(.sessionTermination(agent: self.agent, sessionId: sessionId))
let sessionId = getSessionId()

if sessionId.isEmpty {
let promise = Promise<Void, Error>()
promise.fail(error: AnyError(message: ""))
return promise.future
} else {
return sessionProvider
.terminate(sessionID: sessionId)
.observe(on: .main)
.map({ _ in
Session.terminate(forceDeleteCreds: false)
self.router?.trigger(.sessionTermination(agent: self.agent, sessionId: sessionId))
})
}
}

func prepareTranscript() {
Expand All @@ -311,6 +318,15 @@ class ChatViewModel {
func back() {
router?.trigger(.popToRoot)
}

func terminate() {
Session.terminate(forceDeleteCreds: true)
router?.trigger(.popToRoot)
}

func getSessionId() -> String {
Session.ID
}
}

extension Array {
Expand Down

0 comments on commit b40fec9

Please sign in to comment.