Преглед изворни кода

refactor(登录页) :
- 重构 `LoginFragment` 与 `SwitchStatusFragment`
- 移除构造函数中的 `ViewPager2` 依赖,改为在 Fragment 内部通过 `activity?.findViewById` 获取实例
- 新增 `newInstance` 静态方法来创建 Fragment 实例
- 简化 `TwoFragmentAdapter` 的构造,不再传递 `ViewPager2` 实例
- 优化 `SwitchStatusFragment` 部分代码逻辑,移除冗余注释和空方法体

周文健 пре 2 недеља
родитељ
комит
953061f2a6

+ 1 - 1
app/src/main/java/com/grkj/iscs_mars/view/activity/LoginActivity.kt

@@ -30,7 +30,7 @@ class LoginActivity : BaseMvpActivity<ILoginView, LoginPresenter, ActivityLoginB
     override fun initView() {
         MMKV.initialize(SIKCore.getApplication())
         cronJobManager.bindService()
-        mBinding?.vp2?.adapter = TwoFragmentAdapter(this, mBinding?.vp2)
+        mBinding?.vp2?.adapter = TwoFragmentAdapter(this)
         mBinding?.vp2?.offscreenPageLimit = 1 // 只两页,提前缓存,不卡顿
         mBinding?.vp2?.isUserInputEnabled = false
 

+ 3 - 5
app/src/main/java/com/grkj/iscs_mars/view/adapter/TwoFragmentAdapter.kt

@@ -3,13 +3,11 @@ package com.grkj.iscs_mars.view.adapter
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.FragmentActivity
 import androidx.viewpager2.adapter.FragmentStateAdapter
-import androidx.viewpager2.widget.ViewPager2
 import com.grkj.iscs_mars.view.fragment.LoginFragment
 import com.grkj.iscs_mars.view.fragment.SwitchStatusFragment
 
-class TwoFragmentAdapter(activity: FragmentActivity, val vp2: ViewPager2?) : FragmentStateAdapter(activity) {
+class TwoFragmentAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
     override fun getItemCount() = 2
     override fun createFragment(position: Int): Fragment =
-        if (position == 0) SwitchStatusFragment(vp2) else LoginFragment(vp2)
-
-}
+        if (position == 0) SwitchStatusFragment.newInstance() else LoginFragment.newInstance()
+}

+ 18 - 15
app/src/main/java/com/grkj/iscs_mars/view/fragment/LoginFragment.kt

@@ -24,21 +24,28 @@ import com.sik.sikcore.shell.ShellUtils
 import com.zhy.adapter.recyclerview.CommonAdapter
 import com.zhy.adapter.recyclerview.base.ViewHolder
 
