From dbc99dd280cd2a70bc237974fc0489900a6848f3 Mon Sep 17 00:00:00 2001 From: Jing <42014615+jing332@users.noreply.github.com> Date: Thu, 11 Jan 2024 20:51:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=86=85=E7=BD=AEWebview=E7=BD=91?= =?UTF-8?q?=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alistandroid/ui/nav/web/WebScreen.kt | 6 +++ .../alistandroid/ui/nav/web/WebView.kt | 43 ++++++++++++++++++- .../alistandroid/ui/nav/web/WebViewModel.kt | 11 ----- .../jing332/alistandroid/util/AndroidUtils.kt | 11 ++++- 4 files changed, 57 insertions(+), 14 deletions(-) delete mode 100644 app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebViewModel.kt diff --git a/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebScreen.kt b/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebScreen.kt index 6224f55..4fd3128 100644 --- a/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebScreen.kt +++ b/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebScreen.kt @@ -1,19 +1,25 @@ package com.github.jing332.alistandroid.ui.nav.web +import android.content.Intent import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.remember import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import com.github.jing332.alistandroid.model.alist.AListConfigManager +import com.github.jing332.alistandroid.service.AListService @Composable fun WebScreen(modifier: Modifier = Modifier) { Scaffold { + Column(modifier = modifier.padding(it)) { val url = remember { "http://localhost:${AListConfigManager.config().scheme.httpPort}" } WebView(url = url) + } } } \ No newline at end of file diff --git a/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebView.kt b/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebView.kt index ae6cf81..16b8669 100644 --- a/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebView.kt +++ b/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebView.kt @@ -2,9 +2,11 @@ package com.github.jing332.alistandroid.ui.nav.web import android.annotation.SuppressLint import android.content.Intent +import android.content.IntentFilter import android.view.ViewGroup.LayoutParams import android.webkit.JsResult import android.webkit.WebChromeClient +import android.webkit.WebResourceError import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient @@ -17,22 +19,29 @@ import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.Text import androidx.compose.material3.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.viewinterop.AndroidView -import androidx.lifecycle.viewmodel.compose.viewModel import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.github.jing332.alistandroid.R +import com.github.jing332.alistandroid.service.AListService +import com.github.jing332.alistandroid.ui.widgets.LocalBroadcastReceiver import com.github.jing332.alistandroid.util.ToastUtils.longToast +import com.github.jing332.alistandroid.util.ToastUtils.toast +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch @SuppressLint("SetJavaScriptEnabled") @Composable -internal fun WebView(modifier: Modifier = Modifier, url: String, vm: WebViewModel = viewModel()) { +internal fun WebView(modifier: Modifier = Modifier, url: String) { var progress by remember { mutableIntStateOf(0) } var showAlertDialog by remember { mutableStateOf?>(null) } @@ -64,6 +73,26 @@ internal fun WebView(modifier: Modifier = Modifier, url: String, vm: WebViewMode BackHandler(webView?.canGoBack() == true) { webView?.goBack() } + + val context = LocalContext.current + val scope = rememberCoroutineScope() + LocalBroadcastReceiver(intentFilter = IntentFilter(AListService.ACTION_STATUS_CHANGED)) { + if (AListService.isRunning) + scope.launch { + delay(2000) + context.toast("reload") + + } + } + + LaunchedEffect(key1 = Unit) { + if (!AListService.isRunning) { + context.startService(Intent(context, AListService::class.java)) + webView?.reload() + } + } + + AndroidView( modifier = Modifier.fillMaxSize(), factory = { @@ -71,6 +100,16 @@ internal fun WebView(modifier: Modifier = Modifier, url: String, vm: WebViewMode webView ?: run { webView = WebView(it).apply { webViewClient = object : WebViewClient() { + override fun onReceivedError( + view: WebView, + request: WebResourceRequest, + error: WebResourceError + ) { + super.onReceivedError(view, request, error) + Thread.sleep(500) + if (request.isForMainFrame) view.reload() + } + override fun shouldOverrideUrlLoading( view: WebView?, request: WebResourceRequest? diff --git a/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebViewModel.kt b/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebViewModel.kt deleted file mode 100644 index a937b21..0000000 --- a/app/src/main/java/com/github/jing332/alistandroid/ui/nav/web/WebViewModel.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.jing332.alistandroid.ui.nav.web - -import android.annotation.SuppressLint -import android.app.Application -import android.webkit.WebView -import androidx.lifecycle.AndroidViewModel - -class WebViewModel(private val application: Application ) : AndroidViewModel(application) { - @SuppressLint("StaticFieldLeak") - var webView: WebView? = null -} \ No newline at end of file diff --git a/app/src/main/java/com/github/jing332/alistandroid/util/AndroidUtils.kt b/app/src/main/java/com/github/jing332/alistandroid/util/AndroidUtils.kt index eb74d79..a32df82 100644 --- a/app/src/main/java/com/github/jing332/alistandroid/util/AndroidUtils.kt +++ b/app/src/main/java/com/github/jing332/alistandroid/util/AndroidUtils.kt @@ -1,9 +1,18 @@ package com.github.jing332.alistandroid.util +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter import android.os.Build import android.view.View -import androidx.compose.ui.platform.LocalHapticFeedback +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.rememberUpdatedState +import androidx.compose.ui.platform.LocalContext import androidx.core.view.HapticFeedbackConstantsCompat +import com.github.jing332.alistandroid.constant.AppConst object AndroidUtils { const val ABI_ARM = "armeabi-v7a"