MVP1(乞丐版)

MvpActivity

public class MvpActivity extends Activity implements View.OnClickListener, IView {
    EditText mEtName, mEtPw;
    LoginPresenter mLoginPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp);

        initView();

        //创建一个Presenter实例
        initPresenter();
    }

    /**
     * 绑定Presenter
     */
    private void initPresenter() {
        //把view传给presenter进行绑定
        mLoginPresenter = new LoginPresenter(this);
    }

    private void initView() {
        mEtName = findViewById(R.id.et_name);
        mEtPw = findViewById(R.id.et_pw);

        mEtPw.invalidate();
        findViewById(R.id.button_login).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.button_login:
                User user = new User(mEtName.getText().toString(), mEtPw.getText().toString());
                //通过presenter的实例,调用presenter中的方法
                mLoginPresenter.submit(user);
                break;
            default:
                break;
        }
    }

    /**
     * 在Activity結束的時候解綁Presenter
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLoginPresenter.detachView();
    }

    @Override
    public void success(Object data) {
        //接受到了结果,进行数据展示
        Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void fail(String msg) {
        //接受到了结果,进行数据展示
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}

LoginPresenter

public class LoginPresenter {
    /**
     *  持有view的实例
     */

    private IView mIView;

    public LoginPresenter(@NonNull IView iView) {
        mIView = iView;
    }

    public void submit(User user){
        if(checkName(user.getName()) && checkPw(user.getPw())){
            //进行网络请求
            boolean loginResult = NetUtils.loginApi(user);
            //拿到结果后
            if (loginResult){
                //通过view的实例,把数据回调给view
                mIView.success("");
            }else{
                //通过view的实例,把数据回调给view
                mIView.fail("失败");
            }
        }else{
            //通过view的实例,把数据回调给view
            mIView.fail("用户名密码错");
        }
    }


    public void detachView(){
        mIView = null;
    }



    private boolean checkName(String name){
        return !TextUtils.isEmpty(name);
    }

    private boolean checkPw(String pw){
        return (!TextUtils.isEmpty(pw) && pw.length()>=6);
    }
}

IView

public interface IView {
     void success(T data);
     void fail(String msg);
}

NetUtils网络请求数据

public class NetUtils {
    public static boolean loginApi(User user){
        SystemClock.sleep(2000);
        if(user.getName().equals("dj") && user.getPw().equals("123456")){
            //TODO:这里最终需用真正的接口
            return true;
        }
        return false;
    }
}

User bean类

public class User {
    private String name;
    private String pw;

    public User(String name, String pw) {
        this.name = name;
        this.pw = pw;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }
}

 

你可能感兴趣的:(MVP1(乞丐版))