Updated on 2026-08-14
This commit is contained in:
parent
03e155d1a5
commit
22141e15d8
4 changed files with 47 additions and 83 deletions
|
|
@ -35,6 +35,7 @@ dependencies {
|
|||
implementation(deps.compose.foundation)
|
||||
implementation(deps.compose.ui)
|
||||
implementation(deps.compose.ui.tooling)
|
||||
implementation(deps.compose.accompanist.webView)
|
||||
|
||||
/** Preferences */
|
||||
implementation(deps.krateSharedPref)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
package com.tangem.feature.learn2earn.presentation.webView
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import android.webkit.WebView
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.ActionBar
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.tangem.feature.learn2earn.impl.R
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import com.google.accompanist.web.WebView
|
||||
import com.google.accompanist.web.rememberWebViewState
|
||||
import com.tangem.core.ui.components.appbar.AppBarWithBackButton
|
||||
import com.tangem.core.ui.res.TangemTheme
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
||||
/**
|
||||
|
|
@ -17,53 +24,47 @@ class Learn2earnWebViewActivity : AppCompatActivity() {
|
|||
|
||||
private val viewModel by viewModels<WebViewViewModel>()
|
||||
|
||||
private lateinit var actionBar: ActionBar
|
||||
private lateinit var webView: WebView
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_web_view)
|
||||
|
||||
val webViewData = viewModel.extractData(intent)
|
||||
|
||||
setSupportActionBar(findViewById(R.id.toolbar))
|
||||
actionBar = supportActionBar!!
|
||||
actionBar.setDisplayHomeAsUpEnabled(true)
|
||||
actionBar.setDisplayShowHomeEnabled(true)
|
||||
actionBar.title = webViewData.uri.authority
|
||||
webView = findViewById(R.id.web_view)
|
||||
|
||||
webView.settings.javaScriptEnabled = true
|
||||
webView.settings.domStorageEnabled = true
|
||||
webView.settings.loadsImagesAutomatically = true
|
||||
|
||||
webView.webViewClient = Learn2earnWebViewClient(
|
||||
redirectHandler = viewModel,
|
||||
headers = webViewData.headers,
|
||||
finishSessionHandler = { finish() },
|
||||
)
|
||||
webView.loadUrl(
|
||||
webViewData.uri.toString(),
|
||||
webViewData.headers,
|
||||
)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
return when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
finish()
|
||||
true
|
||||
setContent {
|
||||
TangemTheme {
|
||||
BackHandler(onBack = { finish() })
|
||||
ScreenContent(webViewData)
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
webView.clearHistory()
|
||||
webView.clearCache(true)
|
||||
webView.destroy()
|
||||
|
||||
super.onDestroy()
|
||||
@Composable
|
||||
private fun ScreenContent(webViewData: WebViewData) {
|
||||
Column {
|
||||
AppBarWithBackButton(
|
||||
modifier = Modifier.background(TangemTheme.colors.background.secondary),
|
||||
text = webViewData.uri.authority,
|
||||
onBackClick = { finish() },
|
||||
)
|
||||
WebView(
|
||||
state = rememberWebViewState(
|
||||
url = webViewData.uri.toString(),
|
||||
additionalHttpHeaders = webViewData.headers
|
||||
),
|
||||
onCreated = {
|
||||
it.settings.apply {
|
||||
javaScriptEnabled = true
|
||||
domStorageEnabled = true
|
||||
loadsImagesAutomatically = true
|
||||
}
|
||||
},
|
||||
client = remember {
|
||||
Learn2earnWebViewClient(
|
||||
redirectHandler = viewModel,
|
||||
headers = webViewData.headers,
|
||||
finishSessionHandler = { finish() },
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package com.tangem.feature.learn2earn.presentation.webView
|
|||
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import com.google.accompanist.web.AccompanistWebViewClient
|
||||
import com.tangem.feature.learn2earn.domain.api.WebViewAction
|
||||
import com.tangem.feature.learn2earn.domain.api.WebViewRedirectHandler
|
||||
import timber.log.Timber
|
||||
|
|
@ -11,7 +11,7 @@ internal class Learn2earnWebViewClient(
|
|||
private val redirectHandler: WebViewRedirectHandler,
|
||||
private val headers: Map<String, String>,
|
||||
private val finishSessionHandler: () -> Unit,
|
||||
) : WebViewClient() {
|
||||
) : AccompanistWebViewClient() {
|
||||
|
||||
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
|
||||
Timber.d("url: ${request.url}")
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/coordinator"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.google.android.material.appbar.AppBarLayout
|
||||
android:id="@+id/app_bar"
|
||||
style="@style/Widget.MaterialComponents.Toolbar.Surface"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:elevation="0dp">
|
||||
|
||||
<com.google.android.material.appbar.MaterialToolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
app:layout_scrollFlags="scroll|enterAlways"
|
||||
app:navigationIcon="@drawable/ic_close_24" />
|
||||
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
<androidx.core.widget.NestedScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layout_behavior="@string/appbar_scrolling_view_behavior">
|
||||
|
||||
<WebView
|
||||
android:id="@+id/web_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
|
||||
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
Loading…
Add table
Add a link
Reference in a new issue