Skip to content

Commit 1f33926

Browse files
authored
Share login state with SingleWebViewActivity (commons-app#6136)
1 parent 16ac08f commit 1f33926

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

app/src/main/java/fr/free/nrw/commons/activity/SingleWebViewActivity.kt

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.Context
55
import android.content.Intent
66
import android.os.Bundle
77
import android.webkit.ConsoleMessage
8+
import android.webkit.CookieManager
89
import android.webkit.WebChromeClient
910
import android.webkit.WebResourceRequest
1011
import android.webkit.WebView
@@ -28,13 +29,20 @@ import androidx.compose.runtime.remember
2829
import androidx.compose.ui.Modifier
2930
import androidx.compose.ui.viewinterop.AndroidView
3031
import fr.free.nrw.commons.R
32+
import fr.free.nrw.commons.di.ApplicationlessInjection
33+
import fr.free.nrw.commons.wikidata.cookies.CommonsCookieJar
34+
import okhttp3.HttpUrl.Companion.toHttpUrl
3135
import timber.log.Timber
36+
import javax.inject.Inject
3237

3338
/**
3439
* SingleWebViewActivity is a reusable activity webView based on a given url(initial url) and
3540
* closes itself when a specified success URL is reached to success url.
3641
*/
3742
class SingleWebViewActivity : ComponentActivity() {
43+
@Inject
44+
lateinit var cookieJar: CommonsCookieJar
45+
3846
@OptIn(ExperimentalMaterial3Api::class)
3947
override fun onCreate(savedInstanceState: Bundle?) {
4048
super.onCreate(savedInstanceState)
@@ -44,6 +52,11 @@ class SingleWebViewActivity : ComponentActivity() {
4452
finish()
4553
return
4654
}
55+
ApplicationlessInjection
56+
.getInstance(applicationContext)
57+
.commonsApplicationComponent
58+
.inject(this)
59+
setCookies(url)
4760
enableEdgeToEdge()
4861
setContent {
4962
Scaffold(
@@ -131,6 +144,7 @@ class SingleWebViewActivity : ComponentActivity() {
131144

132145
override fun onPageFinished(view: WebView?, url: String?) {
133146
super.onPageFinished(view, url)
147+
setCookies(url.orEmpty())
134148
}
135149

136150
}
@@ -152,6 +166,20 @@ class SingleWebViewActivity : ComponentActivity() {
152166

153167
}
154168

169+
/**
170+
* Sets cookies for the given URL using the cookies stored in the `CommonsCookieJar`.
171+
*
172+
* @param url The URL for which cookies need to be set.
173+
*/
174+
private fun setCookies(url: String) {
175+
CookieManager.getInstance().let {
176+
val cookies = cookieJar.loadForRequest(url.toHttpUrl())
177+
for (cookie in cookies) {
178+
it.setCookie(url, cookie.toString())
179+
}
180+
}
181+
}
182+
155183
companion object {
156184
private const val VANISH_ACCOUNT_URL = "VanishAccountUrl"
157185
private const val VANISH_ACCOUNT_SUCCESS_URL = "vanishAccountSuccessUrl"

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationComponent.kt

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dagger.android.AndroidInjectionModule
66
import dagger.android.AndroidInjector
77
import dagger.android.support.AndroidSupportInjectionModule
88
import fr.free.nrw.commons.CommonsApplication
9+
import fr.free.nrw.commons.activity.SingleWebViewActivity
910
import fr.free.nrw.commons.auth.LoginActivity
1011
import fr.free.nrw.commons.contributions.ContributionsModule
1112
import fr.free.nrw.commons.explore.SearchModule
@@ -51,6 +52,8 @@ interface CommonsApplicationComponent : AndroidInjector<ApplicationlessInjection
5152

5253
fun inject(activity: LoginActivity)
5354

55+
fun inject(activity: SingleWebViewActivity)
56+
5457
fun inject(fragment: SettingsFragment)
5558

5659
fun inject(fragment: MoreBottomSheetFragment)

0 commit comments

Comments
 (0)