mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Share login state with SingleWebViewActivity (#6136)
This commit is contained in:
parent
16ac08fe21
commit
1f33926ed5
2 changed files with 31 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.webkit.ConsoleMessage
|
||||
import android.webkit.CookieManager
|
||||
import android.webkit.WebChromeClient
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
|
|
@ -28,13 +29,20 @@ import androidx.compose.runtime.remember
|
|||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.viewinterop.AndroidView
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.di.ApplicationlessInjection
|
||||
import fr.free.nrw.commons.wikidata.cookies.CommonsCookieJar
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrl
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* SingleWebViewActivity is a reusable activity webView based on a given url(initial url) and
|
||||
* closes itself when a specified success URL is reached to success url.
|
||||
*/
|
||||
class SingleWebViewActivity : ComponentActivity() {
|
||||
@Inject
|
||||
lateinit var cookieJar: CommonsCookieJar
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
|
@ -44,6 +52,11 @@ class SingleWebViewActivity : ComponentActivity() {
|
|||
finish()
|
||||
return
|
||||
}
|
||||
ApplicationlessInjection
|
||||
.getInstance(applicationContext)
|
||||
.commonsApplicationComponent
|
||||
.inject(this)
|
||||
setCookies(url)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
Scaffold(
|
||||
|
|
@ -131,6 +144,7 @@ class SingleWebViewActivity : ComponentActivity() {
|
|||
|
||||
override fun onPageFinished(view: WebView?, url: String?) {
|
||||
super.onPageFinished(view, url)
|
||||
setCookies(url.orEmpty())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -152,6 +166,20 @@ class SingleWebViewActivity : ComponentActivity() {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets cookies for the given URL using the cookies stored in the `CommonsCookieJar`.
|
||||
*
|
||||
* @param url The URL for which cookies need to be set.
|
||||
*/
|
||||
private fun setCookies(url: String) {
|
||||
CookieManager.getInstance().let {
|
||||
val cookies = cookieJar.loadForRequest(url.toHttpUrl())
|
||||
for (cookie in cookies) {
|
||||
it.setCookie(url, cookie.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val VANISH_ACCOUNT_URL = "VanishAccountUrl"
|
||||
private const val VANISH_ACCOUNT_SUCCESS_URL = "vanishAccountSuccessUrl"
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import dagger.android.AndroidInjectionModule
|
|||
import dagger.android.AndroidInjector
|
||||
import dagger.android.support.AndroidSupportInjectionModule
|
||||
import fr.free.nrw.commons.CommonsApplication
|
||||
import fr.free.nrw.commons.activity.SingleWebViewActivity
|
||||
import fr.free.nrw.commons.auth.LoginActivity
|
||||
import fr.free.nrw.commons.contributions.ContributionsModule
|
||||
import fr.free.nrw.commons.explore.SearchModule
|
||||
|
|
@ -51,6 +52,8 @@ interface CommonsApplicationComponent : AndroidInjector<ApplicationlessInjection
|
|||
|
||||
fun inject(activity: LoginActivity)
|
||||
|
||||
fun inject(activity: SingleWebViewActivity)
|
||||
|
||||
fun inject(fragment: SettingsFragment)
|
||||
|
||||
fun inject(fragment: MoreBottomSheetFragment)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue