Skip to content

Commit

Permalink
Add the possibility to specify an url to quit the webview automatical…
Browse files Browse the repository at this point in the history
…ly and let the caller activity know we reach that specific url
  • Loading branch information
tevincent committed Oct 3, 2024
1 parent 8b40df0 commit 0a44cd3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
43 changes: 38 additions & 5 deletions src/main/java/com/infomaniak/lib/core/ui/WebViewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.navArgs
import com.infomaniak.lib.core.databinding.ActivityWebviewBinding
Expand All @@ -38,19 +41,49 @@ class WebViewActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.webview.apply {
webViewClient = WebViewClient()
webViewClient = CustomWebViewClient(navArgs.urlToQuit, onUrlToQuitReached = {
setResult(RESULT_OK)
finish()
})
settings.javaScriptEnabled = true
val headers = navArgs.headers?.let { Json.decodeFromString<Map<String, String>>(it) } ?: mapOf()
loadUrl(navArgs.url, headers)
}
}

private class CustomWebViewClient(
private val urlToQuit: String?,
private val onUrlToQuitReached: () -> Unit,
) : WebViewClient() {

override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest?): Boolean {
return if (urlToQuit != null && request?.url?.toString()?.contains(urlToQuit) == true) {
onUrlToQuitReached()
true
} else {
super.shouldOverrideUrlLoading(view, request)
}
}
}

companion object {

fun startActivity(context: Context, url: String, headers: Map<String, String>? = mapOf()) {
Intent(context, WebViewActivity::class.java).apply {
putExtras(WebViewActivityArgs(url, Json.encodeToString(headers)).toBundle())
}.also(context::startActivity)
fun startActivity(
context: Context,
url: String,
headers: Map<String, String>? = mapOf(),
urlToQuit: String? = null,
activityResultLauncher: ActivityResultLauncher<Intent>? = null,
) {
val intent = Intent(context, WebViewActivity::class.java).apply {
val webViewActivityArgs = WebViewActivityArgs(url, Json.encodeToString(headers), urlToQuit)
putExtras(webViewActivityArgs.toBundle())
}
activityResultLauncher?.let {
activityResultLauncher.launch(intent)
} ?: run {
context.startActivity(intent)
}
}
}
}
7 changes: 6 additions & 1 deletion src/main/res/navigation/webview_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@

<argument
android:name="url"
app:argType="string" />
app:argType="string"/>

<argument
android:name="headers"
app:argType="string"
app:nullable="true"/>

<argument
android:name="urlToQuit"
app:argType="string"
app:nullable="true"/>

</activity>

</navigation>

0 comments on commit 0a44cd3

Please sign in to comment.