-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOSIABWebViewRouterAdapter.kt
More file actions
123 lines (113 loc) · 4.59 KB
/
OSIABWebViewRouterAdapter.kt
File metadata and controls
123 lines (113 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.outsystems.plugins.inappbrowser.osinappbrowserlib.routeradapters
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.OSIABEvents
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.RequiresEventBridgeRegistration
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.helpers.OSIABFlowHelperInterface
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.models.OSIABWebViewOptions
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.views.OSIABWebViewActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.lang.ref.WeakReference
import java.util.UUID
class OSIABWebViewRouterAdapter(
context: Context,
lifecycleScope: CoroutineScope,
options: OSIABWebViewOptions,
flowHelper: OSIABFlowHelperInterface,
onBrowserPageLoaded: () -> Unit,
onBrowserFinished: () -> Unit,
private val onBrowserPageNavigationCompleted: (String?) -> Unit,
private val customHeaders: Map<String, String>? = null
) : OSIABBaseRouterAdapter<OSIABWebViewOptions, Boolean>(
context = context,
lifecycleScope = lifecycleScope,
options = options,
flowHelper = flowHelper,
onBrowserPageLoaded = onBrowserPageLoaded,
onBrowserFinished = onBrowserFinished,
) {
private val browserId = UUID.randomUUID().toString()
companion object {
const val WEB_VIEW_URL_EXTRA = "WEB_VIEW_URL_EXTRA"
const val WEB_VIEW_OPTIONS_EXTRA = "WEB_VIEW_OPTIONS_EXTRA"
const val CUSTOM_HEADERS_EXTRA = "CUSTOM_HEADERS_EXTRA"
}
private var webViewActivityRef: WeakReference<OSIABWebViewActivity>? = null
private fun setWebViewActivity(activity: OSIABWebViewActivity?) {
webViewActivityRef = if (activity == null) {
null
} else {
WeakReference(activity)
}
}
private fun getWebViewActivity(): OSIABWebViewActivity? {
return webViewActivityRef?.get()
}
override fun close(completionHandler: (Boolean) -> Unit) {
getWebViewActivity().let { activity ->
if(activity == null) {
completionHandler(false)
}
else {
activity.finish()
setWebViewActivity(null)
onBrowserFinished()
completionHandler(true)
}
}
}
/**
* Handles opening the passed `url` in the WebView.
* @param url URL to be opened.
* @param completionHandler The callback with the result of opening the url.
*/
@OptIn(RequiresEventBridgeRegistration::class)
override fun handleOpen(url: String, completionHandler: (Boolean) -> Unit) {
lifecycleScope.launch {
try {
// Collect the browser events
OSIABEvents.registerReceiver(context)
var eventsJob: Job? = null
eventsJob = flowHelper.listenToEvents(browserId, lifecycleScope) { event ->
when (event) {
is OSIABEvents.OSIABWebViewEvent -> {
setWebViewActivity(event.activity)
completionHandler(true)
}
is OSIABEvents.BrowserPageLoaded -> {
onBrowserPageLoaded()
}
is OSIABEvents.BrowserFinished -> {
setWebViewActivity(null)
onBrowserFinished()
eventsJob?.cancel()
}
is OSIABEvents.BrowserPageNavigationCompleted -> {
onBrowserPageNavigationCompleted(event.url)
}
else -> {}
}
}
context.startActivity(
Intent(
context, OSIABWebViewActivity::class.java
).apply {
putExtra(OSIABEvents.EXTRA_BROWSER_ID, browserId)
putExtra(WEB_VIEW_URL_EXTRA, url)
putExtra(WEB_VIEW_OPTIONS_EXTRA, options)
putExtra(CUSTOM_HEADERS_EXTRA, Bundle().apply {
customHeaders?.forEach { (key, value) ->
putString(key, value)
}
})
}
)
} catch (e: Exception) {
completionHandler(false)
}
}
}
}