Browse Source

refactor(architecture): 重构应用启动和导航逻辑

- 修改 ReactHostHelper 和 MultipleReactActivityDelegate以支持新的架构
- 更新 AppManager 和 NavigationManager 以优化活动管理和导航
- 重构 MainActivity 和 MainApplication 以使用 ReactActivity 和 ReactNativeHost
- 更新资源文件和 TypeScript 文件以适应新的结构
xuqm 1 tuần trước cách đây
mục cha
commit
da112f0733

+ 1 - 0
android/app/src/main/java/com/facebook/react/runtime/ReactHostHelper.kt

@@ -12,6 +12,7 @@ class ReactHostHelper(
         task.waitForCompletion()
         return task.getResult()
     }
+
     fun getOrCreateReactInstance() {
         delegate.isInstanceInitialized
     }

+ 5 - 1
android/app/src/main/java/com/trust/ywx/AppManager.kt

@@ -13,16 +13,20 @@ object AppManager {
         activityStack.remove(activity)
     }
 
-    fun finishActivity(activity: Activity?) {
+    fun finishActivity(activity: Activity?): Activity? {
         if (activity != null) {
             removeActivity(activity)
             activity.finish()
+            return activity
         } else {
             lastActivity()?.apply {
                 removeActivity(this)
                 this.finish()
+                return this
             }
+
         }
+        return null
     }
 
     fun lastActivity(): Activity? {

+ 4 - 6
android/app/src/main/java/com/trust/ywx/BuzActivity.kt

@@ -34,12 +34,10 @@ class BuzActivity : ReactActivity() {
         AppManager.addActivity(this)
         Toast.makeText(this, "BuzActivity:" + NavigationHelper.routerName, Toast.LENGTH_SHORT)
             .show()
-//        if (reactHost.lifecycleState != LifecycleState.RESUMED) {
-//            reactHost.onHostResume(this)
-//        }
-//        Handler(Looper.getMainLooper()).post {
-//            reactHost.reload("Requested by CodeUpdater")
-//        }
+    }
+
+    override fun onResume() {
+        super.onResume()
     }
 
     override fun onDestroy() {

+ 10 - 7
android/app/src/main/java/com/trust/ywx/MainActivity.kt

@@ -1,15 +1,18 @@
 package com.trust.ywx
 
-import android.content.Intent
 import android.os.Bundle
-import androidx.appcompat.app.AppCompatActivity
-import com.trust.ywx.specs.navigation.NavigationHelper
+import com.facebook.react.ReactActivity
+import com.facebook.react.ReactActivityDelegate
+import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
+import com.facebook.react.defaults.DefaultReactActivityDelegate
+
+class MainActivity : ReactActivity() {
+    override fun getMainComponentName(): String = "app"
+    override fun createReactActivityDelegate(): ReactActivityDelegate =
+        DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
 
-class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        NavigationHelper.routerName = "app"
-        startActivity(Intent(this, BuzActivity::class.java))
-        finish()
+        AppManager.addActivity(this)
     }
 }

+ 18 - 26
android/app/src/main/java/com/trust/ywx/MainApplication.kt

@@ -15,37 +15,29 @@ import java.io.File
 
 class MainApplication : Application(), ReactApplication {
 
-    override val reactNativeHost: ReactNativeHost =
-        object : DefaultReactNativeHost(this) {
-            override fun getPackages(): List<ReactPackage> =
-                PackageList(this).packages.apply {
-                    // Packages that cannot be autolinked yet can be added manually here, for example:
-                    add(NavigationPackage())
-                }
+    override val reactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) {
+        override fun getPackages(): List<ReactPackage> = PackageList(this).packages.apply {
+            // Packages that cannot be autolinked yet can be added manually here, for example:
+            add(NavigationPackage())
+        }
 
-            override fun getJSMainModuleName(): String =
-                if (getUseDeveloperSupport()) "index" else "commom"
+        override fun getJSMainModuleName(): String =
+            if (getUseDeveloperSupport()) "index" else "commom"
 
-            override fun getBundleAssetName(): String? =
-                if (getUseDeveloperSupport()) "index.android.bundle" else null
+        override fun getBundleAssetName(): String? =
+            if (getUseDeveloperSupport()) "index.android.bundle" else null
 
-            //
-//            override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
-//                super.getJSBundleFile()
-//            } else {
-//                FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
-//            }
-            override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
-                null
-            } else {
-                FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
-            }
+        override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
+            null
+        } else {
+            FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
+        }
 
-            override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
+        override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
 
-            override val isNewArchEnabled: Boolean = true
-            override val isHermesEnabled: Boolean = true
-        }
+        override val isNewArchEnabled: Boolean = true
+        override val isHermesEnabled: Boolean = true
+    }
 
     override val reactHost: ReactHost
         get() = getDefaultReactHost(applicationContext, reactNativeHost)

