Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ android {
applicationId = "ltd.grunt.brainwallet"
minSdk = 29
targetSdk = 35
versionCode = 202506333
versionName = "v4.9.2"
versionCode = 202506335
versionName = "v4.9.3"
multiDexEnabled = true
base.archivesName.set("${defaultConfig.versionName}(${defaultConfig.versionCode})")
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package com.brainwallet.ui.bentosections.shopbento
sealed class ShopBentoEvent {
data object OnLoad : ShopBentoEvent()
data object OnTapShop : ShopBentoEvent()

object InvoiceCreated : ShopBentoEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.koin.android.annotation.KoinViewModel
import java.util.Locale
import com.brainwallet.data.repository.SettingRepository
import com.brainwallet.data.repository.ShopProxyRepository
import com.brainwallet.tools.manager.AnalyticsManager

@KoinViewModel
class ShopBentoViewModel(
Expand Down Expand Up @@ -74,6 +75,13 @@ class ShopBentoViewModel(
)
}
}
is ShopBentoEvent.InvoiceCreated -> {
AnalyticsManager
.logCustomEventWithParams(
"user_shop_invoice_created",
null
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import com.brainwallet.ui.bentosections.balancebento.BalanceBentoScreen
import com.brainwallet.ui.bentosections.buyreceivebento.receive.ReceiveDialog
import com.brainwallet.ui.bentosections.gamehubbento.GameHubBentoPagerScreen
import com.brainwallet.ui.bentosections.ltcpickerbento.LTCPickerBentoScreen
import com.brainwallet.ui.bentosections.shopbento.ShopBentoEvent
import com.brainwallet.ui.bentosections.shopbento.ShopBentoScreen
import com.brainwallet.ui.bentosections.shopbento.ShopBentoViewModel
import com.brainwallet.ui.bentosections.transactionbento.TransactionsBentoScreen
Expand Down Expand Up @@ -461,7 +462,10 @@ fun MainScreen(
onNavigate = onNavigate,
url = (modalContentRoute as Route.BitrefillWeb).url.withTheme(
if (isDarkMode) "dark" else "light"
)
),
invoiceCreated = { _, _ ->
shopBentoViewModel.onEvent(ShopBentoEvent.InvoiceCreated)
}
)
else -> {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.brainwallet.ui.screens.main

import android.graphics.Bitmap
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.compose.foundation.background
Expand Down Expand Up @@ -30,6 +31,8 @@ fun WebModalScreen(
onNavigate: OnNavigate,
url: String,
modifier: Modifier = Modifier,
invoiceCreated: (invoiceId: String, paymentUri: String) -> Unit = { _, _ -> }

) {
val eventString = if (url.contains("bitrefill")) {
"user_did_tap_shop_bento"
Expand Down Expand Up @@ -64,7 +67,6 @@ fun WebModalScreen(
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.useWideViewPort = true

webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
Expand All @@ -74,8 +76,35 @@ fun WebModalScreen(
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
isLoading = false

view?.evaluateJavascript(
"""
(function() {
function handleMessage(event) {
try {
var data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
if (data && data.event === 'invoice_created') {
BitrefillAndroid.invoiceCreated(JSON.stringify(data));
}
} catch(e) {
console.log('parse error:', e);
}
}
window.addEventListener('message', handleMessage);
document.addEventListener('message', handleMessage);
})();
""".trimIndent(),
null
)
}
}

addJavascriptInterface(
WebAppInterface { invoiceId, paymentUri ->
invoiceCreated(invoiceId, paymentUri)
},
"BitrefillAndroid"
)
}
},
update = { it.loadUrl(url) }
Expand All @@ -89,3 +118,19 @@ fun WebModalScreen(
}
}
}

class WebAppInterface(
private val onInvoiceCreated: (invoiceId: String, paymentUri: String) -> Unit
) {
@JavascriptInterface
fun invoiceCreated(dataJson: String) {
try {
val obj = org.json.JSONObject(dataJson)
val invoiceId = obj.getString("invoiceId")
val paymentUri = obj.getString("paymentUri")
onInvoiceCreated(invoiceId, paymentUri)
} catch (e: Exception) {
android.util.Log.e("WebAppInterface", "Failed to parse invoice data", e)
}
}
}
Loading