From 0abcffee0fd65699270ddf95180ec4cbda94c762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E9=93=AD=E6=9D=B0?= Date: Wed, 3 Apr 2024 10:35:04 +0800 Subject: [PATCH 1/3] feat:Import Pull to Refresh Library --- app/build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index cb986d79..3c1c1c54 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -61,6 +61,7 @@ dependencies { implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.compose.material3:material3") implementation("androidx.navigation:navigation-compose:2.7.6") + implementation("com.github.fengdai.compose:pulltorefresh:0.2.0") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") From d4580677500d01b6855f510f450f34abf803f319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E9=93=AD=E6=9D=B0?= Date: Wed, 3 Apr 2024 10:35:36 +0800 Subject: [PATCH 2/3] [Not Working]feat:Basic Pull to Refresh sample --- app/src/main/AndroidManifest.xml | 6 ++ .../com/kevinnzou/webview/MainActivity.kt | 11 +++ .../sample/PullToRefreshWebViewSample.kt | 82 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4fcda456..9c61fa55 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -38,6 +38,12 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/kevinnzou/webview/MainActivity.kt b/app/src/main/java/com/kevinnzou/webview/MainActivity.kt index 06566534..c5420119 100644 --- a/app/src/main/java/com/kevinnzou/webview/MainActivity.kt +++ b/app/src/main/java/com/kevinnzou/webview/MainActivity.kt @@ -17,6 +17,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.kevinnzou.webview.sample.WebViewSaveStateSample import com.kevinnzou.webview.sample.BasicWebViewSample +import com.kevinnzou.webview.sample.PullToRefreshWebViewSample import com.kevinnzou.webview.ui.theme.ComposewebviewTheme class MainActivity : ComponentActivity() { @@ -54,6 +55,16 @@ class MainActivity : ComponentActivity() { }) { Text("Save State") } + Button(onClick = { + startActivity( + Intent( + this@MainActivity, + PullToRefreshWebViewSample::class.java + ) + ) + }) { + Text("Pull to Refresh") + } } } } diff --git a/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt b/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt new file mode 100644 index 00000000..355697a5 --- /dev/null +++ b/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt @@ -0,0 +1,82 @@ +package com.kevinnzou.webview.sample + +import android.annotation.SuppressLint +import android.graphics.Bitmap +import android.os.Bundle +import android.util.Log +import android.webkit.WebView +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material3.ExperimentalMaterial3Api +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 com.github.fengdai.compose.pulltorefresh.PullToRefresh +import com.github.fengdai.compose.pulltorefresh.rememberPullToRefreshState +import com.kevinnzou.web.AccompanistWebViewClient +import com.kevinnzou.web.WebView +import com.kevinnzou.web.rememberWebViewNavigator +import com.kevinnzou.web.rememberWebViewState +import com.kevinnzou.webview.ui.theme.ComposewebviewTheme +import kotlinx.coroutines.delay + +class PullToRefreshWebViewSample : ComponentActivity() { + val initialUrl = "https://github.com/KevinnZou/compose-webview" + + @OptIn(ExperimentalMaterial3Api::class) + @SuppressLint("SetJavaScriptEnabled") + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContent { + ComposewebviewTheme { + val state = rememberWebViewState(url = initialUrl) + val navigator = rememberWebViewNavigator() + var textFieldValue by remember(state.lastLoadedUrl) { + mutableStateOf(state.lastLoadedUrl) + } + + var refreshing by remember { mutableStateOf(false) } + LaunchedEffect(refreshing) { + if (refreshing) { + delay(1200) + refreshing = false + } + } + + PullToRefresh( + state = rememberPullToRefreshState(isRefreshing = refreshing), + onRefresh = { refreshing = true } + ) { + // A custom WebViewClient and WebChromeClient can be provided via subclassing + val webClient = remember { + object : AccompanistWebViewClient() { + override fun onPageStarted( + view: WebView, + url: String?, + favicon: Bitmap? + ) { + super.onPageStarted(view, url, favicon) + Log.d("Accompanist WebView", "Page started loading for $url") + } + } + } + + WebView( + state = state, + modifier = Modifier + .fillMaxSize(), + navigator = navigator, + onCreated = { webView -> + webView.settings.javaScriptEnabled = true + }, + client = webClient + ) + } + } + } + } +} \ No newline at end of file From 7270728f58805dc54a268ff728dd1184df085252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E9=93=AD=E6=9D=B0?= Date: Wed, 3 Apr 2024 10:46:53 +0800 Subject: [PATCH 3/3] feat:Basic Pull to Refresh sample --- .../sample/PullToRefreshWebViewSample.kt | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt b/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt index 355697a5..48971124 100644 --- a/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt +++ b/app/src/main/java/com/kevinnzou/webview/sample/PullToRefreshWebViewSample.kt @@ -7,7 +7,10 @@ import android.util.Log import android.webkit.WebView import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue @@ -42,6 +45,7 @@ class PullToRefreshWebViewSample : ComponentActivity() { var refreshing by remember { mutableStateOf(false) } LaunchedEffect(refreshing) { if (refreshing) { + navigator.reload() delay(1200) refreshing = false } @@ -51,30 +55,34 @@ class PullToRefreshWebViewSample : ComponentActivity() { state = rememberPullToRefreshState(isRefreshing = refreshing), onRefresh = { refreshing = true } ) { - // A custom WebViewClient and WebChromeClient can be provided via subclassing - val webClient = remember { - object : AccompanistWebViewClient() { - override fun onPageStarted( - view: WebView, - url: String?, - favicon: Bitmap? - ) { - super.onPageStarted(view, url, favicon) - Log.d("Accompanist WebView", "Page started loading for $url") + Column( + modifier = Modifier.verticalScroll(state = rememberScrollState()) + ) { + // A custom WebViewClient and WebChromeClient can be provided via subclassing + val webClient = remember { + object : AccompanistWebViewClient() { + override fun onPageStarted( + view: WebView, + url: String?, + favicon: Bitmap? + ) { + super.onPageStarted(view, url, favicon) + Log.d("Accompanist WebView", "Page started loading for $url") + } } } - } - WebView( - state = state, - modifier = Modifier - .fillMaxSize(), - navigator = navigator, - onCreated = { webView -> - webView.settings.javaScriptEnabled = true - }, - client = webClient - ) + WebView( + state = state, + modifier = Modifier + .fillMaxSize(), + navigator = navigator, + onCreated = { webView -> + webView.settings.javaScriptEnabled = true + }, + client = webClient + ) + } } } }