一个MVP模式的实例
在MVP模式中,P通过接口依赖 M 和 V,且 P 尽量少的依赖其他类。
MVP三个角色就像三个领导,分别负责:数据,视图,业务 三大逻辑。
而项目中的其他类,就相当于专业人员,负责好自己的岗位工作,随时等候领导的调用。
PacketProgressActivity.java (MVP模式的V)
// 通讯等待界面
代码逻辑
// 启动入口:
// 入参:包类型,组包信息
public static void actionStart(Context context, int packetType, Bundle info) {
mPacketType = packetType;
Intent intent = new Intent(context, PacketProgressActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(PacketTypes.class.getSimpleName(), packetType);
if (info != null) {
intent.putExtra(PACKET_INFO, info);
}
context.startActivity(intent);
}
// 包类型,组包信息 都放入 intent,接着启动自己
调用链条
--> PacketProgressActivity.onCreate ( V 端)
{
super.onCreate(savedInstanceState);
--> BaseMvpActivity.onCreate
{
// 实例化了 Presenter ( P 端)
mPresenter = createPresenter();
{
return StrategySelector.selectPresenterStrategy(mPacketType);
}
// p
mPresenter.attachView((V) this);
}
init();
}
--> PacketProgressActivity.init (V 端)
{
// 初始化视图组件
// 初始化 Presenter,使得 Presenter 持有 V 和 M 的引用
mPresenter.init();
{
mViewInterface = mViewRef.get(); // 持有 V 的引用
mModelInterface = new PacketOperationModel(this); // 持有 M 的引用
}
// 请求发送 报文包,入参:intent
// 业务转移给 P
mPresenter.requestSendPacket(getIntent());
}
// BasePacketPresenter.java (MVP模式的 P 端)
// (子类)SignInPacketPresenter.java
--> SignInPacketPresenter.requestSendPacket
{
// 不同子类的特定逻辑
handleIcDownload(); // 以签到子类为例子,缓存一个签到后要调用的函数。
{
AidlPboc pboc = DeviceServiceManager.getInstance().getPbocManager();
int aidQueryResult = 0;
Intent intent = new Intent();
try {
// 是否存在 公钥
aidQueryResult = pboc.isExistAidPublicKey();
}
// 公钥不存在,则缓存后续操作
switch (aidQueryResult) {
case -0x01:
case -0x02:
intent.putExtra(PacketTypes.class.getSimpleName()
, PacketTypes.PACKET_IC_PARA_STATUS_QUERY);
mHandleCache.addRequestCache(intent);
break;
case -0x03:
intent.putExtra(PacketTypes.class.getSimpleName()
, PacketTypes.PACKET_IC_CAPK_STATUS_QUERY);
mHandleCache.addRequestCache(intent);
break;
default:
break;
}
}
// 调用父类的逻辑
super.requestSendPacket(typeIntent);
}
--> BasePacketPresenter.requestSendPacket (P 端)
{
// 发送报文包
// 业务交给 model 端
mModelInterface.sendPacket(mPacketType, mPacketExtraInfo);
}
--> PacketOperationModel.sendPacket (M 端)
{
// M 端 组包
mPacket = StrategySelector.selectPacketStrategy(packetType, info);
// 网络通讯,交给专业人员,异步任务类
mPacketTransferTask = new PacketTransferTask(mContext, this, mPacket);
mPacketTransferTask.execute(sendPacket);
}
--> PacketTransferTask.doInBackground (专业人员 端)
{
// 类通讯结束,返回结果
return SocketEvent.SOCKET_ERROR_CONNECT;
return SocketEvent.SOCKET_SUCCESS;
}
--> PacketTransferTask.onPostExecute (专业人员 端)
{
// 接收到包完成,回调函数
// 报告 M 端
mModelInterface.onReceivePacket(socketResult, mReceivePacket);
}
--> PacketOperationModel.onReceivePacket (M 端)
{
// M 端进行数据处理
// 包不为空,就解包
if (receivedPacket != null) {
mPacket.clearPacket();
mPacket.parsePacket(receivedPacket);
//result = checkMac(receivedPacket);
result = true;
}
// 处理结果 报告 Presenter
if (!result) {
mPacketPresenter.handleMacError();
} else {
mPacketPresenter.handlePacket(socketResult, mPacketResendNum);
}
}
--> BasePacketPresenter.handlePacket ( P 端)
// 收到包后,首先判断是否还需要下载其他包
// 如果需要,把任务添加到 缓存对象 mHandleCache
if (mModelInterface.getProcessRequired() != 0) {
LogUtil.i("mModelInterface.getProcessRequired(): " + mModelInterface.getProcessRequired());
Intent intent = new Intent();
switch (mModelInterface.getProcessRequired()) {
/**
* 下传终端磁条卡参数
*/
case 1:
intent.putExtra(PacketTypes.class.getSimpleName()
, PacketTypes.PACKET_PARAM_TRANS);
mHandleCache.addRequestCache(intent);
break;
/**
* 上传终端磁条卡状态信息
*/
case 2:
intent.putExtra(PacketTypes.class.getSimpleName()
, PacketTypes.PACKET_STATUS_UPLOAD);
mHandleCache.addRequestCache(intent);
break;
/**
* 重新签到
*/
case 3:
intent.putExtra(PacketTypes.class.getSimpleName()
, PacketTypes.PACKET_SIGN_UP);
mHandleCache.addRequestCache(intent);
break;
/**
* 通知终端发起更新公钥信息操作
*/
case 4:
intent.putExtra(PacketTypes.class.getSimpleName()
, PacketTypes.PACKET_IC_CAPK_STATUS_QUERY);
mHandleCache.addRequestCache(intent);
break;
/**
* 下载终端IC卡参数
*/
case 5:
intent.putExtra(PacketTypes.class.getSimpleName()
, PacketTypes.PACKET_IC_PARA_STATUS_QUERY);
mHandleCache.addRequestCache(intent);
break;
/**
* TMS参数下载
*/
case 6:
break;
default:
break;
}
}
handlePacketSocketResult(socketResult, packetResendNum);
--> (子类)SignInPacketPresenter.handlePacketSocketResult ( P 端)
if ("success".equals(bundle.getString(RESULT_DETAIL))) {
if (!packetInfo.getIsSignUp()) {
packetInfo.setIsSignUp(true);
mViewInterface.showResult(bundle, null);
return;
}
if (isLogin) {
mViewInterface.showResult(bundle, AmountInputActivity.class);
} else {
mViewInterface.showResult(bundle, null);
}
} else {
mViewInterface.showResult(bundle, null);
}
--> PacketProgressActivity.showResult ( V 端)
// 判断是否需要后续通讯
// 判断是否有缓存
if (!mHandleCache.isRequestEmpty()) {
mPresenter.handleRequestCache(bundle, targetActivity);
return;
}
//
if (!mHandleCache.isResultEmpty()) {
mPresenter.handleCenterRequiredResult();
return;
}
// 如果没有后续通讯,则展示通讯结果
if (targetActivity != null) {
Intent intent = new Intent(this, targetActivity);
if (bundle != null) {
intent.putExtra(PacketResultActivity.class.getSimpleName(), bundle);
}
if (targetActivity == AmountInputActivity.class) {
ClientInfoManager.getConsumeData().setConsumeType(PacketTypes.PACKET_CONSUME);
ConsumeAmountInputActivity.actionStart(this, null, getString(R.string.title_consume));
} else {
startActivity(intent);
}
} else {
PacketResultActivity.actionStart(this, bundle);
}
finish();
网友评论