Skip to content

Commit

Permalink
override reset, change init types, depreciation flags
Browse files Browse the repository at this point in the history
  • Loading branch information
lujcheng committed Mar 17, 2022
1 parent 5342cda commit 043e335
Showing 1 changed file with 73 additions and 4 deletions.
77 changes: 73 additions & 4 deletions EmbedFramework/AdaWebHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class AdaWebHost: NSObject {

/// Metafields can be passed in during init; use `setMetaFields()` and `setSensitiveMetafields()`
/// to send values in at runtime
private var metafields: [String: String] = [:]
private var sensitiveMetafields: [String: String] = [:]
private var metafields: [String: Any] = [:]
private var sensitiveMetafields: [String: Any] = [:]

public var openWebLinksInSafari = false
public var appScheme = ""
Expand Down Expand Up @@ -71,8 +71,8 @@ public class AdaWebHost: NSObject {
language: String = "",
styles: String = "",
greeting: String = "",
metafields: [String: String] = [:],
sensitiveMetafields: [String:String] = [:],
metafields: [String: Any] = [:],
sensitiveMetafields: [String:Any] = [:],
openWebLinksInSafari: Bool = false,
appScheme: String = "",
zdChatterAuthCallback: (((@escaping (_ token: String) -> Void)) -> Void)? = nil,
Expand Down Expand Up @@ -142,6 +142,7 @@ public class AdaWebHost: NSObject {
// MARK: - Public Methods

/// Push a dictionary of fields to the server
@available(*, deprecated, message: "This method will be deprecated in the future, please upgrade to MetaFields.Builder.", renamed: "setMetaFields(builder:)")
public func setMetaFields(_ fields: [String: Any]) {
guard let json = try? JSONSerialization.data(withJSONObject: fields, options: []),
let jsonString = String(data: json, encoding: .utf8) else { return }
Expand All @@ -151,6 +152,7 @@ public class AdaWebHost: NSObject {
}

/// Push a dictionary of fields to the server
@available(*, deprecated, message: "This method will be deprecated in the future, please upgrade to MetaFields.Builder.", renamed: "setSensitiveMetaFields(builder:)")
public func setSensitiveMetaFields(_ fields: [String: Any]) {
guard let json = try? JSONSerialization.data(withJSONObject: fields, options: []),
let jsonString = String(data: json, encoding: .utf8) else { return }
Expand Down Expand Up @@ -179,6 +181,8 @@ public class AdaWebHost: NSObject {
}

/// Re-initialize chat and optionally reset history, language, meta data, etc
@available(*, deprecated, message: "This method will be deprecated in the future, please upgrade to MetaFields.Builder.", renamed: "reset(metaFields:sensitiveMetaFields:)")
/// When this method is depreciated, the 4 override reset methods should be replaced
public func reset(language: String? = nil, greeting: String? = nil, metaFields: [String: Any]? = nil, sensitiveMetaFields: [String: Any]? = nil, resetChatHistory: Bool? = true) {

let data: [String: Any?] = [
Expand All @@ -196,6 +200,71 @@ public class AdaWebHost: NSObject {
self.evalJS(toRun)
}

public func reset(language: String? = nil, greeting: String? = nil, metaFields: MetaFields.Builder, resetChatHistory: Bool? = true) {

let data: [String: Any?] = [
"language": language,
"greeting": greeting,
"metaFields": metaFields.build().metaFields,
"sensitiveMetaFields": nil,
"resetChatHistory": resetChatHistory
]
guard let json = try? JSONSerialization.data(withJSONObject: data, options: .fragmentsAllowed),
let jsonString = String(data: json, encoding: .utf8) else { return }
let toRun = "adaEmbed.reset(\(jsonString));"

self.evalJS(toRun)
}

public func reset(language: String? = nil, greeting: String? = nil, sensitiveMetaFields: MetaFields.Builder, resetChatHistory: Bool? = true) {

let data: [String: Any?] = [
"language": language,
"greeting": greeting,
"metaFields": nil,
"sensitiveMetaFields": sensitiveMetaFields.build().metaFields,
"resetChatHistory": resetChatHistory
]
guard let json = try? JSONSerialization.data(withJSONObject: data, options: .fragmentsAllowed),
let jsonString = String(data: json, encoding: .utf8) else { return }

let toRun = "adaEmbed.reset(\(jsonString));"

self.evalJS(toRun)
}

public func reset(language: String? = nil, greeting: String? = nil, metaFields: MetaFields.Builder, sensitiveMetaFields: MetaFields.Builder, resetChatHistory: Bool? = true) {

let data: [String: Any?] = [
"language": language,
"greeting": greeting,
"metaFields": metaFields.build().metaFields,
"sensitiveMetaFields": sensitiveMetaFields.build().metaFields,
"resetChatHistory": resetChatHistory
]
guard let json = try? JSONSerialization.data(withJSONObject: data, options: .fragmentsAllowed),
let jsonString = String(data: json, encoding: .utf8) else { return }
let toRun = "adaEmbed.reset(\(jsonString));"

self.evalJS(toRun)
}

public func reset(language: String? = nil, greeting: String? = nil, resetChatHistory: Bool? = true) {

let data: [String: Any?] = [
"language": language,
"greeting": greeting,
"metaFields": nil,
"sensitiveMetaFields": nil,
"resetChatHistory": resetChatHistory
]
guard let json = try? JSONSerialization.data(withJSONObject: data, options: .fragmentsAllowed),
let jsonString = String(data: json, encoding: .utf8) else { return }
let toRun = "adaEmbed.reset(\(jsonString));"

self.evalJS(toRun)
}

/// Re-initialize chat and optionally reset history, language, meta data, etc
public func deleteHistory() {
let toRun = "adaEmbed.deleteHistory();"
Expand Down

0 comments on commit 043e335

Please sign in to comment.