Skip to content

Commit

Permalink
Merge pull request #8 from KevinnZou/feature/pull_to_refresh
Browse files Browse the repository at this point in the history
Feature/pull to refresh
  • Loading branch information
KevinnZou authored Apr 16, 2024
2 parents f49cc97 + 7270728 commit 6794305
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name=".sample.PullToRefreshWebViewSample"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>

</manifest>
11 changes: 11 additions & 0 deletions app/src/main/java/com/kevinnzou/webview/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -54,6 +55,16 @@ class MainActivity : ComponentActivity() {
}) {
Text("Save State")
}
Button(onClick = {
startActivity(
Intent(
this@MainActivity,
PullToRefreshWebViewSample::class.java
)
)
}) {
Text("Pull to Refresh")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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.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
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) {
navigator.reload()
delay(1200)
refreshing = false
}
}

PullToRefresh(
state = rememberPullToRefreshState(isRefreshing = refreshing),
onRefresh = { refreshing = true }
) {
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
)
}
}
}
}
}
}

0 comments on commit 6794305

Please sign in to comment.