Skip to content

Commit 258ad5e

Browse files
authored
Merge pull request #20 from verloop/error-handling
Error handling and LogEventListener implementation
2 parents 0aa06b5 + e173d80 commit 258ad5e

11 files changed

Lines changed: 306 additions & 116 deletions

File tree

app/src/main/java/io/verloop/TestActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import android.widget.*
99
import androidx.appcompat.app.AppCompatActivity
1010
import com.google.firebase.messaging.FirebaseMessaging
1111
import io.verloop.sdk.*
12+
import io.verloop.sdk.model.LogEvent
13+
import io.verloop.sdk.model.LogLevel
1214
import org.json.JSONException
1315
import org.json.JSONObject
1416

@@ -124,6 +126,12 @@ class TestActivity : AppCompatActivity() {
124126
.closeExistingChat(checkCloseExistingChat.isChecked)
125127
.fields(customFields).build()
126128

129+
verloopConfig?.setLogEventListener(object : LiveLogEventListener {
130+
override fun logEvent(event: LogEvent) {
131+
Log.i("Log Event", event.toString() )
132+
}
133+
}, LogLevel.DEBUG)
134+
127135
verloopConfig?.setUrlClickListener(object : LiveChatUrlClickListener {
128136
override fun urlClicked(url: String?) {
129137
Toast.makeText(applicationContext, "Chat 1: $url", Toast.LENGTH_SHORT)
@@ -133,6 +141,7 @@ class TestActivity : AppCompatActivity() {
133141
// startActivity(i)
134142
}
135143
}, checkOverrideUrlClick.isChecked)
144+
136145
verloop = Verloop(this, verloopConfig!!)
137146
verloop?.showChat()
138147
} catch (e: VerloopException) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.verloop.sdk
2+
3+
object Constants {
4+
internal const val JS_CALL_SET_USER_PARAMS = "JS_CALL: SetUserParams"
5+
internal const val JS_CALL_SET_USER_ID = "JS_CALL: SetUserId"
6+
internal const val JS_CALL_SET_DEPARTMENT = "JS_CALL: SetDepartment"
7+
internal const val JS_CALL_SET_RECIPE = "JS_CALL: SetRecipe"
8+
internal const val JS_CALL_SET_CUSTOM_FIELD = "JS_CALL: SetCustomField"
9+
internal const val JS_CALL_SET_WIDGET_OPENED = "JS_CALL: WidgetOpened"
10+
internal const val JS_CALL_SET_CLOSE = "JS_CALL: Close"
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.verloop.sdk
2+
3+
import io.verloop.sdk.model.LogEvent
4+
5+
interface LiveLogEventListener {
6+
fun logEvent(event: LogEvent)
7+
}

sdk/src/main/java/io/verloop/sdk/Verloop.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@ import android.content.Context
44
import android.content.Context.MODE_PRIVATE
55
import android.content.Intent
66
import android.content.SharedPreferences
7-
import android.os.Build
87
import android.util.Log
9-
import android.view.View
10-
import android.webkit.JavascriptInterface
11-
import android.webkit.WebSettings
12-
import android.webkit.WebView
138
import androidx.work.*
9+
import io.verloop.sdk.model.LogEvent
1410
import io.verloop.sdk.model.LogoutRequestBody
1511
import io.verloop.sdk.service.LogoutWorker
1612
import io.verloop.sdk.ui.VerloopActivity
1713
import org.json.JSONException
1814
import org.json.JSONObject
1915
import java.util.*
20-
import kotlin.collections.ArrayList
21-
import kotlin.collections.HashMap
2216

2317
class Verloop(val context: Context, var verloopConfig: VerloopConfig) {
2418

@@ -118,7 +112,6 @@ class Verloop(val context: Context, var verloopConfig: VerloopConfig) {
118112
private const val TAG = "VerloopInterface"
119113
}
120114

121-
@JavascriptInterface
122115
@Throws(JSONException::class)
123116
fun onButtonClick(json: String) {
124117
Log.d(TAG, " onButtonClick $json")
@@ -129,13 +122,16 @@ class Verloop(val context: Context, var verloopConfig: VerloopConfig) {
129122
config.buttonOnClickListener?.buttonClicked(title, type, payload);
130123
}
131124

132-
@JavascriptInterface
133125
@Throws(JSONException::class)
134126
fun onURLClick(json: String) {
135127
Log.d(TAG, " onURLClick $json")
136128
val jsonObject = JSONObject(json)
137129
val url = jsonObject.getString("url")
138130
config.chatUrlClickListener?.urlClicked(url)
139131
}
132+
133+
fun onLogEvent(logEvent: LogEvent) {
134+
config.logEventListener?.logEvent(logEvent)
135+
}
140136
}
141137
}

sdk/src/main/java/io/verloop/sdk/VerloopConfig.kt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.verloop.sdk
22

33
import android.os.Parcel
44
import android.os.Parcelable
5+
import io.verloop.sdk.model.LogLevel
56
import java.util.*
67

78
data class VerloopConfig private constructor(
@@ -19,6 +20,14 @@ data class VerloopConfig private constructor(
1920
var fields: ArrayList<CustomField> = ArrayList()
2021
) : Parcelable {
2122

23+
var logLevel: LogLevel = LogLevel.WARNING
24+
25+
var buttonOnClickListener: LiveChatButtonClickListener? = null
26+
var chatUrlClickListener: LiveChatUrlClickListener? = null
27+
28+
var logEventListener: LiveLogEventListener? = null
29+
private set
30+
2231
@Deprecated("Use builder instead")
2332
constructor(clientId: String) : this(
2433
clientId,
@@ -48,16 +57,14 @@ data class VerloopConfig private constructor(
4857
this.userPhone = source.readString()
4958
this.recipeId = source.readString()
5059
this.department = source.readString()
60+
this.logLevel = LogLevel.values()[source.readInt()]
5161
this.isStaging = source.readInt() == 1
5262
this.closeExistingChat = source.readInt() == 1
5363
this.overrideUrlClick = source.readInt() == 1
5464
this.fields =
5565
source.readArrayList(CustomField::class.java.classLoader) as ArrayList<CustomField>
5666
}
5767

58-
var buttonOnClickListener: LiveChatButtonClickListener? = null
59-
var chatUrlClickListener: LiveChatUrlClickListener? = null
60-
6168
fun putCustomField(key: String, value: String, scope: Scope) {
6269
fields.add(CustomField(key, value, scope))
6370
}
@@ -86,6 +93,18 @@ data class VerloopConfig private constructor(
8693
this.overrideUrlClick = overrideUrlClick
8794
}
8895

96+
/**
97+
* Callback for log events from within the chat
98+
* @param logEventListener
99+
*/
100+
fun setLogEventListener(
101+
logEventListener: LiveLogEventListener,
102+
logLevel: LogLevel
103+
) {
104+
this.logEventListener = logEventListener
105+
this.logLevel = logLevel
106+
}
107+
89108
override fun describeContents(): Int {
90109
return 0
91110
}
@@ -99,6 +118,7 @@ data class VerloopConfig private constructor(
99118
dest.writeString(this.userPhone)
100119
dest.writeString(this.recipeId)
101120
dest.writeString(this.department)
121+
dest.writeInt(this.logLevel.ordinal)
102122
dest.writeByte((if (this.isStaging) 1 else 0).toByte())
103123
dest.writeByte((if (this.closeExistingChat) 1 else 0).toByte())
104124
dest.writeByte((if (this.overrideUrlClick) 1 else 0).toByte())
@@ -178,8 +198,10 @@ data class VerloopConfig private constructor(
178198

179199
@Deprecated("Not required anymore", ReplaceWith(""))
180200
fun isStaging(isStaging: Boolean) = apply { this.isStaging = isStaging }
181-
182-
fun closeExistingChat(closeExistingChat: Boolean) = apply { this.closeExistingChat = closeExistingChat }
201+
202+
fun closeExistingChat(closeExistingChat: Boolean) =
203+
apply { this.closeExistingChat = closeExistingChat }
204+
183205
fun overrideUrlClick(overrideUrlClick: Boolean) =
184206
apply { this.overrideUrlClick = overrideUrlClick }
185207

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.verloop.sdk.model
2+
3+
import androidx.annotation.Keep
4+
import org.json.JSONObject
5+
6+
@Keep
7+
data class LogEvent(
8+
var type: String, var message: String, var params: JSONObject? = null
9+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.verloop.sdk.model
2+
3+
enum class LogLevel {
4+
ERROR, WARNING, INFO, DEBUG
5+
}

sdk/src/main/java/io/verloop/sdk/ui/VerloopActivity.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ import io.verloop.sdk.VerloopNotification
2222
import io.verloop.sdk.api.VerloopAPI
2323
import io.verloop.sdk.api.VerloopServiceBuilder.buildService
2424
import io.verloop.sdk.model.ClientInfo
25+
import io.verloop.sdk.model.LogEvent
26+
import io.verloop.sdk.model.LogLevel
2527
import io.verloop.sdk.repository.VerloopRepository
2628
import io.verloop.sdk.utils.CommonUtils
2729
import io.verloop.sdk.viewmodel.MainViewModel
2830
import io.verloop.sdk.viewmodel.MainViewModelFactory
31+
import org.json.JSONObject
2932

3033
class VerloopActivity : AppCompatActivity() {
3134

@@ -55,11 +58,16 @@ class VerloopActivity : AppCompatActivity() {
5558
BlendModeCompat.SRC_ATOP
5659
)
5760

58-
val config: VerloopConfig? = intent.getParcelableExtra("config")
61+
val config = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
62+
intent.getParcelableExtra("config", VerloopConfig::class.java)
63+
} else {
64+
@Suppress("DEPRECATION") intent.getParcelableExtra("config")
65+
}
5966
configKey = intent.getStringExtra("configKey")
6067
this.config = config
6168

6269
if (config != null) {
70+
logEvent(LogLevel.DEBUG, "$TAG:onCreate", null)
6371
val baseUrl =
6472
if (config.isStaging) "https://${config.clientId}.stage.verloop.io"
6573
else "https://${config.clientId}.verloop.io"
@@ -82,20 +90,19 @@ class VerloopActivity : AppCompatActivity() {
8290
}
8391

8492
override fun onResume() {
85-
Log.d(TAG, "onResume")
8693
super.onResume()
8794
setActivityActive(true)
8895
VerloopNotification.cancelNotification(this)
8996
}
9097

9198
override fun onPause() {
92-
Log.d(TAG, "onPause")
9399
super.onPause()
94100
setActivityActive(false)
95101
}
96102

97103
override fun onDestroy() {
98104
Log.d(TAG, "onDestroy")
105+
logEvent(LogLevel.DEBUG, "$TAG:onDestroy", null)
99106
super.onDestroy()
100107
eventListeners.remove(configKey)
101108
}
@@ -111,13 +118,15 @@ class VerloopActivity : AppCompatActivity() {
111118

112119
private fun addFragment() {
113120
Log.d(TAG, "addFragment")
121+
logEvent(LogLevel.DEBUG, "$TAG:addFragment", null)
114122
verloopFragment = VerloopFragment.newInstance(configKey, config)
115123
val ft = supportFragmentManager.beginTransaction()
116124
ft.add(R.id.verloop_layout, verloopFragment, "VerloopActivity#Fragment").commit()
117125
}
118126

119127
private fun updateClientInfo(clientInfo: ClientInfo) {
120128
Log.d(TAG, "updateClientInfo")
129+
logEvent(LogLevel.DEBUG, "$TAG:updateClientInfo", null)
121130
toolbar?.title = clientInfo.title
122131
toolbar?.setBackgroundColor(Color.parseColor(clientInfo.bgColor ?: "#FFFFFF"))
123132
toolbar?.setTitleTextColor(Color.parseColor(CommonUtils.getExpandedColorHex(clientInfo.textColor)))
@@ -130,6 +139,7 @@ class VerloopActivity : AppCompatActivity() {
130139
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
131140
super.onActivityResult(requestCode, resultCode, data)
132141
Log.d(TAG, "onActivityResult")
142+
logEvent(LogLevel.DEBUG, "$TAG:onActivityResult", null)
133143
verloopFragment.fileUploadResult(requestCode, resultCode, data)
134144
}
135145

@@ -142,4 +152,10 @@ class VerloopActivity : AppCompatActivity() {
142152
// Permission not granted. Notifications will be disabled.
143153
}
144154
}
155+
156+
private fun logEvent(level: LogLevel, message: String, params: JSONObject?) {
157+
if (config?.logLevel?.ordinal!! >= level.ordinal) {
158+
viewModel?.logEvent(LogEvent(level.name, message, params))
159+
}
160+
}
145161
}

0 commit comments

Comments
 (0)