Forráskód Böngészése

add(webview): add webview

zhaoyadi 1 éve
szülő
commit
687521a37d

+ 0 - 22
core/auth/src/main/res/layout/layout_login.xml

@@ -1,22 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <EditText
-        android:id="@+id/edit_phone"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content" />
-
-    <EditText
-        android:id="@+id/edit_sms"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content" />
-
-    <Button
-        android:id="@+id/action_login"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:text="登录" />
-</LinearLayout>

+ 2 - 0
feat/design/build.gradle.kts

@@ -9,6 +9,8 @@ android {
 }
 
 dependencies {
+    api(project(":feat:webview"))
+
     implementation("androidx.constraintlayout:constraintlayout-compose:1.0.1")
 
     implementation(libs.coil.kt.compose)

+ 10 - 0
feat/design/src/main/kotlin/com/zaojiao/app/feat/design/viewmodel/BaseViewModel.kt

@@ -25,6 +25,16 @@ abstract class BaseViewModel<T> : ViewModel() {
         }
     }
 
+    fun refreshState() = viewModelScope.launch {
+        try {
+            val result = productState()
+            setSuccess(result)
+        } catch (e: Throwable) {
+            Log.e("BaseViewModel", "${this.javaClass.simpleName}: error", e)
+            setFailure(0)
+        }
+    }
+
     abstract suspend fun productState(): T
 
     protected suspend fun setLoading() {

+ 0 - 4
feat/home/build.gradle.kts

@@ -23,8 +23,4 @@ dependencies {
     implementation(project(":feat:design"))
     implementation(project(":feat:baby"))
     implementation(project(":feat:settings"))
-}
-
-tasks.assembleAndroidTest{
-
 }

+ 24 - 0
feat/webview/build.gradle.kts

@@ -0,0 +1,24 @@
+plugins {
+    id("d.convention.library")
+    kotlin("plugin.serialization")
+}
+
+android {
+    namespace = "com.zaojiao.app.feat.webview"
+
+    buildTypes {
+        release {
+            isMinifyEnabled = false
+            proguardFiles(
+                getDefaultProguardFile("proguard-android-optimize.txt"),
+                "proguard-rules.pro"
+            )
+        }
+    }
+}
+
+dependencies {
+    implementation("androidx.appcompat:appcompat:1.6.1")
+
+    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
+}

+ 9 - 0
feat/webview/src/main/AndroidManifest.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <application>
+        <activity android:name=".WebViewActivity">
+
+        </activity>
+    </application>
+</manifest>

+ 25 - 0
feat/webview/src/main/java/com/zaojiao/app/feat/webview/JavaScriptChannel.kt

@@ -0,0 +1,25 @@
+package com.zaojiao.app.feat.webview
+
+import android.os.Looper
+import android.webkit.JavascriptInterface
+import kotlinx.serialization.SerializationException
+import kotlinx.serialization.json.Json
+import java.util.logging.Handler
+
+
+class JavaScriptChannel(
+    private val webViewHandler: WebViewHandler
+) {
+    private val json = Json {
+        ignoreUnknownKeys = true
+    }
+
+    @JavascriptInterface
+    fun postMessage(message: String) {
+        try {
+            webViewHandler.callHandler(json.decodeFromString(message))
+        } catch (exception: SerializationException) {
+            exception.printStackTrace()
+        }
+    }
+}

+ 6 - 0
feat/webview/src/main/java/com/zaojiao/app/feat/webview/LJGWebViewClient.kt

@@ -0,0 +1,6 @@
+package com.zaojiao.app.feat.webview
+
+import android.webkit.WebViewClient
+
+class LJGWebViewClient:WebViewClient() {
+}

+ 43 - 0
feat/webview/src/main/java/com/zaojiao/app/feat/webview/WebViewActivity.kt

@@ -0,0 +1,43 @@
+package com.zaojiao.app.feat.webview
+
+import android.content.Context
+import android.content.Intent
+import android.net.Uri
+import android.os.Bundle
+import android.webkit.WebView
+import android.widget.ImageView
+import android.widget.LinearLayout
+import android.widget.TextView
+import androidx.appcompat.app.AppCompatActivity
+
+class WebViewActivity : AppCompatActivity() {
+    companion object {
+        fun start(context: Context, url: String) {
+            val intent = Intent(context, WebViewActivity::class.java)
+            intent.data = Uri.parse(url)
+            context.startActivity(intent)
+        }
+    }
+
+    private lateinit var actionBar: LinearLayout
+    private lateinit var actionBack: ImageView
+    private lateinit var actionTitle: TextView
+    private lateinit var webView: WebView
+
+    private lateinit var handler: WebViewHandler
+
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.layout_webview)
+
+        actionBar = findViewById(R.id.action_bar)
+        actionBack = findViewById(R.id.action_back)
+        actionTitle = findViewById(R.id.action_title)
+        webView = findViewById(R.id.content_webview)
+
+        handler = WebViewHandler(webView)
+
+        webView.loadUrl(intent.data.toString())
+    }
+}

