generated from JetBrains/compose-multiplatform-template
-
Notifications
You must be signed in to change notification settings - Fork 41
/
WebViewNavigator.kt
305 lines (272 loc) · 8.81 KB
/
WebViewNavigator.kt
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.multiplatform.webview.web
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import com.multiplatform.webview.request.RequestInterceptor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
* Created By Kevin Zou On 2023/9/5
*/
/**
* Allows control over the navigation of a WebView from outside the composable. E.g. for performing
* a back navigation in response to the user clicking the "up" button in a TopAppBar.
*
* @see [rememberWebViewNavigator]
*/
@Stable
class WebViewNavigator(val coroutineScope: CoroutineScope, val requestInterceptor: RequestInterceptor? = null) {
/**
* Sealed class for constraining possible navigation events.
*/
private sealed interface NavigationEvent {
/**
* Navigate back event.
*/
data object Back : NavigationEvent
/**
* Navigate forward event.
*/
data object Forward : NavigationEvent
/**
* Reload event.
*/
data object Reload : NavigationEvent
/**
* Stop loading event.
*/
data object StopLoading : NavigationEvent
/**
* Load url event.
*/
data class LoadUrl(
val url: String,
val additionalHttpHeaders: Map<String, String> = emptyMap(),
) : NavigationEvent
/**
* Load html event.
*/
data class LoadHtml(
val html: String,
val baseUrl: String? = null,
val mimeType: String? = null,
val encoding: String? = "utf-8",
val historyUrl: String? = null,
) : NavigationEvent
data class LoadHtmlFile(
val fileName: String,
) : NavigationEvent
/**
* Post url event.
*/
data class PostUrl(
val url: String,
val postData: ByteArray,
) : NavigationEvent {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as PostUrl
if (url != other.url) return false
if (!postData.contentEquals(other.postData)) return false
return true
}
override fun hashCode(): Int {
var result = url.hashCode()
result = 31 * result + postData.contentHashCode()
return result
}
}
/**
* Evaluate javascript event.
*/
data class EvaluateJavaScript(
val script: String,
val callback: ((String) -> Unit)?,
) : NavigationEvent
}
/**
* A [MutableSharedFlow] of [NavigationEvent]s that is used to communicate navigation events
* from the composable to the [IWebView].
*/
private val navigationEvents: MutableSharedFlow<NavigationEvent> = MutableSharedFlow(replay = 1)
/**
* Handles navigation events from the composable and calls the appropriate method on the
* [IWebView].
* Use Dispatchers.Main to ensure that the webview methods are called on UI thread
*/
internal suspend fun IWebView.handleNavigationEvents(): Nothing =
withContext(Dispatchers.Main) {
navigationEvents.collect { event ->
when (event) {
is NavigationEvent.Back -> goBack()
is NavigationEvent.Forward -> goForward()
is NavigationEvent.Reload -> reload()
is NavigationEvent.StopLoading -> stopLoading()
is NavigationEvent.LoadHtml ->
loadHtml(
event.html,
event.baseUrl,
event.mimeType,
event.encoding,
event.historyUrl,
)
is NavigationEvent.LoadHtmlFile -> {
loadHtmlFile(event.fileName)
}
is NavigationEvent.LoadUrl -> {
loadUrl(event.url, event.additionalHttpHeaders)
}
is NavigationEvent.PostUrl -> {
postUrl(event.url, event.postData)
}
is NavigationEvent.EvaluateJavaScript -> {
evaluateJavaScript(event.script, event.callback)
}
}
}
}
/**
* True when the web view is able to navigate backwards, false otherwise.
*/
var canGoBack: Boolean by mutableStateOf(false)
internal set
/**
* True when the web view is able to navigate forwards, false otherwise.
*/
var canGoForward: Boolean by mutableStateOf(false)
internal set
/**
* Loads the given URL.
*
* @param url The URL of the resource to load.
*/
fun loadUrl(
url: String,
additionalHttpHeaders: Map<String, String> = emptyMap(),
) {
coroutineScope.launch {
navigationEvents.emit(
NavigationEvent.LoadUrl(
url,
additionalHttpHeaders,
),
)
}
}
/**
* Loads the given HTML string.
*
* @param html The HTML string to load.
* @param baseUrl The URL to use as the page's base URL.
* @param mimeType The MIME type of the data in the string.
* @param encoding The encoding of the data in the string.
* @param historyUrl The history URL for the loaded HTML. Leave null to use about:blank.
*/
fun loadHtml(
html: String,
baseUrl: String? = null,
mimeType: String? = null,
encoding: String? = "utf-8",
historyUrl: String? = null,
) {
coroutineScope.launch {
navigationEvents.emit(
NavigationEvent.LoadHtml(
html,
baseUrl,
mimeType,
encoding,
historyUrl,
),
)
}
}
fun loadHtmlFile(fileName: String) {
coroutineScope.launch {
navigationEvents.emit(
NavigationEvent.LoadHtmlFile(
fileName,
),
)
}
}
/**
* Posts the given data to the given URL.
*
* @param url The URL to post the data to.
* @param postData The data to post.
*/
fun postUrl(
url: String,
postData: ByteArray,
) {
coroutineScope.launch {
navigationEvents.emit(
NavigationEvent.PostUrl(
url,
postData,
),
)
}
}
/**
* Evaluates the given JavaScript in the context of the currently displayed page.
*
* @param script The JavaScript to evaluate.
* @param callback A callback to be invoked when the script execution completes.
*/
fun evaluateJavaScript(
script: String,
callback: ((String) -> Unit)? = null,
) {
coroutineScope.launch {
navigationEvents.emit(
NavigationEvent.EvaluateJavaScript(
script,
callback,
),
)
}
}
/**
* Navigates the webview back to the previous page.
*/
fun navigateBack() {
coroutineScope.launch { navigationEvents.emit(NavigationEvent.Back) }
}
/**
* Navigates the webview forward after going back from a page.
*/
fun navigateForward() {
coroutineScope.launch { navigationEvents.emit(NavigationEvent.Forward) }
}
/**
* Reloads the current page in the webview.
*/
fun reload() {
coroutineScope.launch { navigationEvents.emit(NavigationEvent.Reload) }
}
/**
* Stops the current page load (if one is loading).
*/
fun stopLoading() {
coroutineScope.launch { navigationEvents.emit(NavigationEvent.StopLoading) }
}
}
/**
* Creates and remembers a [WebViewNavigator] using the default [CoroutineScope] or a provided
* override.
*/
@Composable
fun rememberWebViewNavigator(
coroutineScope: CoroutineScope = rememberCoroutineScope(),
requestInterceptor: RequestInterceptor? = null,
): WebViewNavigator = remember(coroutineScope) { WebViewNavigator(coroutineScope, requestInterceptor) }