From 1dda7e314575a967148f55f05e830b91684f4326 Mon Sep 17 00:00:00 2001 From: zhumj Date: Wed, 24 Aug 2022 10:43:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20BasePresenter=EF=BC=9A?= =?UTF-8?q?=E7=BB=A7=E6=89=BF=20DefaultLifecycleObserver=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20Lifecycle=20=E8=87=AA=E5=8A=A8=E6=84=9F=E7=9F=A5?= =?UTF-8?q?=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/zhumj/androidkit/base/BaseActivity.kt | 4 +- .../com/zhumj/androidkit/base/BaseFragment.kt | 12 ++--- .../zhumj/androidkit/base/BasePresenter.kt | 7 ++- README.md | 4 +- app/build.gradle | 3 ++ app/src/main/AndroidManifest.xml | 6 ++- .../{ => activity}/MainActivity.kt | 8 ++- .../activity/MainActivity2.kt | 34 +++++++++++++ .../adapter/Main2ViewPagerAdapter.kt | 34 +++++++++++++ .../fragment/Main2Fragment.kt | 49 +++++++++++++++++++ .../mvp/contract/Main2Contract.kt | 20 ++++++++ .../mvp/contract/Main2F1Contract.kt | 18 +++++++ .../androidkitproject/mvp/model/Main2Model.kt | 16 ++++++ .../mvp/presenter/Main2F1Presenter.kt | 20 ++++++++ .../mvp/presenter/Main2Presenter.kt | 32 ++++++++++++ .../mvp/presenter/MainPresenter.kt | 5 ++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/activity_main2.xml | 18 +++++++ app/src/main/res/layout/fragment_main2.xml | 15 ++++++ .../main/res/layout/item_main2_fragment.xml | 12 +++++ 20 files changed, 303 insertions(+), 16 deletions(-) rename app/src/main/java/com/zhumj/androidkitproject/{ => activity}/MainActivity.kt (97%) create mode 100644 app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity2.kt create mode 100644 app/src/main/java/com/zhumj/androidkitproject/adapter/Main2ViewPagerAdapter.kt create mode 100644 app/src/main/java/com/zhumj/androidkitproject/fragment/Main2Fragment.kt create mode 100644 app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2Contract.kt create mode 100644 app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2F1Contract.kt create mode 100644 app/src/main/java/com/zhumj/androidkitproject/mvp/model/Main2Model.kt create mode 100644 app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2F1Presenter.kt create mode 100644 app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2Presenter.kt create mode 100644 app/src/main/res/layout/activity_main2.xml create mode 100644 app/src/main/res/layout/fragment_main2.xml create mode 100644 app/src/main/res/layout/item_main2_fragment.xml diff --git a/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseActivity.kt b/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseActivity.kt index 4518491..ae4ca12 100644 --- a/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseActivity.kt +++ b/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseActivity.kt @@ -36,6 +36,9 @@ abstract class BaseActivity?>: AppCompatA private fun init() { if (mPresenter == null) { mPresenter = obtainPresenter() + if (mPresenter != null) { + lifecycle.addObserver(mPresenter!!) + } } if (isRegisteredEventBus()) { EventBusUtil.register(this) @@ -100,7 +103,6 @@ abstract class BaseActivity?>: AppCompatA if (isRegisteredEventBus()) { EventBusUtil.unregister(this) } - mPresenter?.onDestroy() super.onDestroy() } diff --git a/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseFragment.kt b/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseFragment.kt index 21caf10..eb51c03 100644 --- a/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseFragment.kt +++ b/AndroidKit/src/main/java/com/zhumj/androidkit/base/BaseFragment.kt @@ -33,6 +33,9 @@ abstract class BaseFragment?>: Fragment() private fun init() { if (mPresenter == null) { mPresenter = obtainPresenter() + if (mPresenter != null) { + lifecycle.addObserver(mPresenter!!) + } } if (isRegisteredEventBus()) { @@ -41,11 +44,11 @@ abstract class BaseFragment?>: Fragment() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - _binding = getViewBinding(); + _binding = getViewBinding(inflater, container) return _binding.root } - protected abstract fun getViewBinding(): VB + protected abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB protected abstract fun obtainPresenter(): BP? /** @@ -94,12 +97,7 @@ abstract class BaseFragment?>: Fragment() if (isRegisteredEventBus()) { EventBusUtil.unregister(this) } - mPresenter?.onDestroy() - onDestroyViewAndThing() super.onDestroyView() } - //销毁 Fragment 时,可能需要做些 解除广播绑定,服务之类的鬼东西 - protected abstract fun onDestroyViewAndThing() - } \ No newline at end of file diff --git a/AndroidKit/src/main/java/com/zhumj/androidkit/base/BasePresenter.kt b/AndroidKit/src/main/java/com/zhumj/androidkit/base/BasePresenter.kt index 1ec5848..4912d81 100644 --- a/AndroidKit/src/main/java/com/zhumj/androidkit/base/BasePresenter.kt +++ b/AndroidKit/src/main/java/com/zhumj/androidkit/base/BasePresenter.kt @@ -1,5 +1,7 @@ package com.zhumj.androidkit.base +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner import java.lang.ref.Reference import java.lang.ref.WeakReference @@ -8,7 +10,7 @@ import java.lang.ref.WeakReference * @date 2022/4/23 * @description MVP 中的 Presenter 基类 */ -open class BasePresenter(view: V) { +open class BasePresenter(view: V): DefaultLifecycleObserver { private var mViewRef: Reference? = null @@ -45,7 +47,8 @@ open class BasePresenter(view: V) { val view: V? get() = mViewRef?.get() - open fun onDestroy() { + override fun onDestroy(owner: LifecycleOwner) { detachView() + super.onDestroy(owner) } } \ No newline at end of file diff --git a/README.md b/README.md index 5c798aa..e9d0b49 100644 --- a/README.md +++ b/README.md @@ -172,9 +172,6 @@ open fun onReceiveEvent(event: EventMessage<*>) { @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) open fun onReceiveStickyEvent(event: EventMessage<*>) { } - -//销毁 Fragment 时,可能需要做些 解除广播绑定,服务之类的鬼东西 -protected abstract fun onDestroyViewAndThing() ``` #### BasePresenter @@ -809,3 +806,4 @@ interface OnZAlertDialogButtonClickListener { 2. 新增 Preferences DataStore 替代 SharedPreferences,使用更简单,详情请看 PreferencesDataStoreExt 3. 新增 GsonUtil 工具类 4. ZAlertDialog 按钮新增简体中文适配 +5. 修改 BasePresenter:继承 DefaultLifecycleObserver,使用 Lifecycle 自动感知生命周期 diff --git a/app/build.gradle b/app/build.gradle index c74b262..6651d50 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -38,5 +38,8 @@ android { dependencies { implementation project(':AndroidKit') + implementation 'androidx.appcompat:appcompat:1.3.0' + implementation 'com.google.android.material:material:1.4.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' // implementation 'com.github.zhumj:AndroidKit:1.0.6' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 12e67f9..87a4473 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,11 @@ android:supportsRtl="true" android:theme="@style/Theme.AndroidKitProject"> + diff --git a/app/src/main/java/com/zhumj/androidkitproject/MainActivity.kt b/app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity.kt similarity index 97% rename from app/src/main/java/com/zhumj/androidkitproject/MainActivity.kt rename to app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity.kt index 9a19387..c9ec1db 100644 --- a/app/src/main/java/com/zhumj/androidkitproject/MainActivity.kt +++ b/app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity.kt @@ -1,7 +1,8 @@ -package com.zhumj.androidkitproject +package com.zhumj.androidkitproject.activity import android.Manifest import android.annotation.SuppressLint +import android.content.Intent import android.location.Address import android.os.Bundle import android.util.Log @@ -25,6 +26,7 @@ import com.zhumj.androidkit.premulticlick.OnPreMultiClickListener import com.zhumj.androidkit.utils.GsonUtil import com.zhumj.androidkit.utils.LocationUtil import com.zhumj.androidkit.widget.ZAlertDialog +import com.zhumj.androidkitproject.R import com.zhumj.androidkitproject.databinding.ActivityMainBinding import com.zhumj.androidkitproject.mvp.contract.MainContract import com.zhumj.androidkitproject.mvp.presenter.MainPresenter @@ -61,6 +63,10 @@ class MainActivity : BaseActivity(), MainCon mViewBinding.btnPDSGet.setOnClickListener(onPreMultiClickListener) mViewBinding.btnPDSClean.setOnClickListener(onPreMultiClickListener) + mViewBinding.tvText.setOnClickListener { + startActivity(Intent(this, MainActivity2::class.java)) + } + locationUtil = LocationUtil(this) } diff --git a/app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity2.kt b/app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity2.kt new file mode 100644 index 0000000..d5b52ed --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/activity/MainActivity2.kt @@ -0,0 +1,34 @@ +package com.zhumj.androidkitproject.activity + +import android.os.Bundle +import android.view.MenuItem +import com.zhumj.androidkit.base.BaseActivity +import com.zhumj.androidkitproject.adapter.Main2ViewPagerAdapter +import com.zhumj.androidkitproject.databinding.ActivityMain2Binding +import com.zhumj.androidkitproject.mvp.contract.Main2Contract +import com.zhumj.androidkitproject.mvp.presenter.Main2Presenter + +class MainActivity2 : BaseActivity(), Main2Contract.View2 { + + override fun getViewBinding(): ActivityMain2Binding { + return ActivityMain2Binding.inflate(layoutInflater) + } + + override fun obtainPresenter(): Main2Presenter { + return Main2Presenter(this) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + initToolBar(true, isShowTitle = true) + mViewBinding.viewPager2.adapter = Main2ViewPagerAdapter(supportFragmentManager, lifecycle) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == android.R.id.home) { + finish() + return true + } + return super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/adapter/Main2ViewPagerAdapter.kt b/app/src/main/java/com/zhumj/androidkitproject/adapter/Main2ViewPagerAdapter.kt new file mode 100644 index 0000000..a0a58f0 --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/adapter/Main2ViewPagerAdapter.kt @@ -0,0 +1,34 @@ +package com.zhumj.androidkitproject.adapter + +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager +import androidx.lifecycle.Lifecycle +import androidx.viewpager2.adapter.FragmentStateAdapter +import com.zhumj.androidkitproject.fragment.Main2Fragment + +/** + * @Author Created by zhumj + * @Date 2022/8/24 9:50 + * @Description 文件描述 + */ +class Main2ViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : + FragmentStateAdapter(fragmentManager, lifecycle) { + + override fun getItemCount(): Int = 3 + + override fun createFragment(position: Int): Fragment { + return Main2Fragment.newInstance( + when (position) { + 0 -> { + "Main2 Fragment1" + } + 1 -> { + "Main2 Fragment2" + } + else -> { + "Main2 Fragment3" + } + } + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/fragment/Main2Fragment.kt b/app/src/main/java/com/zhumj/androidkitproject/fragment/Main2Fragment.kt new file mode 100644 index 0000000..94bccd3 --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/fragment/Main2Fragment.kt @@ -0,0 +1,49 @@ +package com.zhumj.androidkitproject.fragment + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.zhumj.androidkit.base.BaseFragment +import com.zhumj.androidkitproject.databinding.FragmentMain2Binding +import com.zhumj.androidkitproject.mvp.contract.Main2F1Contract +import com.zhumj.androidkitproject.mvp.presenter.Main2F1Presenter + +private const val ARG_PARAM = "param" + +class Main2Fragment : BaseFragment(), Main2F1Contract.View { + private var param: String = "Main2 Fragment1" + + override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentMain2Binding { + return FragmentMain2Binding.inflate(inflater, container, false) + } + + override fun obtainPresenter(): Main2F1Presenter = Main2F1Presenter(this) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + arguments?.let { + param = it.getString(ARG_PARAM) ?: "Main2 Fragment1" + } ?: tag.let { + param = it ?: "Main2 Fragment1" + } + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + mPresenter?.tag = param + mViewBinding.tvText.post { + mViewBinding.tvText.text = param + } + } + + companion object { + @JvmStatic + fun newInstance(param: String) = + Main2Fragment().apply { + arguments = Bundle().apply { + putString(ARG_PARAM, param) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2Contract.kt b/app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2Contract.kt new file mode 100644 index 0000000..88bb3c4 --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2Contract.kt @@ -0,0 +1,20 @@ +package com.zhumj.androidkitproject.mvp.contract + +import androidx.lifecycle.LifecycleCoroutineScope + +/** + * 这里是 MVP 定义接口的地方 + */ +interface Main2Contract { + interface Model2 { + suspend fun queryDates(): List + } + + interface View2 { + + } + + interface Presenter2 { + fun queryDates(lifecycleScope: LifecycleCoroutineScope) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2F1Contract.kt b/app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2F1Contract.kt new file mode 100644 index 0000000..a5e8a23 --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/mvp/contract/Main2F1Contract.kt @@ -0,0 +1,18 @@ +package com.zhumj.androidkitproject.mvp.contract + +/** + * 这里是 MVP 定义接口的地方 + */ +interface Main2F1Contract { + interface Model { + + } + + interface View { + + } + + interface Presenter { + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/mvp/model/Main2Model.kt b/app/src/main/java/com/zhumj/androidkitproject/mvp/model/Main2Model.kt new file mode 100644 index 0000000..336347c --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/mvp/model/Main2Model.kt @@ -0,0 +1,16 @@ +package com.zhumj.androidkitproject.mvp.model + +import com.zhumj.androidkitproject.mvp.contract.Main2Contract +import kotlinx.coroutines.delay + +/** + * 这里在 MVP 中是真正执行数据请求的地方 + */ +class Main2Model: Main2Contract.Model2 { + + override suspend fun queryDates(): List { + delay(2000) + return arrayListOf(0, 1, 2, 3, 4) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2F1Presenter.kt b/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2F1Presenter.kt new file mode 100644 index 0000000..105acdf --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2F1Presenter.kt @@ -0,0 +1,20 @@ +package com.zhumj.androidkitproject.mvp.presenter + +import android.util.Log +import com.zhumj.androidkit.base.BasePresenter +import com.zhumj.androidkitproject.mvp.contract.Main2F1Contract +import androidx.lifecycle.LifecycleOwner + +/** + * 这里是 MVP 中对逻辑进行处理的地方,通过 View 回调告诉 Activity 如何更新 UI + */ +class Main2F1Presenter(view: Main2F1Contract.View): BasePresenter(view), Main2F1Contract.Presenter { + + var tag = "Main2 Fragment1" + + override fun onDestroy(owner: LifecycleOwner) { + Log.d("1111111111111111111", "${tag}: onDestroy(owner: LifecycleOwner)") + super.onDestroy(owner) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2Presenter.kt b/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2Presenter.kt new file mode 100644 index 0000000..4480c99 --- /dev/null +++ b/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/Main2Presenter.kt @@ -0,0 +1,32 @@ +package com.zhumj.androidkitproject.mvp.presenter + +import android.util.Log +import com.zhumj.androidkit.base.BasePresenter +import com.zhumj.androidkitproject.mvp.contract.Main2Contract +import com.zhumj.androidkitproject.mvp.model.Main2Model +import androidx.lifecycle.LifecycleCoroutineScope +import androidx.lifecycle.LifecycleOwner +import kotlinx.coroutines.launch + +/** + * 这里是 MVP 中对逻辑进行处理的地方,通过 View 回调告诉 Activity 如何更新 UI + */ +class Main2Presenter(view: Main2Contract.View2): BasePresenter(view), Main2Contract.Presenter2 { + + private val model = Main2Model() + var dates: List = ArrayList() + + override fun queryDates(lifecycleScope: LifecycleCoroutineScope) { + lifecycleScope.launch { + dates = model.queryDates().also { + + } + } + } + + override fun onDestroy(owner: LifecycleOwner) { + Log.d("1111111111111111111", "Main2Presenter: onDestroy(owner: LifecycleOwner)") + super.onDestroy(owner) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/MainPresenter.kt b/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/MainPresenter.kt index e5d95d3..71efad9 100644 --- a/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/MainPresenter.kt +++ b/app/src/main/java/com/zhumj/androidkitproject/mvp/presenter/MainPresenter.kt @@ -4,6 +4,7 @@ import com.zhumj.androidkit.base.BasePresenter import com.zhumj.androidkitproject.mvp.contract.MainContract import com.zhumj.androidkitproject.mvp.model.MainModel import androidx.lifecycle.LifecycleCoroutineScope +import androidx.lifecycle.LifecycleOwner import kotlinx.coroutines.launch /** @@ -26,4 +27,8 @@ class MainPresenter(view: MainContract.View): BasePresenter(v } } + override fun onDestroy(owner: LifecycleOwner) { + super.onDestroy(owner) + } + } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f53b230..34a4706 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,7 +5,7 @@ android:id="@+id/clRootLayout" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context=".activity.MainActivity"> + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_main2.xml b/app/src/main/res/layout/fragment_main2.xml new file mode 100644 index 0000000..da94a2c --- /dev/null +++ b/app/src/main/res/layout/fragment_main2.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_main2_fragment.xml b/app/src/main/res/layout/item_main2_fragment.xml new file mode 100644 index 0000000..8fd9413 --- /dev/null +++ b/app/src/main/res/layout/item_main2_fragment.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file