Skip to content

Commit

Permalink
修改 BasePresenter:继承 DefaultLifecycleObserver,使用 Lifecycle 自动感知生命周期
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumj committed Aug 24, 2022
1 parent 4f96e63 commit 1dda7e3
Show file tree
Hide file tree
Showing 20 changed files with 303 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ abstract class BaseActivity<VB : ViewBinding, BP: BasePresenter<*>?>: AppCompatA
private fun init() {
if (mPresenter == null) {
mPresenter = obtainPresenter()
if (mPresenter != null) {
lifecycle.addObserver(mPresenter!!)
}
}
if (isRegisteredEventBus()) {
EventBusUtil.register(this)
Expand Down Expand Up @@ -100,7 +103,6 @@ abstract class BaseActivity<VB : ViewBinding, BP: BasePresenter<*>?>: AppCompatA
if (isRegisteredEventBus()) {
EventBusUtil.unregister(this)
}
mPresenter?.onDestroy()
super.onDestroy()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ abstract class BaseFragment<VB : ViewBinding, BP: BasePresenter<*>?>: Fragment()
private fun init() {
if (mPresenter == null) {
mPresenter = obtainPresenter()
if (mPresenter != null) {
lifecycle.addObserver(mPresenter!!)
}
}

if (isRegisteredEventBus()) {
Expand All @@ -41,11 +44,11 @@ abstract class BaseFragment<VB : ViewBinding, BP: BasePresenter<*>?>: 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?

/**
Expand Down Expand Up @@ -94,12 +97,7 @@ abstract class BaseFragment<VB : ViewBinding, BP: BasePresenter<*>?>: Fragment()
if (isRegisteredEventBus()) {
EventBusUtil.unregister(this)
}
mPresenter?.onDestroy()
onDestroyViewAndThing()
super.onDestroyView()
}

//销毁 Fragment 时,可能需要做些 解除广播绑定,服务之类的鬼东西
protected abstract fun onDestroyViewAndThing()

}
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -8,7 +10,7 @@ import java.lang.ref.WeakReference
* @date 2022/4/23
* @description MVP 中的 Presenter 基类
*/
open class BasePresenter<out V>(view: V) {
open class BasePresenter<out V>(view: V): DefaultLifecycleObserver {

private var mViewRef: Reference<V>? = null

Expand Down Expand Up @@ -45,7 +47,8 @@ open class BasePresenter<out V>(view: V) {
val view: V?
get() = mViewRef?.get()

open fun onDestroy() {
override fun onDestroy(owner: LifecycleOwner) {
detachView()
super.onDestroy(owner)
}
}
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -809,3 +806,4 @@ interface OnZAlertDialogButtonClickListener {
2. 新增 Preferences DataStore 替代 SharedPreferences,使用更简单,详情请看 PreferencesDataStoreExt
3. 新增 GsonUtil 工具类
4. ZAlertDialog 按钮新增简体中文适配
5. 修改 BasePresenter:继承 DefaultLifecycleObserver,使用 Lifecycle 自动感知生命周期
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
android:supportsRtl="true"
android:theme="@style/Theme.AndroidKitProject">
<activity
android:name=".MainActivity"
android:name=".activity.MainActivity2"
android:label="MainActivity2"
android:exported="false" />
<activity
android:name=".activity.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -61,6 +63,10 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainPresenter>(), MainCon
mViewBinding.btnPDSGet.setOnClickListener(onPreMultiClickListener)
mViewBinding.btnPDSClean.setOnClickListener(onPreMultiClickListener)

mViewBinding.tvText.setOnClickListener {
startActivity(Intent(this, MainActivity2::class.java))
}

locationUtil = LocationUtil(this)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ActivityMain2Binding, Main2Presenter>(), 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)
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
)
}
}
Original file line number Diff line number Diff line change
@@ -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<FragmentMain2Binding, Main2F1Presenter>(), 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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.zhumj.androidkitproject.mvp.contract

import androidx.lifecycle.LifecycleCoroutineScope

/**
* 这里是 MVP 定义接口的地方
*/
interface Main2Contract {
interface Model2 {
suspend fun queryDates(): List<Int>
}

interface View2 {

}

interface Presenter2 {
fun queryDates(lifecycleScope: LifecycleCoroutineScope)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.zhumj.androidkitproject.mvp.contract

/**
* 这里是 MVP 定义接口的地方
*/
interface Main2F1Contract {
interface Model {

}

interface View {

}

interface Presenter {

}
}
Original file line number Diff line number Diff line change
@@ -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<Int> {
delay(2000)
return arrayListOf(0, 1, 2, 3, 4)
}

}
Original file line number Diff line number Diff line change
@@ -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<Main2F1Contract.View>(view), Main2F1Contract.Presenter {

var tag = "Main2 Fragment1"

override fun onDestroy(owner: LifecycleOwner) {
Log.d("1111111111111111111", "${tag}: onDestroy(owner: LifecycleOwner)")
super.onDestroy(owner)
}

}
Original file line number Diff line number Diff line change
@@ -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<Main2Contract.View2>(view), Main2Contract.Presenter2 {

private val model = Main2Model()
var dates: List<Int> = 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)
}

}
Loading

0 comments on commit 1dda7e3

Please sign in to comment.