-/**
- * 登录界面
- */
-class LoginFragment(private val vp2: ViewPager2?) : BaseMvpFragment<ILoginView, LoginPresenter, FragmentLoginBinding>() {
-    private var cardLoginDialog: LoginDialog? = null
-    override fun initPresenter(): LoginPresenter {
-        return LoginPresenter()
+class LoginFragment :
+    BaseMvpFragment<ILoginView, LoginPresenter, FragmentLoginBinding>() {
+
+    companion object {
+        fun newInstance(): LoginFragment = LoginFragment()
     }
 
+    // 从 Activity 拿 ViewPager2(避免构造参数)
+    private val vp2: ViewPager2?
+        get() = activity?.findViewById(R.id.vp2)
+
+    private var cardLoginDialog: LoginDialog? = null
+
+    override fun initPresenter(): LoginPresenter = LoginPresenter()
+
     override val viewBinding: FragmentLoginBinding
         get() = FragmentLoginBinding.inflate(layoutInflater)
 
     override fun initView() {
         mBinding?.tvVersion?.post {
-            mBinding?.tvVersion?.text = "v${AppUtils.getPkgVerName(requireContext())}-${requireContext().serialNo()}"
+            mBinding?.tvVersion?.text =
+                "v${AppUtils.getPkgVerName(requireContext())}-${requireContext().serialNo()}"
         }
         mBinding?.cbMotor?.setDebouncedClickListener {
             vp2?.currentItem = 0
@@ -88,9 +95,7 @@ class LoginFragment(private val vp2: ViewPager2?) : BaseMvpFragment<ILoginView,
             LogUtil.i("创建Swipe dialog : ${presenter == null}")
             cardLoginDialog =
                 LoginDialog(presenter, requireContext()) { isSuccess, userInfoRespVO ->
-                    if (isSuccess) {
-                        goHome(userInfoRespVO)
-                    }
+                    if (isSuccess) goHome(userInfoRespVO)
                 }
         }
         cardLoginDialog?.setOnDismissListener {
@@ -102,11 +107,9 @@ class LoginFragment(private val vp2: ViewPager2?) : BaseMvpFragment<ILoginView,
 
     private fun goHome(userInfoRespVO: UserInfoRespVO?) {
         val intent = Intent(requireContext(), HomeActivity::class.java)
-        if (userInfoRespVO != null) {
-            intent.putExtra("userInfo", userInfoRespVO)
-        }
+        if (userInfoRespVO != null) intent.putExtra("userInfo", userInfoRespVO)
         startActivity(intent)
         BusinessManager.submitKeyData(requireContext())
         requireActivity().finish()
     }
-}
+}

+ 23 - 34
app/src/main/java/com/grkj/iscs_mars/view/fragment/SwitchStatusFragment.kt

@@ -34,9 +34,17 @@ import com.onlylemi.mapview.library.MapViewListener
 import com.sik.sikcore.extension.setDebouncedClickListener
 import com.sik.sikcore.thread.ThreadUtils
 
-
-class SwitchStatusFragment(private val vp2: ViewPager2?) :
+class SwitchStatusFragment :
     BaseMvpFragment<ISwitchStatusView, SwitchStatusPresenter, FragmentSwitchStatusBinding>() {
+
+    companion object {
+        fun newInstance(): SwitchStatusFragment = SwitchStatusFragment()
+    }
+
+    // 从 Activity 拿 ViewPager2(避免构造参数)
+    private val vp2: ViewPager2?
+        get() = activity?.findViewById(R.id.vp2)
+
     private var stationLayer: CustomSwitchStationLayer? = null
     private lateinit var gestureDetector: GestureDetector
     private lateinit var switchInfoDialog: SwitchInfoDialog
@@ -44,9 +52,7 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
     private var currentLotoId = ""
     private var isMapLoaded = false
 
-    override fun initPresenter(): SwitchStatusPresenter {
-        return SwitchStatusPresenter()
-    }
+    override fun initPresenter(): SwitchStatusPresenter = SwitchStatusPresenter()
 
     override val viewBinding: FragmentSwitchStatusBinding
         get() = FragmentSwitchStatusBinding.inflate(layoutInflater)
@@ -76,9 +82,7 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
         mBinding?.mapview?.setBackgroundColorInt(requireContext().getColor(R.color.color_map_base))
         mBinding?.rvList?.linear()?.dividerSpace(10, DividerOrientation.GRID)?.setup {
             addType<MotorMapInfoRespVO.IsMotorMapPoint>(R.layout.item_switch)
-            onBind {
-                onRVListBinding()
-            }
+            onBind { onRVListBinding() }
         }
         mBinding?.mapRv?.linear(LinearLayout.HORIZONTAL)?.dividerSpace(10, DividerOrientation.GRID)
             ?.setup {
@@ -103,7 +107,7 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
 
     private fun BindingAdapter.BindingViewHolder.onRVListBinding() {
         val itemBinding = getBinding<ItemSwitchBinding>()
-        val item = getModel<MotorMapInfoRespVO.IsMotorMapPoint>()
+        val item = getModel<IsMotorMapPoint>()
         val switchData = ModBusController.getSwitchData()
         itemBinding.switchName.text = "${item.motorName}"
         itemBinding.switchId.text = context.getString(R.string.switch_id, item.motorCode)
@@ -122,28 +126,24 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
                 itemBinding.switchStatus.setBackgroundResource(R.drawable.bg_switch_off)
                 itemBinding.switchStatus.text = CommonUtils.getStr(R.string.switch_close)
             }
-
             true -> {
                 itemBinding.switchStatus.setBackgroundResource(R.drawable.bg_switch_on)
                 itemBinding.switchStatus.text = CommonUtils.getStr(R.string.switch_open)
             }
-
             else -> {
                 itemBinding.switchStatus.setBackgroundResource(R.drawable.bg_switch_alarm)
                 itemBinding.switchStatus.text = CommonUtils.getStr(R.string.switch_alarm)
             }
         }
         itemBinding.root.setDebouncedClickListener {
-            stationLayer?.selectPoint(
-                item.motorId
-            )
+            stationLayer?.selectPoint(item.motorId)
         }
     }
 
     override fun onResume() {
         super.onResume()
         isMapLoaded = false
-        mBinding?.mapview?.setRenderEnabled(true)    // ✅ 当前页开始渲染
+        mBinding?.mapview?.setRenderEnabled(true)
         BusinessManager.mEventBus.observe(this) {
             when (it.code) {
                 MsgEventConstants.MSG_EVENT_SWITCH_COLLECTION_UPDATE_RESULT -> {
@@ -166,8 +166,9 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
                 mBinding?.mapTitle?.text =
                     (it.records.find { it.lotoSerialNumber == requireContext().serialNo() }
                         ?: it.records[0]).motorMapName
-                getMap((it.records.find { it.lotoSerialNumber == requireContext().serialNo() }
-                    ?: it.records[0]).motorMapId.toString(),
+                getMap(
+                    (it.records.find { it.lotoSerialNumber == requireContext().serialNo() }
+                        ?: it.records[0]).motorMapId.toString(),
                     (it.records.find { it.lotoSerialNumber == requireContext().serialNo() }
                         ?: it.records[0]).lotoId.toString()
                 )
@@ -177,13 +178,11 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
 
     override fun onPause() {
         super.onPause()
-        mBinding?.mapview?.setRenderEnabled(false)   // ✅ 离开当前页立刻停渲染
+        mBinding?.mapview?.setRenderEnabled(false)
     }
 
     private fun getMap(mapId: String, lotoId: String) {
-        if (lotoId.isEmpty() || mapId.isEmpty()) {
-            return
-        }
+        if (lotoId.isEmpty() || mapId.isEmpty()) return
         presenter?.getMapInfo(mapId.toLong()) { itMapInfo ->
             presenter?.getMotorMapInfo(lotoId.toLong()) { itMotorMapInfo ->
                 mBinding?.rvList?.models = itMotorMapInfo?.data
@@ -207,16 +206,11 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
         }
     }
 
-    /**
-     * 初始化地图
-     */
     private fun initMap() {
         mBinding?.mapview?.isScaleAndRotateTogether = false
         mBinding?.mapview?.setOnTouchListener { _, event ->
             gestureDetector.onTouchEvent(event); false
         }
-
-        // ✅ 提前创建点位层(不要等 onMapLoadSuccess)
         if (stationLayer == null) {
             stationLayer = CustomSwitchStationLayer(mBinding?.mapview, mutableListOf()).apply {
                 onLongPressListener = { point, screenX, screenY, _, _ ->
@@ -240,24 +234,19 @@ class SwitchStatusFragment(private val vp2: ViewPager2?) :
                             ?.filterIsInstance<IsMotorMapPoint>()
                             ?.indexOfFirst { it.motorCode == point.motorCode } ?: -1,
                         0
-                    ) // 第二个参数是偏移量
+                    )
                 }
             }
             mBinding?.mapview?.addLayer(stationLayer)
         }
-
         mBinding?.mapview?.setMapViewListener(object : MapViewListener {
             override fun onMapLoadSuccess() {
-                // 地图加载完成后,只做视图层面的事,别再 new layer 了
                 mBinding?.mapview?.post {
                     mBinding?.mapview?.currentRotateDegrees = 0f
                     mBinding?.mapview?.refresh()
                 }
             }
-
-            override fun onMapLoadFail() {
-            }
+            override fun onMapLoadFail() = Unit
         })
     }
-
-}
+}