optimize webview
This commit is contained in:
@@ -17,7 +17,9 @@
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.PSP">
|
||||
android:theme="@style/Theme.PSP"
|
||||
android:hardwareAccelerated="true"
|
||||
>
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.psp.di
|
||||
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object AppModule
|
||||
@@ -2,6 +2,7 @@ package com.example.psp.screen
|
||||
|
||||
import android.app.Activity
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.WebSettings
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.activity.compose.BackHandler
|
||||
@@ -12,18 +13,20 @@ import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.viewinterop.AndroidView
|
||||
import com.example.design_system.util.snackbar.SnackbarController
|
||||
import com.example.design_system.util.snackbar.SnackbarEvent
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun WebViewScreen(
|
||||
url: String,
|
||||
modifier: Modifier = Modifier
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
var webView: WebView? by remember { mutableStateOf(null) }
|
||||
val canGoBack = remember { mutableStateOf(false) }
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
val context = LocalContext.current
|
||||
var lastBackTime by remember { mutableLongStateOf(0L) }
|
||||
|
||||
@@ -37,7 +40,9 @@ fun WebViewScreen(
|
||||
} else {
|
||||
lastBackTime = now
|
||||
scope.launch {
|
||||
snackbarHostState.showSnackbar("Press back again to exit")
|
||||
SnackbarController.sendEvent(
|
||||
event = SnackbarEvent(message = "برای خروج یک بار دیگر بر روی دکمه بازگشت کلیک کنید.")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -62,7 +67,6 @@ fun WebViewScreen(
|
||||
}
|
||||
)
|
||||
},
|
||||
snackbarHost = { SnackbarHost(snackbarHostState) }
|
||||
) { padding ->
|
||||
AndroidView(
|
||||
modifier = Modifier
|
||||
@@ -80,13 +84,29 @@ fun WebViewScreen(
|
||||
canGoBack.value = view?.canGoBack() ?: false
|
||||
}
|
||||
}
|
||||
settings.javaScriptEnabled = true
|
||||
loadUrl(url)
|
||||
|
||||
overScrollMode = android.view.View.OVER_SCROLL_NEVER
|
||||
|
||||
// Apply comprehensive WebView settings
|
||||
settings.apply {
|
||||
javaScriptEnabled = true
|
||||
domStorageEnabled = true
|
||||
databaseEnabled = true
|
||||
cacheMode = WebSettings.LOAD_DEFAULT
|
||||
loadsImagesAutomatically = true
|
||||
allowFileAccess = true
|
||||
useWideViewPort = true
|
||||
loadWithOverviewMode = true
|
||||
mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
|
||||
setSupportZoom(false)
|
||||
builtInZoomControls = false
|
||||
displayZoomControls = false
|
||||
}
|
||||
webView = this
|
||||
}
|
||||
},
|
||||
update = { view ->
|
||||
webView = view
|
||||
update = {
|
||||
it.loadUrl(url)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.example.psp.viewmodel
|
||||
|
||||
import android.app.Application
|
||||
import android.webkit.WebSettings
|
||||
import android.webkit.WebView
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class WebViewViewModel @Inject constructor(
|
||||
application: Application
|
||||
) : AndroidViewModel(application) {
|
||||
|
||||
private var _preloadedWebView: WebView? = null
|
||||
val preloadedWebView: WebView?
|
||||
get() = _preloadedWebView
|
||||
|
||||
init {
|
||||
preloadWebView()
|
||||
}
|
||||
|
||||
private fun preloadWebView() {
|
||||
viewModelScope.launch {
|
||||
_preloadedWebView = WebView(getApplication()).apply {
|
||||
overScrollMode = android.view.View.OVER_SCROLL_NEVER
|
||||
webChromeClient = android.webkit.WebChromeClient()
|
||||
// Apply the same settings as in WebViewScreen
|
||||
settings.apply {
|
||||
javaScriptEnabled = true
|
||||
domStorageEnabled = true
|
||||
cacheMode = WebSettings.LOAD_DEFAULT
|
||||
loadsImagesAutomatically = true
|
||||
allowFileAccess = true
|
||||
useWideViewPort = true
|
||||
loadWithOverviewMode = true
|
||||
mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
|
||||
setSupportZoom(false)
|
||||
builtInZoomControls = false
|
||||
displayZoomControls = false
|
||||
}
|
||||
// Load the URL to preload
|
||||
loadUrl("https://tis.shift-am.ir")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
_preloadedWebView?.destroy()
|
||||
_preloadedWebView = null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user