Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat: 内置Webview网页
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Jan 11, 2024
1 parent 24f2fb5 commit dbc99dd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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)

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Triple<String, String, JsResult>?>(null) }
Expand Down Expand Up @@ -64,13 +73,43 @@ 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 = {
val refreshLayout = SwipeRefreshLayout(it)
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?
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit dbc99dd

Please sign in to comment.