package com.bw.lib_mvp;
public class Event {
private String targert;
private Object data;
private int type;
public Event(String targert, Object data, int type) {
this.targert = targert;
this.data = data;
this.type = type;
}
public String getTargert() {
return targert;
}
public void setTargert(String targert) {
this.targert = targert;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
package com.bw.lib_mvp.model;
public abstract class BaseModel <S>{
protected S apisServer;
public BaseModel(S apisServer) {
this.apisServer = createpresenter();
}
protected abstract S createpresenter();
}
package com.bw.lib_mvp.presenter;
import com.bw.lib_mvp.model.BaseModel;
import com.bw.lib_mvp.view.BaseView;
import java.lang.ref.WeakReference;
import io.reactivex.disposables.CompositeDisposable;
public abstract class BasePresneter<V extends BaseView,M extends BaseModel> {
protected M baseModel;
public BasePresneter(M baseModel) {
this.baseModel = creatModel();
}
protected abstract M creatModel();
private WeakReference<V>weakReference;
protected CompositeDisposable disposable =new CompositeDisposable();
protected boolean isAttacheview=false;
public void attchView(V view){
weakReference=new WeakReference<>(view);
isAttacheview=true;
}
public void detachView(){
if(weakReference!=null&&weakReference.get()!=null){
weakReference.clear();
weakReference=null;
}
if(disposable.isDisposed()&&disposable.size()>0&&disposable!=null){
disposable.dispose();
disposable.clear();
}
}
public V getview(){
return weakReference.get()!=null?weakReference.get():null;
}
}
package com.bw.lib_mvp.view;
public interface BaseView {
void onOk(String msg);
void onErrot(String error);
}
package com.bw.lib_mvp.view;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.bw.lib_mvp.Event;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public abstract class BaseAcitviy extends AppCompatActivity implements BaseView {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayouid());
initData();
initView();
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
protected abstract void initView();
protected abstract void initData();
protected abstract int getLayouid();
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRecser(Event event){
if(event==null||TextUtils.isEmpty(event.getTargert()))return;
if(event.getTargert().equals(getClass().getSimpleName())){
hadwesd(event);
}
}
private void hadwesd(Event event) {
}
@Override
public void onOk(String msg) {
if(TextUtils.isEmpty(msg))return;
ShowTast(msg);
}
@Override
public void onErrot(String error) {
if(TextUtils.isEmpty(error))return;
ShowTast(error);
}
public void ShowTast(String msg){
Toast toast=Toast.makeText(this,msg,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,10);
toast.show();
}
}
package com.bw.lib_mvp.view;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.bw.lib_mvp.Event;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public abstract class BaseFragment extends Fragment implements BaseView {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(getLayouid(),null);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initData();
initView();
EventBus.getDefault().register(this);
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onResrs(Event event){
if(event==null|| TextUtils.isEmpty(event.getTargert()))return;
if(event.getTargert().equals(getClass().getSimpleName())){
hawsese(event);
}
}
private void hawsese(Event event) {
}
protected abstract void initView();
protected abstract void initData();
protected abstract int getLayouid();
@Override
public void onOk(String msg) {
if(TextUtils.isEmpty(msg))
showToast(msg);
}
@Override
public void onErrot(String error) {
if(TextUtils.isEmpty(error))
showToast(error);
}
public void showToast(String msg){
Toast toast=Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,10);
toast.show();
}
}
package com.bw.lib_mvp.view;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.bw.lib_mvp.Event;
import com.bw.lib_mvp.presenter.BasePresneter;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public abstract class BaseMvpAcivity<P extends BasePresneter> extends AppCompatActivity implements BaseView{
protected P preseneter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
if(preseneter==null){
preseneter=createpresenter();
}
preseneter.attchView(this);
initView();
initData();
EventBus.getDefault().register(this);
}
protected abstract void initView();
protected abstract void initData();
protected abstract P createpresenter();
protected abstract int getLayout();
@Override
protected void onDestroy() {
super.onDestroy();
if(preseneter!=null){
preseneter.detachView();
}
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRecevie(Event event){
if(event==null&& TextUtils.isEmpty(event.getTargert()))return;
if(event.getTargert().equals(getClass().getSimpleName())){
handEvent(event);
}
}
private void handEvent(Event event) {
}
@Override
public void onOk(String msg) {
if(TextUtils.isEmpty(msg))return;
showcommcotast(msg);
}
@Override
public void onErrot(String error) {
if(TextUtils.isEmpty(error))return;
showcommcotast(error);
}
public void showcommcotast(String msg){
Toast toast=Toast.makeText(this,msg,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,10);
toast.show();
}
}
package com.bw.lib_mvp.view;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.bw.lib_mvp.Event;
import com.bw.lib_mvp.presenter.BasePresneter;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public abstract class BaseMvpFragment<P extends BasePresneter>extends Fragment implements BaseView {
protected P presenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(getLayouId(),null);
return view;
}
protected abstract int getLayouId();
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
if (presenter==null){
presenter=creasetpresenetr();
}
presenter.attchView(this);
EventBus.getDefault().register(this);
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
if(presenter!=null){
presenter.detachView();
}
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRecsert(Event event){
if(event==null|| TextUtils.isEmpty(event.getTargert()))return;
if(event.getTargert().equals(getClass().getSimpleName())){
hasdiise(event);
}
}
private void hasdiise(Event event) {
}
protected abstract P creasetpresenetr();
@Override
public void onOk(String msg) {
if(TextUtils.isEmpty(msg))return;
showToast(msg);
}
@Override
public void onErrot(String error) {
if(TextUtils.isEmpty(error))return;
showToast(error);
}
public void showToast(String msg){
Toast toast=Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,10);
toast.show();
}
}