From d00434c0c4de15d13aab2df0a43836f8e954aa6e Mon Sep 17 00:00:00 2001 From: bekannax Date: Thu, 21 Nov 2024 17:15:50 +0700 Subject: [PATCH 01/11] 0.1.9 --- OnlineChatSdk/Classes/ChatController.swift | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index fd07341..34964d5 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -10,6 +10,7 @@ import UIKit import WebKit import AVFoundation +@available(iOS 13.0, *) open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler { public static let event_operatorSendMessage = "operatorSendMessage" @@ -37,6 +38,8 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa private var widgetUrl: String = "" private var widgetOrg: String = "" private var css: String = "" + private let activityIndicator = UIActivityIndicatorView(style: .large) + private static func getUnreadedMessagesCallback(_ result: NSDictionary) -> NSDictionary { let resultWrapper = ChatApiMessagesWrapper(result) @@ -151,9 +154,54 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa } chatView = WKWebView(frame: frame, configuration: config) chatView.navigationDelegate = self + + chatView.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + chatView.topAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.topAnchor), + chatView.leadingAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.leadingAnchor), + chatView.trailingAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.trailingAnchor), + chatView.bottomAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.bottomAnchor) + ]) + +// // Настройки Activity Indicator + activityIndicator.hidesWhenStopped = true + chatView.addSubview(activityIndicator) + activityIndicator.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + activityIndicator.centerXAnchor.constraint(equalTo: chatView.centerXAnchor), + activityIndicator.centerYAnchor.constraint(equalTo: chatView.centerYAnchor) + ]) + view = chatView } + public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { + activityIndicator.startAnimating() + } + + public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { + activityIndicator.stopAnimating() + showMessage(error.localizedDescription) + } + + public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: any Error) { + activityIndicator.stopAnimating() + showMessage(error.localizedDescription) + + } + + private func showMessage(_ message: String) { + let alert = UIAlertController( + title: nil, + message: message, + preferredStyle: .alert + ) + alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in + self.onCloseSupport() + }) + present(alert, animated: true, completion: nil) + } + public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { didFinish = true if callJs != nil && !callJs.isEmpty { @@ -162,6 +210,9 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa } callJs = nil } +// print("Завершение загрузки страницы.") + activityIndicator.stopAnimating() + } public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> ()) { From 41ff2bd89aca2b51452e5934c0dd5aa7e2b8cb58 Mon Sep 17 00:00:00 2001 From: bekannax Date: Thu, 21 Nov 2024 17:36:28 +0700 Subject: [PATCH 02/11] 0.2.0 --- OnlineChatSdk/Classes/ChatController.swift | 4 ++-- OnlineChatSdk/Classes/ChatDateFormatter.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index 34964d5..fa60993 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -7,7 +7,7 @@ // import UIKit -import WebKit +@preconcurrency import WebKit import AVFoundation @available(iOS 13.0, *) @@ -514,7 +514,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa } open func onLinkPressed(url: URL) { - UIApplication.shared.openURL(url) + UIApplication.shared.open(url, options: [:], completionHandler: nil) } open func playSound(_ systemSoundId: SystemSoundID) { diff --git a/OnlineChatSdk/Classes/ChatDateFormatter.swift b/OnlineChatSdk/Classes/ChatDateFormatter.swift index b8e4d05..ad0aa9e 100644 --- a/OnlineChatSdk/Classes/ChatDateFormatter.swift +++ b/OnlineChatSdk/Classes/ChatDateFormatter.swift @@ -1,6 +1,6 @@ import Foundation -class ChatDateFormatter : DateFormatter { +class ChatDateFormatter : DateFormatter, @unchecked Sendable { override init() { super.init() From 5d411e60c8b5dae60d19a689a42f7189fcbd49ee Mon Sep 17 00:00:00 2001 From: bekannax Date: Tue, 3 Dec 2024 19:47:04 +0700 Subject: [PATCH 03/11] 0.2.1 --- OnlineChatSdk/Classes/ChatController.swift | 81 +++++++++++++++------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index fa60993..30a325a 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -38,8 +38,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa private var widgetUrl: String = "" private var widgetOrg: String = "" private var css: String = "" - private let activityIndicator = UIActivityIndicatorView(style: .large) - + private var alertLoading: UIAlertController? private static func getUnreadedMessagesCallback(_ result: NSDictionary) -> NSDictionary { let resultWrapper = ChatApiMessagesWrapper(result) @@ -155,37 +154,68 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa chatView = WKWebView(frame: frame, configuration: config) chatView.navigationDelegate = self - chatView.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - chatView.topAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.topAnchor), - chatView.leadingAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.leadingAnchor), - chatView.trailingAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.trailingAnchor), - chatView.bottomAnchor.constraint(equalTo: chatView.safeAreaLayoutGuide.bottomAnchor) - ]) - -// // Настройки Activity Indicator - activityIndicator.hidesWhenStopped = true - chatView.addSubview(activityIndicator) - activityIndicator.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - activityIndicator.centerXAnchor.constraint(equalTo: chatView.centerXAnchor), - activityIndicator.centerYAnchor.constraint(equalTo: chatView.centerYAnchor) - ]) - view = chatView } + + private func getAlertLoadingActionCloseTitle() -> String { + let currentLanguage = Locale.current.languageCode + if currentLanguage == "ru" { + return "Закрыть" + } + return "Close" + } + + private func showLoadingDialog() { + if alertLoading != nil { + return + } + alertLoading = UIAlertController( + title: nil, + message: " ", + preferredStyle: .alert + ) + alertLoading?.addAction(UIAlertAction(title: getAlertLoadingActionCloseTitle(), style: .destructive, handler: cancelLoading)) + + + let loadingIndicator = UIActivityIndicatorView(style: .medium) + loadingIndicator.translatesAutoresizingMaskIntoConstraints = false + loadingIndicator.startAnimating() + + alertLoading?.view.addSubview(loadingIndicator) + + NSLayoutConstraint.activate([ + loadingIndicator.centerXAnchor.constraint(equalTo: alertLoading!.view.centerXAnchor), + loadingIndicator.bottomAnchor.constraint(equalTo: alertLoading!.view.bottomAnchor, constant: -60) + ]) + present(alertLoading!, animated: true) + } + + private func cancelLoading(action: UIAlertAction) { + onCloseSupport() + } + + private func hideLoadingDialog() { + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + if self.alertLoading == nil { + return + } + self.alertLoading?.dismiss(animated: true, completion: nil) + self.alertLoading = nil + } + } + public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { - activityIndicator.startAnimating() + showLoadingDialog() } public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { - activityIndicator.stopAnimating() + hideLoadingDialog() showMessage(error.localizedDescription) - } + } public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: any Error) { - activityIndicator.stopAnimating() + hideLoadingDialog() showMessage(error.localizedDescription) } @@ -210,9 +240,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa } callJs = nil } -// print("Завершение загрузки страницы.") - activityIndicator.stopAnimating() - +// hideLoadingDialog() } public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> ()) { @@ -438,6 +466,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa } public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { + hideLoadingDialog() if message.name != "chatInterface" { return } From c1223c6acc79ccb96039e5eab8ad00dddfece4b3 Mon Sep 17 00:00:00 2001 From: bekannax Date: Tue, 3 Dec 2024 19:49:44 +0700 Subject: [PATCH 04/11] 0.2.1 --- OnlineChatSdk/Classes/ChatController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index 30a325a..52ed99a 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -31,7 +31,8 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa public static let method_getContacts = "getContacts" private static let method_destroy = "destroy" private static let method_pageLoaded = "pageLoaded" - + + public var chatView: WKWebView! private var callJs: Array! private var didFinish: Bool = false @@ -40,6 +41,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa private var css: String = "" private var alertLoading: UIAlertController? + private static func getUnreadedMessagesCallback(_ result: NSDictionary) -> NSDictionary { let resultWrapper = ChatApiMessagesWrapper(result) if resultWrapper.getMessages().count == 0 { From 0338a14762cb0a41024694944b1c62c3a492e62c Mon Sep 17 00:00:00 2001 From: bekannax Date: Tue, 3 Dec 2024 23:35:10 +0700 Subject: [PATCH 05/11] 0.2.2 --- OnlineChatSdk/Classes/ChatController.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index 52ed99a..1d0d28a 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -32,7 +32,6 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa private static let method_destroy = "destroy" private static let method_pageLoaded = "pageLoaded" - public var chatView: WKWebView! private var callJs: Array! private var didFinish: Bool = false @@ -41,7 +40,6 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa private var css: String = "" private var alertLoading: UIAlertController? - private static func getUnreadedMessagesCallback(_ result: NSDictionary) -> NSDictionary { let resultWrapper = ChatApiMessagesWrapper(result) if resultWrapper.getMessages().count == 0 { From bea9cbcb7a4f743f80af59d2c1f5575ba31038fb Mon Sep 17 00:00:00 2001 From: bekannax Date: Wed, 15 Jan 2025 18:40:59 +0700 Subject: [PATCH 06/11] 0.2.3 --- OnlineChatSdk/Classes/ChatController.swift | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index 1d0d28a..42e0aa5 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -217,7 +217,6 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: any Error) { hideLoadingDialog() showMessage(error.localizedDescription) - } private func showMessage(_ message: String) { @@ -244,9 +243,6 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa } public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> ()) { -// print("widgetUrl = \(self.widgetUrl)") -// print("widgetOrg = \(self.widgetOrg)") -// print("absoluteString = \(navigationAction.request.url?.absoluteString)") if let _ = navigationAction.request.url?.host { if (navigationAction.request.url?.absoluteString.contains(self.widgetOrg))! { decisionHandler(.allow) @@ -528,18 +524,17 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa if chatView == nil { return } + chatView.stopLoading() + callJsDestroy() + chatView = nil + dismiss(animated: true, completion: nil) navigationController?.popViewController(animated: true) } open override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) - - if animated && chatView != nil { - chatView.stopLoading() - callJsDestroy() - chatView = nil - } + onCloseSupport() } open func onLinkPressed(url: URL) { From b99ac0c086487d5cc98295cf5a38555ad8f61c53 Mon Sep 17 00:00:00 2001 From: bekannax Date: Thu, 6 Feb 2025 12:24:38 +0700 Subject: [PATCH 07/11] 0.2.4 --- OnlineChatSdk/Classes/ChatApi.swift | 9 +++++++ OnlineChatSdk/Classes/ChatController.swift | 29 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/OnlineChatSdk/Classes/ChatApi.swift b/OnlineChatSdk/Classes/ChatApi.swift index 03ac8cc..be4118e 100644 --- a/OnlineChatSdk/Classes/ChatApi.swift +++ b/OnlineChatSdk/Classes/ChatApi.swift @@ -34,6 +34,11 @@ open class ChatApi { } } + public func setInfo(_ token: String, params: Dictionary, callback: @escaping (NSDictionary?) -> Void) { + send(token, "chat/client/setInfo", params, callback: callback) + } + + public func messages(_ token: String, params: Dictionary, callback: @escaping (NSDictionary?) -> Void) { send(token, "chat/message/getList", params, callback: callback) } @@ -53,4 +58,8 @@ open class ChatApi { ] as [String : Any] (ChatApi()).messages(token, params: params, callback: callback) } + + public static func setInfo(_ token: String, _ params: Dictionary, callback: @escaping (NSDictionary?) -> Void) { + (ChatApi()).setInfo(token, params: params, callback: callback) + } } diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index 42e0aa5..44124e7 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -134,6 +134,23 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa callback( ChatController.getNewMessagesCallback(result!) ) }) } + + public static func setInfoCustomDataValue(key: String, value: String, callback: @escaping (NSDictionary?) -> Void) { + DispatchQueue.global().async { + ChatApi.setInfo( + ChatConfig.getApiToken(), + [ + "client": [ + "id": ChatConfig.getClientId(), + "customData": [ + key: value + ] + ], + ] as [String : Any], + callback: callback + ) + } + } override public func loadView() { let contentController = WKUserContentController() @@ -288,6 +305,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa } private func callJs(_ script: String) { + print("callJs : \(script)") chatView.evaluateJavaScript(script) } @@ -487,6 +505,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa switch name { case ChatController.method_pageLoaded: injectCss(style: self.css) + onChatWasOpen() break case ChatController.event_closeSupport: onCloseSupport() @@ -520,6 +539,14 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa onEvent(name, data!) } + open func onChatWasOpen() { + + } + + open func onChatWasClosed() { + + } + open func onCloseSupport() { if chatView == nil { return @@ -530,6 +557,8 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa dismiss(animated: true, completion: nil) navigationController?.popViewController(animated: true) + + onChatWasClosed() } open override func viewDidDisappear(_ animated: Bool) { From b74e63c0ca0814a94d6f91be98da3730854d1c4e Mon Sep 17 00:00:00 2001 From: bekannax Date: Thu, 6 Feb 2025 12:34:01 +0700 Subject: [PATCH 08/11] 0.2.4 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 34cff75..e3d0c04 100644 --- a/README.md +++ b/README.md @@ -154,4 +154,4 @@ ChatController.getNewMessages { data in } ## License -OnlineChatSdk is available under the MIT license. See the LICENSE file for more info. +OnlineChatSdk is available under the MIT license. See the LICENSE file for more info. \ No newline at end of file From e06ff2ca73e6f0167e9611c010441a1fb32e69ff Mon Sep 17 00:00:00 2001 From: bekannax Date: Thu, 6 Feb 2025 18:44:43 +0700 Subject: [PATCH 09/11] 0.2.5 --- OnlineChatSdk/Classes/ChatController.swift | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index 44124e7..d9c886f 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -506,6 +506,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa case ChatController.method_pageLoaded: injectCss(style: self.css) onChatWasOpen() + listenApplicationState() break case ChatController.event_closeSupport: onCloseSupport() @@ -539,6 +540,30 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa onEvent(name, data!) } + private func listenApplicationState() { + NotificationCenter.default.addObserver( + self, + selector: #selector(appDidBecomeActive), + name: UIApplication.didBecomeActiveNotification, + object: nil + ) + NotificationCenter.default.addObserver( + self, + selector: #selector(appWillResignActive), + name: UIApplication.willResignActiveNotification, + object: nil + ) + } + + @objc private func appDidBecomeActive() { + onChatWasOpen() + } + + @objc private func appWillResignActive() { + onChatWasClosed() + } + + open func onChatWasOpen() { } @@ -557,6 +582,7 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa dismiss(animated: true, completion: nil) navigationController?.popViewController(animated: true) + NotificationCenter.default.removeObserver(self) onChatWasClosed() } From 45543cbfa794f984886ce4b1af3010a387f38563 Mon Sep 17 00:00:00 2001 From: bekannax Date: Thu, 6 Feb 2025 18:51:11 +0700 Subject: [PATCH 10/11] 0.2.5 --- OnlineChatSdk/Classes/Command.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OnlineChatSdk/Classes/Command.swift b/OnlineChatSdk/Classes/Command.swift index 20de39c..f0f5c7b 100644 --- a/OnlineChatSdk/Classes/Command.swift +++ b/OnlineChatSdk/Classes/Command.swift @@ -14,4 +14,4 @@ public struct Command { init(_ command: String) { self.command = command } -} +} \ No newline at end of file From 087f6943a89c9faa9d4db962701e0fa53c88e6a3 Mon Sep 17 00:00:00 2001 From: bekannax Date: Thu, 6 Feb 2025 18:52:15 +0700 Subject: [PATCH 11/11] 0.2.5 --- OnlineChatSdk/Classes/ChatController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OnlineChatSdk/Classes/ChatController.swift b/OnlineChatSdk/Classes/ChatController.swift index d9c886f..e6ee082 100644 --- a/OnlineChatSdk/Classes/ChatController.swift +++ b/OnlineChatSdk/Classes/ChatController.swift @@ -631,4 +631,4 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa open func getContactsCallback(_ data: NSDictionary) { } -} +} \ No newline at end of file