public class MainActivity extends AppCompatActivity implements IContract.IView {
private IContract.IPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenter = new PresenterImpl();
presenter.attachView(this);
}
@Override
public void requestDataPre(final String ss) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// Toast.makeText(MainActivity.this, ss, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void requestDataPre1(final String ss1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, ss1, Toast.LENGTH_SHORT).show();
}
});
}
public void login(View view) {
// presenter.requestDataPre();
presenter.requestDataPre1();
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detachView(this);
}
}
public interface IContract {
interface IModel {
interface ResponseStr{
void sendMMsg(String ss);
void sendMMsg1(String ss1);
}
void requestDataMod(ResponseStr responseStr);
void requestDataMod1(ResponseStr responseStr);
}
interface IPresenter<IView> {
void detachView(IView iView);
void attachView(IView iView);
void requestDataPre();
void requestDataPre1();
}
interface IView {
void requestDataPre(String ss);
void requestDataPre1(String ss1);
}
}
public class ModelImpl implements IContract.IModel {
@Override
public void requestDataMod(final ResponseStr responseStr) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
String ss="hahaha";
responseStr.sendMMsg(ss);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public void requestDataMod1(final ResponseStr responseStr1) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
String ss1="哈哈";
responseStr1.sendMMsg1(ss1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
public class PresenterImpl implements IContract.IPresenter<IContract.IView> {
IContract.IView iView;
private IContract.IModel model;
private WeakReference<IContract.IView> iViewWeakReference;
private WeakReference<IContract.IModel> iModelWeakReference;
@Override
public void attachView(IContract.IView iView) {
this.iView = iView;
model = new ModelImpl();
iViewWeakReference = new WeakReference<>(iView);
iModelWeakReference = new WeakReference<>(model);
}
@Override
public void detachView(IContract.IView iView) {
iViewWeakReference.clear();
iModelWeakReference.clear();
}
@Override
public void requestDataPre() {
model.requestDataMod(new IContract.IModel.ResponseStr() {
@Override
public void sendMMsg(String ss) {
Log.d("TAG","开过ss");
iView.requestDataPre(ss);
}
@Override
public void sendMMsg1(String ss1) {
Log.d("TAG","开过ss1");
iView.requestDataPre1(ss1);
}
});
}
@Override
public void requestDataPre1() {
model.requestDataMod1(new IContract.IModel.ResponseStr() {
@Override
public void sendMMsg(String ss) {
}
@Override
public void sendMMsg1(String ss1) {
iView.requestDataPre1(ss1);
}
});
}
}