拥有生命周期的Presenter的MVP框架,支持Multi P

拥有生命周期的Presenter的MVP框架,支持Multi P

Android小彩虹2021-07-10 18:34:17170A+A-

结合自己对MVP的理解,加上个人习惯,整理出这么一份Android MVP框架

  • 通过注解动态注入Presenter
  • Presenter拥有完整生命周期
  • 支持多Presenter注入
  • 状态复原

0x00 前言

此框架可以说是Fork nucleus 而来,只不过结合了个人理解和习惯进行更改,放上此框架链接 github.com/izyhang/Dam…

0x01 介绍

先介绍一下Damon的简单使用

新建View、Presenter

Presenter具有与Activity一样的生命周期,这里展示部分生命周期

interface MainView {
    fun log(msg: String)
}

class MainPresenter : BasePresenter<MainView>() {
    override fun onCreate(arguments: Bundle?, savedState: Bundle?) {
        super.onCreate(arguments, savedState)
        view.log("MainPresenter.onCreate")
    }

    override fun onResume() {
        super.onResume()
        view.log("MainPresenter.onResume")
    }

    override fun onPause() {
        super.onPause()
        view.log("MainPresenter.onPause")
    }

    override fun onDestroy() {
        super.onDestroy()
        view.log("MainPresenter.onDestroy")
    }
}

修改MainActivity

MainActivity绑定Presenter,想要拿到Presenter通过@BindPresenter获取

@RequiresPresenter(MainPresenter::class)
class MainActivity : BaseActivity(), MainView {
    @BindPresenter
    private var mainPresenter: MainPresenter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mainPresenter.xxx()
    }

    override fun log(msg: String) {
        println(msg)
    }
}

多Presenter注入

@RequiresPresenter(value = [MainPresenter::class, SecondPresenter::class])
class MainActivity : BaseActivity(), MainView, SecondView {
    @BindPresenter
    private var mainPresenter: MainPresenter? = null
    @BindPresenter
    private var secondPresenter: SecondPresenter? = null

    override fun log(msg: String) {
        println(msg)
    }
}

0x02 工作原理

下面介绍一下Damon的工作原理

Presenter的动态注入和赋值

用注解代替手动实例化Presenter更加方便,并且在封装好的Activity实现了状态恢复,避免重复创建Presenter

在界面创建之时会传入当前classReflectionPresenterFactory,通过cls.getAnnotation拿到@RequiresPresenter注解并保存,在这里也对当前class@BindPresenter注解的成员进行保存

@Nullable
public static ReflectionPresenterFactory fromViewClass(Object host, Class<?> cls) {
    RequiresPresenter annotation = cls.getAnnotation(RequiresPresenter.class);
    Class<? extends MvpPresenter>[] pClass = null != annotation ? annotation.value() : null;
    if (null == pClass) {
        return null;
    }
    List<Field> fields = new ArrayList<>();
    for (Field field : cls.getDeclaredFields()) {
        Annotation[] annotations = field.getDeclaredAnnotations();
        if (annotations.length < 1) {
            continue;
        }
        if (annotations[0] instanceof BindPresenter) {
            fields.add(field);
        }
    }
    return new ReflectionPresenterFactory(host, pClass, fields);
}

Presenter的创建

上面讲过DamonPresenter是具有完整生命周期的,看到封装的Activity MvpAppCompatActivity.java

Activity的生命周期通过PresenterLifecycleDelegate去控制Presenter的方法调用,赋予其生命周期。其中还重载了onSaveInstanceState辅助Presenter状态恢复机制

public class MvpAppCompatActivity extends AppCompatActivity implements MvpView {

    private static final String PRESENTER_STATE_KEY = "presenter_state";
    private PresenterLifecycleDelegate mPresenterDelegate =
            new PresenterLifecycleDelegate(ReflectionPresenterFactory.fromViewClass(this, getClass()));

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenterDelegate.onCreate(this, getIntent().getExtras(), null != savedInstanceState ? savedInstanceState.getBundle(PRESENTER_STATE_KEY) : null);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(PRESENTER_STATE_KEY, mPresenterDelegate.onSaveInstanceState());
    }

    @Override
    public void onStart() {
        super.onStart();
        mPresenterDelegate.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenterDelegate.onResume();
    }

    @Override
    public void onPause() {
        mPresenterDelegate.onPause();
        super.onPause();
    }

    @Override
    public void onStop() {
        mPresenterDelegate.onStop();
        super.onStop();
    }

    @Override
    public void onDestroy() {
        mPresenterDelegate.onDestroy(!isChangingConfigurations());
        super.onDestroy();
    }
}