+ 11 - 3
android/app/src/main/java/com/trust/ywx/multiple/MultipleReactActivityDelegate.kt

@@ -38,6 +38,7 @@ class MultipleReactActivityDelegate(
         }
 
         if (enableBridgelessArchitecture()) {
+
             this.mReactDelegate =
                 ReactDelegate(
                     this.plainActivity,
@@ -59,6 +60,7 @@ class MultipleReactActivityDelegate(
                 )
 
             this.loadApp(mainComponentName)
+
         } else {
             this.mReactDelegate =
                 object : ReactDelegate(
@@ -86,9 +88,14 @@ class MultipleReactActivityDelegate(
 
                         val instance =
                             reactNativeHost.reactInstanceManager.currentReactContext?.catalystInstance
-                        instance?.loadScriptFromAssets(
-                            context.assets,
-                            "assets://index.android.bundle",
+                        val fileName = FileHelper.getFilePath(
+                            "$mainComponentName.android.bundle",
+                            reactActivity.applicationContext,
+                            "bundles"
+                        )
+                        instance?.loadScriptFromFile(
+                            fileName,
+                            fileName,
                             false
                         )
                         Log.i("TestApp", "loaded biz bundle")
@@ -185,4 +192,5 @@ class MultipleReactActivityDelegate(
      * context will no longer be valid.
      */
     override fun getCurrentReactContext(): ReactContext = mReactDelegate!!.currentReactContext!!
+
 }

+ 3 - 3
android/app/src/main/java/com/trust/ywx/specs/navigation/NavigationManager.kt

@@ -1,6 +1,7 @@
 package com.trust.ywx.specs.navigation
 
 import android.content.Intent
+import android.widget.Toast
 import com.facebook.react.bridge.ReactApplicationContext
 import com.trust.ywx.AppManager
 import com.trust.ywx.BuzActivity
@@ -12,15 +13,14 @@ class NavigationManager(reactContext: ReactApplicationContext) :
         NavigationHelper.routerName = name
         AppManager.lastActivity()
             ?.apply {
+                Toast.makeText(this, "Navigate to $name", Toast.LENGTH_SHORT).show()
                 startActivity(Intent(this, BuzActivity::class.java))
                 overridePendingTransition(0, 0)
             }
-//        AppManager.lastActivity()
-//            ?.overridePendingTransition(0, 0)
     }
 
     override fun pop() {
-        AppManager.finishActivity(null)
+        AppManager.finishActivity(null)?.overridePendingTransition(0, 0)
     }
 
     override fun getName() = NAME

+ 2 - 2
android/app/src/main/java/com/trust/ywx/utils/FileHelper.kt

@@ -18,8 +18,8 @@ class FileHelper {
         }
 
         fun getFilePath(name: String, context: Context?, type: String?): String {
-            val path = getDirPath(context, type) + File.separator + name
-            return path
+            val path = getDirPath(context, null)
+            return if (type.isNullOrBlank()) path + File.separator + name else path + File.separator + type + File.separator + name
         }
 
         fun copyAssetFileToInternalStorage(

+ 7 - 1
android/app/src/main/res/layout/activity_main.xml

@@ -6,7 +6,13 @@
 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:text="button"
+    android:text="button1"
     android:id="@+id/btn1"
     android:layout_gravity="center"/>
+<Button
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:text="button2"
+    android:id="@+id/btn2"
+    android:layout_gravity="center"/>
 </LinearLayout>

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 811
bundle/android/common.android.bundle


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 532
config/bundleCommonInfo.json


+ 4 - 4
src/common/common.ts

@@ -1,4 +1,4 @@
-import  'react';
-import  'react-native';
-import  '@common/NavigationHelper';
-import  '@common/ToastHelper.ts';
+import 'react';
+import 'react-native';
+import '@common/NavigationHelper';
+import '@common/ToastHelper.ts';

+ 2 - 2
src/ywq/Ywq.tsx

@@ -15,7 +15,7 @@ import {
 import { showMessage } from '@common/ToastHelper.ts';
 import Toast from 'react-native-toast-message';
 
-function App() {
+function YWQ() {
   const isDarkMode = useColorScheme() === 'dark';
 
   return (
@@ -41,4 +41,4 @@ const styles = StyleSheet.create({
   },
 });
 
-export default App;
+export default YWQ;

+ 2 - 2
src/ywq/ywq.ts

@@ -1,5 +1,5 @@
 import { AppRegistry } from 'react-native';
-import App from './Ywq.tsx';
+import YWQ from '@ywx/Ywq.tsx';
 import { Apps } from '@common/NavigationHelper.ts';
 
-AppRegistry.registerComponent(Apps.Ywq, () => App);
+AppRegistry.registerComponent(Apps.Ywq, () => YWQ);

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác