Skip to content

Commit ed5ec9d

Browse files
committed
getFeatureConfig message impl
1 parent d5a6441 commit ed5ec9d

File tree

15 files changed

+421
-26
lines changed

15 files changed

+421
-26
lines changed

duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/ui/DuckChatWebViewActivity.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import com.duckduckgo.js.messaging.api.JsMessageCallback
7272
import com.duckduckgo.js.messaging.api.JsMessaging
7373
import com.duckduckgo.navigation.api.GlobalActivityStarter
7474
import com.duckduckgo.navigation.api.getActivityParams
75+
import com.duckduckgo.subscriptions.api.SUBSCRIPTIONS_FEATURE_NAME
7576
import com.google.android.material.snackbar.BaseTransientBottomBar
7677
import com.google.android.material.snackbar.Snackbar
7778
import java.io.File
@@ -101,6 +102,9 @@ open class DuckChatWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
101102
@Inject
102103
lateinit var duckChatJSHelper: DuckChatJSHelper
103104

105+
@Inject
106+
lateinit var subscriptionsHandler: SubscriptionsHandler
107+
104108
@Inject
105109
@AppCoroutineScope
106110
lateinit var appCoroutineScope: CoroutineScope
@@ -240,6 +244,19 @@ open class DuckChatWebViewActivity : DuckDuckGoActivity(), DownloadConfirmationD
240244
}
241245
}
242246
}
247+
248+
SUBSCRIPTIONS_FEATURE_NAME -> {
249+
subscriptionsHandler.handleSubscriptionsFeature(
250+
featureName,
251+
method,
252+
id,
253+
data,
254+
this@DuckChatWebViewActivity,
255+
appCoroutineScope,
256+
contentScopeScripts,
257+
)
258+
}
259+
243260
else -> {}
244261
}
245262
}

duckchat/duckchat-impl/src/main/java/com/duckduckgo/duckchat/impl/ui/DuckChatWebViewFragment.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import com.duckduckgo.duckchat.impl.ui.filechooser.capture.launcher.UploadFromEx
7272
import com.duckduckgo.duckchat.impl.ui.filechooser.capture.launcher.UploadFromExternalMediaAppLauncher.MediaCaptureResult.NoMediaCaptured
7373
import com.duckduckgo.js.messaging.api.JsMessageCallback
7474
import com.duckduckgo.js.messaging.api.JsMessaging
75+
import com.duckduckgo.subscriptions.api.SUBSCRIPTIONS_FEATURE_NAME
7576
import com.google.android.material.snackbar.BaseTransientBottomBar
7677
import com.google.android.material.snackbar.Snackbar
7778
import java.io.File
@@ -96,6 +97,9 @@ open class DuckChatWebViewFragment : DuckDuckGoFragment(R.layout.activity_duck_c
9697
@Inject
9798
lateinit var duckChatJSHelper: DuckChatJSHelper
9899

100+
@Inject
101+
lateinit var subscriptionsHandler: SubscriptionsHandler
102+
99103
@Inject
100104
@AppCoroutineScope
101105
lateinit var appCoroutineScope: CoroutineScope
@@ -236,6 +240,18 @@ open class DuckChatWebViewFragment : DuckDuckGoFragment(R.layout.activity_duck_c
236240
}
237241
}
238242

243+
SUBSCRIPTIONS_FEATURE_NAME -> {
244+
subscriptionsHandler.handleSubscriptionsFeature(
245+
featureName,
246+
method,
247+
id,
248+
data,
249+
requireActivity(),
250+
appCoroutineScope,
251+
contentScopeScripts,
252+
)
253+
}
254+
239255
else -> {}
240256
}
241257
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.duckchat.impl.ui
18+
19+
import android.content.Context
20+
import com.duckduckgo.common.utils.DispatcherProvider
21+
import com.duckduckgo.js.messaging.api.JsMessaging
22+
import com.duckduckgo.navigation.api.GlobalActivityStarter
23+
import com.duckduckgo.subscriptions.api.SubscriptionScreens.RestoreSubscriptionScreenWithParams
24+
import com.duckduckgo.subscriptions.api.SubscriptionScreens.SubscriptionScreenNoParams
25+
import com.duckduckgo.subscriptions.api.SubscriptionScreens.SubscriptionsSettingsScreenWithEmptyParams
26+
import com.duckduckgo.subscriptions.api.SubscriptionsJSHelper
27+
import javax.inject.Inject
28+
import kotlinx.coroutines.CoroutineScope
29+
import kotlinx.coroutines.launch
30+
import kotlinx.coroutines.withContext
31+
import org.json.JSONObject
32+
33+
class SubscriptionsHandler @Inject constructor(
34+
private val subscriptionsJSHelper: SubscriptionsJSHelper,
35+
private val globalActivityStarter: GlobalActivityStarter,
36+
private val dispatcherProvider: DispatcherProvider,
37+
) {
38+
39+
fun handleSubscriptionsFeature(
40+
featureName: String,
41+
method: String,
42+
id: String?,
43+
data: JSONObject?,
44+
context: Context,
45+
appCoroutineScope: CoroutineScope,
46+
contentScopeScripts: JsMessaging,
47+
) {
48+
appCoroutineScope.launch(dispatcherProvider.io()) {
49+
val response = subscriptionsJSHelper.processJsCallbackMessage(featureName, method, id, data)
50+
withContext(dispatcherProvider.main()) {
51+
response?.let {
52+
contentScopeScripts.onResponse(response)
53+
}
54+
}
55+
56+
when (method) {
57+
METHOD_BACK_TO_SETTINGS -> {
58+
appCoroutineScope.launch(dispatcherProvider.io()) {
59+
withContext(dispatcherProvider.main()) {
60+
globalActivityStarter.start(context, SubscriptionsSettingsScreenWithEmptyParams)
61+
}
62+
}
63+
}
64+
65+
METHOD_OPEN_SUBSCRIPTION_ACTIVATION -> {
66+
appCoroutineScope.launch(dispatcherProvider.io()) {
67+
withContext(dispatcherProvider.main()) {
68+
globalActivityStarter.start(context, RestoreSubscriptionScreenWithParams(isOriginWeb = true))
69+
}
70+
}
71+
}
72+
73+
METHOD_OPEN_SUBSCRIPTION_PURCHASE -> {
74+
appCoroutineScope.launch(dispatcherProvider.io()) {
75+
withContext(dispatcherProvider.main()) {
76+
globalActivityStarter.start(context, SubscriptionScreenNoParams)
77+
}
78+
}
79+
}
80+
81+
else -> {}
82+
}
83+
}
84+
}
85+
86+
companion object {
87+
private const val METHOD_BACK_TO_SETTINGS = "backToSettings"
88+
private const val METHOD_OPEN_SUBSCRIPTION_ACTIVATION = "openSubscriptionActivation"
89+
private const val METHOD_OPEN_SUBSCRIPTION_PURCHASE = "openSubscriptionPurchase"
90+
}
91+
}

0 commit comments

Comments
 (0)