PresenterLifecycleDelegate

最重要的就是此类,连接ActivityPresenter的枢纽,控制Presenter的创建、恢复、生命周期

public class PresenterLifecycleDelegate {

    private static final String PRESENTER_KEY = "presenter - ";
    private static final String PRESENTER_ID_KEYS = "presenter_ids";

    @Nullable
    private PresenterFactory mPresenterFactory;
    @Nullable
    private List<? extends MvpPresenter> mPresenters;

    private boolean mPresenterHasView;

    public PresenterLifecycleDelegate(@Nullable PresenterFactory presenterFactory) {
        this.mPresenterFactory = presenterFactory;
    }

    public void onCreate(MvpView view, @Nullable Bundle arguments, @Nullable Bundle savedState) {
        if (mPresenterFactory == null) {
            return;
        }
        Bundle presenterBundle = null;
        if (savedState != null) {
            presenterBundle = ParcelFn.unmarshall(ParcelFn.marshall(savedState));
        }
        createPresenter(presenterBundle);
        if (mPresenters != null && !mPresenters.isEmpty()) {
            mPresenterFactory.bindPresenter(mPresenters);
            for (MvpPresenter presenter : mPresenters) {
                //noinspection unchecked
                presenter.create(view, arguments, null != presenterBundle ? presenterBundle.getBundle(PRESENTER_KEY.concat(presenter.getClass().getSimpleName())) : null);
            }
        }
    }

    private void createPresenter(Bundle presenterBundle) {
        if (presenterBundle != null) {
            mPresenters = PresenterStorage.INSTANCE.getPresenter(presenterBundle.getStringArray(PRESENTER_ID_KEYS));
        }

        if (mPresenters == null) {
            //noinspection ConstantConditions
            mPresenters = mPresenterFactory.createPresenter();
            PresenterStorage.INSTANCE.add(mPresenters);
        }
    }

    public void onStart() {
        if (mPresenters != null && !mPresenters.isEmpty()) {
            for (MvpPresenter presenter : mPresenters) {
                presenter.start();
            }
        }
    }

    public Bundle onSaveInstanceState() {
        Bundle bundle = new Bundle();
        if (mPresenters != null && !mPresenters.isEmpty()) {
            String[] ids = new String[mPresenters.size()];
            for (MvpPresenter presenter : mPresenters) {
                Bundle presenterBundle = new Bundle();
                presenter.save(presenterBundle);
                bundle.putBundle(PRESENTER_KEY.concat(presenter.getClass().getSimpleName()), presenterBundle);

                ids[mPresenters.indexOf(presenter)] = PresenterStorage.INSTANCE.getId(presenter);
            }
            bundle.putStringArray(PRESENTER_ID_KEYS, ids);
        }
        return bundle;
    }

    public void onResume() {
        if (mPresenters != null && !mPresenters.isEmpty() && !mPresenterHasView) {
            for (MvpPresenter presenter : mPresenters) {
                presenter.resume();
            }
            mPresenterHasView = true;
        }
    }

    public void onPause() {
        if (mPresenters != null && !mPresenters.isEmpty() && mPresenterHasView) {
            for (MvpPresenter presenter : mPresenters) {
                presenter.pause();
            }
            mPresenterHasView = false;
        }
    }

    public void onStop() {
        if (mPresenters != null && !mPresenters.isEmpty()) {
            for (MvpPresenter presenter : mPresenters) {
                presenter.stop();
            }
        }
    }

    public void onDestroy(boolean isFinal) {
        if (isFinal && mPresenters != null && !mPresenters.isEmpty()) {
            for (MvpPresenter presenter : mPresenters) {
                presenter.destroy();
            }
            mPresenters.clear();
            mPresenters = null;
        }
    }
}

0x03 讲在最后

该框架实现的Presenter恢复机制,存储的id不止是单纯的Presenter类名,大可不必担心Presenter复用混乱的情况,具体可以看 PresenterStorage.java 的实现

此次说讲Multi Presenter版本的Damon是新变支的2.0.0-alpha,未经过大量测试。 不过此前的Single Presenter版本的Damon,版本号1.1.0,则在开发环境稳定运行许久

最后再放上项目链接github.com/izyhang/Dam…

欢迎大家指出不足、留下您的意见~

点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1

联系我们