Feat(All): Tis

some changes
This commit is contained in:
Amir Mousavi
2026-05-06 23:33:56 +03:30
parent c9e656f705
commit d73df5524e
128 changed files with 1635 additions and 156 deletions
+3
View File
@@ -37,6 +37,7 @@ dependencies {
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.retrofit.converter.gson)
testImplementation(libs.junit)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
@@ -44,4 +45,6 @@ dependencies {
androidTestImplementation(libs.androidx.junit)
debugImplementation(libs.androidx.compose.ui.test.manifest)
debugImplementation(libs.androidx.compose.ui.tooling)
implementation(project(":core"))
}
@@ -1,8 +1,10 @@
package com.example.design_system
import android.annotation.SuppressLint
import android.app.Activity
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
@@ -15,14 +17,17 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.example.core.model.PrintEntity
import com.example.design_system.util.PspJavaScriptInterface
import com.example.design_system.util.snackbar.SnackbarController
import com.example.design_system.util.snackbar.SnackbarEvent
import kotlinx.coroutines.launch
@SuppressLint("JavascriptInterface")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PspWebView(
@@ -31,7 +36,8 @@ fun PspWebView(
modifier: Modifier = Modifier,
onWebView: (WebView) -> Unit,
onPayClick: (amount: Long, id: String?) -> Unit,
onPrintClick: () -> Unit,
onUUID: () -> Unit,
onPrintClick: (printEntities: List<PrintEntity>) -> Unit,
) {
val canGoBack = remember { mutableStateOf(false) }
@@ -40,6 +46,18 @@ fun PspWebView(
var lastBackTime by remember { mutableLongStateOf(0L) }
var isPaymentLaunching by remember { mutableStateOf(false) }
// Use rememberUpdatedState to ensure the bridge always calls the latest callbacks
val currentOnPayClick by rememberUpdatedState(onPayClick)
val currentOnPrintClick by rememberUpdatedState(onPrintClick)
val currentOnUUID by rememberUpdatedState(onUUID)
val bridge = remember {
PspJavaScriptInterface(
onPayClick = { amount, id -> currentOnPayClick(amount, id) },
onPrintClick = { entities -> currentOnPrintClick(entities) },
onUUID = { currentOnUUID() }
)
}
BackHandler {
if (canGoBack.value) {
@@ -61,7 +79,6 @@ fun PspWebView(
}
}
AndroidView(
modifier = modifier,
factory = { ctx ->
@@ -89,23 +106,7 @@ fun PspWebView(
"Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 Chrome/120.0 Mobile Safari/537.36"
}
addJavascriptInterface(
object : PspJavaScriptInterface {
@JavascriptInterface
override fun pay(amount: Long, id: String?) {
onPayClick(amount, id)
}
@JavascriptInterface
override fun print(text: String) {
TODO("Not yet implemented")
}
}, "AndroidPSP"
)
addJavascriptInterface(bridge, "NativeBridge")
loadUrl(url)
@@ -1,8 +1,51 @@
package com.example.design_system.util
interface PspJavaScriptInterface {
import android.util.Log
import android.webkit.JavascriptInterface
import androidx.annotation.Keep
import com.example.core.model.PrintEntity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
fun pay(amount: Long,id: String?)
@Keep
class PspJavaScriptInterface(
private val onPayClick: (Long, String?) -> Unit,
private val onPrintClick: (List<PrintEntity>) -> Unit,
private val onUUID: () -> Unit,
) {
fun print(text: String)
}
@JavascriptInterface
@Keep
fun pay(amount: Double, id: String?) {
Log.d("PspBridge", "pay called: amount=$amount, id=$id")
try {
onPayClick(amount.toLong(), id)
} catch (e: Exception) {
Log.e("PspBridge", "Error in pay callback", e)
}
}
@JavascriptInterface
@Keep
fun print(printEntitiesJson: String) {
Log.d("PspBridge", "print called: $printEntitiesJson")
try {
val type = object : TypeToken<List<PrintEntity>>() {}.type
val list: List<PrintEntity> = Gson().fromJson(printEntitiesJson, type)
onPrintClick(list)
} catch (e: Exception) {
Log.e("PspBridge", "Error parsing print entities", e)
}
}
@JavascriptInterface
@Keep
fun uuid() {
Log.d("PspBridge", "uuid called")
try {
onUUID()
} catch (e: Exception) {
Log.e("PspBridge", "Error in uuid callback", e)
}
}
}