NTP-13457 | Shiv Natani | Tribute Back Press Handled (#13678)

This commit is contained in:
Shiv Natani
2024-11-19 19:06:33 +05:30
committed by GitHub
parent 1e62869fb8
commit f120a45fa2
3 changed files with 49 additions and 10 deletions

View File

@@ -17,10 +17,16 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.navi.analytics.utils.NaviTrackEvent
import com.navi.base.utils.orFalse
import com.navi.common.utils.Constants
import com.naviapp.analytics.utils.NaviAnalytics
import com.naviapp.utils.Constants.WEB_URL
@@ -33,18 +39,34 @@ fun RenderWebView(
webChromeCustomClient: WebChromeClient = WebChromeClient(),
javascriptInterfaces: (WebView) -> (Map<String, Any>) = { mapOf() },
bottomPadding: Int = 0,
backPressCounter: Int = 0,
fallbackBackHandler: () -> Unit = {}
) {
var webView by remember { mutableStateOf<WebView?>(null) }
LaunchedEffect(backPressCounter) {
if (backPressCounter > 0) {
if (webView?.canGoBack().orFalse()) {
webView?.goBack()
} else {
fallbackBackHandler.invoke()
}
}
}
AndroidView(
factory = { context ->
WebView(context).apply {
isVerticalScrollBarEnabled = false
setWebViewSettings()
setOverScrollMode(View.OVER_SCROLL_NEVER)
setLayerType(View.LAYER_TYPE_HARDWARE, null)
webViewClient = webViewCustomClient
webChromeClient = webChromeCustomClient
addJavaScriptInterfaces(javascriptInterfaces)
}
webView =
WebView(context).apply {
isVerticalScrollBarEnabled = false
setWebViewSettings()
setOverScrollMode(View.OVER_SCROLL_NEVER)
setLayerType(View.LAYER_TYPE_HARDWARE, null)
webViewClient = webViewCustomClient
webChromeClient = webChromeCustomClient
addJavaScriptInterfaces(javascriptInterfaces)
}
webView!!
},
modifier = Modifier.fillMaxSize().padding(bottom = bottomPadding.dp).statusBarsPadding(),
update = { webView ->

View File

@@ -291,6 +291,7 @@ class WebRedirectionActivity : BaseActivity() {
handleException: (Throwable) -> Unit
) {
val lifecycleOwner = LocalLifecycleOwner.current
val backPressCounter by webRedirectionVM.backPressCounter.collectAsStateWithLifecycle()
when (data) {
is UiState.Loading -> {
LoansTabContentShimmer()
@@ -322,12 +323,21 @@ class WebRedirectionActivity : BaseActivity() {
handleException = handleException
)
)
}
},
backPressCounter = backPressCounter,
fallbackBackHandler = { super.onBackPressed() }
)
}
}
}
override fun onBackPressed() {
webRedirectionVM.incrementBackPressedCounter()
if (webRedirectionVM.backPressCounter.value < 0) {
super.onBackPressed()
}
}
private fun handleNavigation(ctaData: CtaData) =
NaviDeepLinkNavigator.navigateTo(
activity = this,

View File

@@ -75,6 +75,9 @@ constructor(
private val _webRedirectionPlatform = MutableStateFlow(WebRedirectionPlatform.EXTERNAL_WEB)
val webRedirectionPlatform = _webRedirectionPlatform.asStateFlow()
private val _backPressCounter = MutableStateFlow(0)
val backPressCounter = _backPressCounter.asStateFlow()
private val _webRedirectionData = MutableStateFlow<WebRedirectionData?>(null)
val webRedirectionData = _webRedirectionData.asStateFlow()
@@ -220,6 +223,10 @@ constructor(
fun showNaviFinservLogo(): Boolean {
return businessUnit == PL
}
fun incrementBackPressedCounter() {
_backPressCounter.update { it + 1 }
}
}
sealed class UiState {