-
Notifications
You must be signed in to change notification settings - Fork 0
MOBILEWEBVIEW-3: Add mindbox webview #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
...ox-common/src/androidMain/kotlin/cloud/mindbox/mobile_sdk/inapp/webview/WebViewAndroid.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.webview | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.graphics.Color | ||
| import android.os.Build | ||
| import android.view.View | ||
| import android.webkit.* | ||
| import cloud.mindbox.mobile_sdk.annotations.InternalMindboxApi | ||
|
|
||
| @InternalMindboxApi | ||
| public actual typealias WebViewPlatformView = View | ||
|
|
||
| @InternalMindboxApi | ||
| public fun WebViewController.Companion.create( | ||
| context: android.content.Context, | ||
| isDebugEnabled: Boolean | ||
| ): WebViewController { | ||
| return AndroidWebViewController(context, isDebugEnabled) | ||
| } | ||
|
|
||
| @OptIn(InternalMindboxApi::class) | ||
| private class AndroidWebViewController( | ||
| context: android.content.Context, | ||
| isDebugEnabled: Boolean | ||
| ) : WebViewController { | ||
|
|
||
| private val webView: WebView = WebView(context) | ||
| private var eventListener: WebViewEventListener? = null | ||
|
|
||
| init { | ||
| WebView.setWebContentsDebuggingEnabled(isDebugEnabled) | ||
| configureWebView() | ||
| webView.webViewClient = createWebViewClient() | ||
| } | ||
|
|
||
| override val view: WebViewPlatformView | ||
| get() = webView | ||
|
|
||
| override fun loadContent(content: WebViewHtmlContent) { | ||
| webView.loadDataWithBaseURL( | ||
| content.baseUrl, | ||
| content.html, | ||
| "text/html", | ||
| "UTF-8", | ||
| null | ||
| ) | ||
| } | ||
|
|
||
| override fun setVisibility(isVisible: Boolean) { | ||
| webView.visibility = if (isVisible) View.VISIBLE else View.INVISIBLE | ||
| } | ||
|
|
||
| override fun setUserAgentSuffix(suffix: String) { | ||
| val currentUserAgent: String = webView.settings.userAgentString ?: "" | ||
| if (currentUserAgent.contains(suffix)) { | ||
| return | ||
| } | ||
| webView.settings.userAgentString = "$currentUserAgent $suffix".trim() | ||
| } | ||
|
|
||
| override fun setJsBridge(bridge: WebViewJsBridge, bridgeName: String) { | ||
| webView.removeJavascriptInterface(bridgeName) | ||
| webView.addJavascriptInterface(AndroidWebViewJsBridge(bridge), bridgeName) | ||
| } | ||
|
|
||
| override fun setEventListener(listener: WebViewEventListener?) { | ||
| eventListener = listener | ||
| } | ||
|
|
||
| override fun executeOnViewThread(action: () -> Unit) { | ||
| webView.post(action) | ||
| } | ||
|
|
||
| override fun destroy() { | ||
| webView.stopLoading() | ||
| webView.loadUrl("about:blank") | ||
| webView.clearHistory() | ||
| webView.removeAllViews() | ||
| webView.destroy() | ||
| } | ||
|
|
||
| @SuppressLint("SetJavaScriptEnabled") | ||
| private fun configureWebView() { | ||
| with(webView.settings) { | ||
| javaScriptEnabled = true | ||
| domStorageEnabled = true | ||
| loadWithOverviewMode = true | ||
| builtInZoomControls = true | ||
| displayZoomControls = false | ||
| defaultTextEncodingName = "utf-8" | ||
| cacheMode = WebSettings.LOAD_NO_CACHE | ||
| allowContentAccess = true | ||
| } | ||
| webView.setBackgroundColor(Color.TRANSPARENT) | ||
| } | ||
|
|
||
| private fun createWebViewClient(): WebViewClient { | ||
| return object : WebViewClient() { | ||
|
|
||
|
|
||
| override fun onReceivedError( | ||
| view: WebView?, | ||
| request: WebResourceRequest?, | ||
| error: WebResourceError? | ||
| ) { | ||
| val webViewError: WebViewError = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
| WebViewError( | ||
| code = error?.errorCode, | ||
| description = error?.description?.toString(), | ||
| url = request?.url?.toString(), | ||
| isForMainFrame = request?.isForMainFrame | ||
| ) | ||
| } else { | ||
| WebViewError( | ||
| code = null, | ||
| description = null, | ||
| url = request?.url?.toString(), | ||
| isForMainFrame = request?.isForMainFrame | ||
| ) | ||
| } | ||
| eventListener?.onError(webViewError) | ||
| } | ||
|
|
||
| @Deprecated("Deprecated in Java") | ||
| @Suppress("DEPRECATION") | ||
| override fun onReceivedError( | ||
| view: WebView?, | ||
| errorCode: Int, | ||
| description: String?, | ||
| failingUrl: String? | ||
| ) { | ||
| val webViewError: WebViewError = WebViewError( | ||
| code = errorCode, | ||
| description = description, | ||
| url = failingUrl, | ||
| isForMainFrame = failingUrl == view?.originalUrl | ||
| ) | ||
| eventListener?.onError(webViewError) | ||
| } | ||
|
|
||
| override fun onPageFinished(view: WebView?, url: String?) { | ||
| eventListener?.onPageFinished(url) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private class AndroidWebViewJsBridge( | ||
| private val bridge: WebViewJsBridge | ||
| ) { | ||
| @JavascriptInterface | ||
| fun receiveParam(key: String): String? { | ||
| return bridge.getParam(key) | ||
| } | ||
|
|
||
| @JavascriptInterface | ||
| fun postMessage(action: String, data: String) { | ||
| bridge.onAction(action, data) | ||
| } | ||
| } | ||
| } | ||
|
|
||
14 changes: 14 additions & 0 deletions
14
...x-common/src/commonMain/kotlin/cloud/mindbox/mobile_sdk/annotations/InternalMindboxApi.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package cloud.mindbox.mobile_sdk.annotations | ||
|
|
||
| @Target( | ||
| AnnotationTarget.CLASS, | ||
| AnnotationTarget.TYPEALIAS, | ||
| AnnotationTarget.FUNCTION, | ||
| AnnotationTarget.PROPERTY | ||
| ) | ||
| @RequiresOptIn( | ||
| message = "Internal API. Use only inside Mindbox SDK.", | ||
| level = RequiresOptIn.Level.WARNING | ||
| ) | ||
| public annotation class InternalMindboxApi | ||
|
enotniy marked this conversation as resolved.
|
||
|
|
||
60 changes: 60 additions & 0 deletions
60
...-common/src/commonMain/kotlin/cloud/mindbox/mobile_sdk/inapp/webview/WebViewInterfaces.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.webview | ||
|
|
||
| import cloud.mindbox.mobile_sdk.annotations.InternalMindboxApi | ||
|
|
||
| @InternalMindboxApi | ||
| public expect class WebViewPlatformView | ||
|
|
||
| @InternalMindboxApi | ||
| public data class WebViewHtmlContent( | ||
| val baseUrl: String, | ||
| val html: String | ||
| ) | ||
|
|
||
| @InternalMindboxApi | ||
| public data class WebViewError( | ||
| val code: Int?, | ||
| val description: String?, | ||
| val url: String?, | ||
| val isForMainFrame: Boolean? | ||
| ) | ||
|
|
||
| @InternalMindboxApi | ||
| public interface WebViewJsBridge { | ||
| public fun getParam(key: String): String? | ||
|
|
||
| public fun onAction(action: String, data: String) { | ||
| } | ||
| } | ||
|
|
||
| @InternalMindboxApi | ||
| public interface WebViewEventListener { | ||
| public fun onPageFinished(url: String?) | ||
|
|
||
| public fun onError(error: WebViewError) { | ||
| } | ||
| } | ||
|
|
||
| @InternalMindboxApi | ||
| public interface WebViewController { | ||
| public val view: WebViewPlatformView | ||
|
|
||
| public fun loadContent(content: WebViewHtmlContent) | ||
|
|
||
| public fun setVisibility(isVisible: Boolean) | ||
|
|
||
| public fun setUserAgentSuffix(suffix: String) | ||
|
|
||
| public fun setJsBridge(bridge: WebViewJsBridge, bridgeName: String = DEFAULT_WEBVIEW_BRIDGE_NAME) | ||
|
|
||
| public fun setEventListener(listener: WebViewEventListener?) | ||
|
|
||
| public fun executeOnViewThread(action: () -> Unit) | ||
|
|
||
| public fun destroy() | ||
|
|
||
| public companion object | ||
| } | ||
|
|
||
| public const val DEFAULT_WEBVIEW_BRIDGE_NAME: String = "SdkBridge" | ||
|
|
6 changes: 6 additions & 0 deletions
6
mindbox-common/src/iosMain/kotlin/cloud/mindbox/mobile_sdk/inapp/webview/WebViewIos.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package cloud.mindbox.mobile_sdk.inapp.webview | ||
|
|
||
| import cloud.mindbox.mobile_sdk.annotations.InternalMindboxApi | ||
|
|
||
| @InternalMindboxApi | ||
| public actual typealias WebViewPlatformView = Any |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Думаешь эти настройки останутся такими же и нам не надо выносить возможность их задавать при инициализации?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
думаю сейчас все оставить внутри. Пока сомнительно, чтобы что-то поменялось