mirror of
https://github.com/hub-team/OnlineChatSdk-SwiftPM.git
synced 2026-04-15 09:03:05 +00:00
update
This commit is contained in:
@@ -1,5 +1,61 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class ChatConfig {
|
class ChatConfig {
|
||||||
let defaults = UserDefaults.standard
|
|
||||||
|
private static var instance: ChatConfig?
|
||||||
|
|
||||||
|
private struct ConfigKeys {
|
||||||
|
static let configKeyApiToken = "onlineChatSdkConfig_apiToken"
|
||||||
|
static let configKeyClientId = "onlineChatSdkConfig_clientId"
|
||||||
|
static let configKeyLastDateTimeNewMessage = "onlineChatSdkConfig_lastDateTimeNewMessage"
|
||||||
|
}
|
||||||
|
|
||||||
|
private let config: UserDefaults
|
||||||
|
|
||||||
|
init() {
|
||||||
|
self.config = UserDefaults.standard
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func getInstance() -> ChatConfig {
|
||||||
|
if instance == nil {
|
||||||
|
instance = ChatConfig()
|
||||||
|
}
|
||||||
|
return instance!
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func setLastDateTimeNewMessage(_ dateTime: String) {
|
||||||
|
getInstance().setConfig(ConfigKeys.configKeyLastDateTimeNewMessage, dateTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func getLastDateTimeNewMessage() -> String {
|
||||||
|
getInstance().getConfigString(ConfigKeys.configKeyLastDateTimeNewMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func setClientId(_ clientId: String) {
|
||||||
|
getInstance().setConfig(ConfigKeys.configKeyClientId, clientId)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func getClientId() -> String {
|
||||||
|
getInstance().getConfigString(ConfigKeys.configKeyClientId)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func setApiToken(_ apiToken: String) {
|
||||||
|
getInstance().setConfig(ConfigKeys.configKeyApiToken, apiToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func getApiToken() -> String {
|
||||||
|
getInstance().getConfigString(ConfigKeys.configKeyApiToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setConfig(_ key: String, _ value: String) {
|
||||||
|
self.config.set(value, forKey: key)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setConfig(_ key: String, _ value: Int) {
|
||||||
|
self.config.set(value, forKey: key)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getConfigString(_ key: String) -> String {
|
||||||
|
self.config.value(forKey: key) as! String
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -33,14 +33,59 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
private var didFinish: Bool = false
|
private var didFinish: Bool = false
|
||||||
private var widgetUrl: String = ""
|
private var widgetUrl: String = ""
|
||||||
|
|
||||||
public static func getUnreadedMessages() {
|
private static func getUnreadedMessages(_ startDate: String, callback: @escaping (NSDictionary?) -> Void) {
|
||||||
|
let token = ChatConfig.getApiToken()
|
||||||
|
if token == "" {
|
||||||
|
callback([
|
||||||
|
"success": false,
|
||||||
|
"error": [
|
||||||
|
"code": 0,
|
||||||
|
"descr": "Не задан token"
|
||||||
|
]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
let clientId = ChatConfig.getClientId()
|
||||||
|
if clientId == "" {
|
||||||
|
callback([
|
||||||
|
"success": false,
|
||||||
|
"error": [
|
||||||
|
"code": 0,
|
||||||
|
"descr": "Не задан clientId"
|
||||||
|
]
|
||||||
|
])
|
||||||
|
}
|
||||||
|
let dtFormat = ChatDateFormatter()
|
||||||
|
let currentDate = NSDate() as Date
|
||||||
|
|
||||||
|
ChatApi().messages(token, params: [
|
||||||
|
"client": [
|
||||||
|
"clientId": clientId
|
||||||
|
],
|
||||||
|
"sender": "operator",
|
||||||
|
"status": "unreaded",
|
||||||
|
"dateRange": [
|
||||||
|
"start": startDate,
|
||||||
|
"stop": dtFormat.string(from: currentDate)
|
||||||
|
]
|
||||||
|
] as [String: Any], callback: {(result) in
|
||||||
|
callback([
|
||||||
|
"success": false,
|
||||||
|
"error": [
|
||||||
|
"code": 0,
|
||||||
|
"descr": "Не реализовано"
|
||||||
|
]
|
||||||
|
])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func getUnreadedMessages(callback: @escaping (NSDictionary?) -> Void) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func getNewMessages() {
|
public static func getNewMessages(callback: @escaping (NSDictionary?) -> Void) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override public func loadView() {
|
override public func loadView() {
|
||||||
let contentController = WKUserContentController()
|
let contentController = WKUserContentController()
|
||||||
contentController.add(self, name: "chatInterface")
|
contentController.add(self, name: "chatInterface")
|
||||||
@@ -123,6 +168,10 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func load(_ id: String, _ domain: String, _ language: String = "", _ clientId: String = "", _ apiToken: String = "") {
|
public func load(_ id: String, _ domain: String, _ language: String = "", _ clientId: String = "", _ apiToken: String = "") {
|
||||||
|
if apiToken != "" {
|
||||||
|
ChatConfig.setApiToken(apiToken)
|
||||||
|
}
|
||||||
|
|
||||||
var setup: Dictionary<String, Any> = [:]
|
var setup: Dictionary<String, Any> = [:]
|
||||||
if !language.isEmpty {
|
if !language.isEmpty {
|
||||||
setup["language"] = language
|
setup["language"] = language
|
||||||
@@ -211,7 +260,9 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
let name = body!["name"] as! String
|
let name = body!["name"] as! String
|
||||||
switch name {
|
switch name {
|
||||||
case ChatController.event_clientId:
|
case ChatController.event_clientId:
|
||||||
onClientId(data!["clientId"] != nil ? data!["clientId"] as! String : "")
|
let clientId = data!["clientId"] != nil ? data!["clientId"] as! String : ""
|
||||||
|
ChatConfig.setClientId(clientId)
|
||||||
|
onClientId(clientId)
|
||||||
break
|
break
|
||||||
case ChatController.event_sendRate:
|
case ChatController.event_sendRate:
|
||||||
onSendRate(data!)
|
onSendRate(data!)
|
||||||
@@ -250,7 +301,6 @@ open class ChatController: UIViewController, WKNavigationDelegate, WKScriptMessa
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
open func onSendRate(_ data: NSDictionary) {
|
open func onSendRate(_ data: NSDictionary) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
15
OnlineChatSdk/Classes/ChatDateFormatter.swift
Normal file
15
OnlineChatSdk/Classes/ChatDateFormatter.swift
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
class ChatDateFormatter : DateFormatter {
|
||||||
|
override init() {
|
||||||
|
super.init()
|
||||||
|
self.calendar = Calendar(identifier: Calendar.Identifier.iso8601)
|
||||||
|
self.dateFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss'"
|
||||||
|
self.locale = Locale(identifier: "en")
|
||||||
|
self.timeZone = TimeZone(secondsFromGMT: 10800)
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
fatalError("init(coder:) has not been implemented")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user