+ 35 - 0
feat/webview/src/main/java/com/zaojiao/app/feat/webview/WebViewHandler.kt

@@ -0,0 +1,35 @@
+package com.zaojiao.app.feat.webview
+
+import android.webkit.WebView
+
+class WebViewHandler(
+    private val webView: WebView,
+) {
+    private val handlers = mutableMapOf<String, (WebViewRequest) -> WebViewResponse>()
+
+    init {
+        webView.addJavascriptInterface(JavaScriptChannel(this), "jsBridge")
+    }
+
+    @Throws(IllegalArgumentException::class)
+    fun register(callbackName: String, executor: (WebViewRequest) -> WebViewResponse) {
+        if (handlers[callbackName] != null) {
+            throw IllegalArgumentException("$callbackName 已经注册过了")
+        }
+
+        handlers[callbackName] = executor
+    }
+
+    fun unregister(callName: String) {
+        handlers.remove(callName)
+    }
+
+    @Throws(IllegalArgumentException::class)
+    fun callHandler(request: WebViewRequest) {
+        val executor = handlers[request.callbackName]
+            ?: throw IllegalArgumentException("${request.callbackName} 还未注册")
+
+        val response = executor.invoke(request)
+        webView.evaluateJavascript("javascript:${request.callbackName}", null)
+    }
+}

+ 26 - 0
feat/webview/src/main/java/com/zaojiao/app/feat/webview/WebViewMessage.kt

@@ -0,0 +1,26 @@
+package com.zaojiao.app.feat.webview
+
+data class WebViewRequest(
+    /* 回调的方法 */
+    val callbackName: String,
+    /* 回调的id */
+    val callbackId: String,
+    val payload: Any?,
+)
+
+
+data class WebViewResponse(
+    val callbackName: String,
+    val callbackId: String,
+    val data: Any?,
+    val error: Any?,
+)
+
+fun WebViewRequest.response(data: Any?, error: Any?): WebViewResponse {
+    return WebViewResponse(
+        callbackName = this.callbackName,
+        callbackId = this.callbackId,
+        data = data,
+        error = error,
+    )
+}

+ 42 - 0
feat/webview/src/main/res/layout/layout_webview.xml

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@android:color/white"
+    android:orientation="vertical">
+
+    <LinearLayout
+        android:id="@+id/action_bar"
+        android:layout_width="match_parent"
+        android:layout_height="44dp"
+        android:background="@android:color/transparent">
+
+        <ImageView
+            android:id="@+id/action_back"
+            android:layout_width="44dp"
+            android:layout_height="44dp" />
+
+        <TextView
+            android:id="@+id/action_title"
+            android:layout_width="0dp"
+            android:layout_height="match_parent"
+            android:layout_weight="1"
+            android:ellipsize="end"
+            android:gravity="center"
+            android:paddingHorizontal="16dp"
+            android:text="hello"
+            android:textAlignment="center"
+            android:textColor="@android:color/black"
+            android:textSize="16sp" />
+
+        <View
+            android:layout_width="44dp"
+            android:layout_height="44dp" />
+    </LinearLayout>
+
+    <WebView
+        android:id="@+id/content_webview"
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="1" />
+</LinearLayout>

+ 1 - 1
settings.gradle.kts

@@ -29,6 +29,7 @@ include(":core:nav")
 include(":core:navx")
 
 include(":data:model")
+include(":data:database")
 include(":data:local")
 include(":data:remote")
 include(":data:repo")
@@ -38,5 +39,4 @@ include(":feat:design")
 include(":feat:home")
 include(":feat:baby")
 include(":feat:settings")
-include(":data:database")
 include(":feat:webview")