服务状态机的实现
需求背景
现状
上门服务和取送车服务流程比较复杂,状态比较多,且各个状态可相互切换,因此采用状态机来实现该功能需求
方案概述
- 利用系统的GameplayKit中的GKState、GKStateMachine建立状态机模式,每一个状态存放一个ui视图
对于GKState中各事件的响应,采用响应链机制将事件往vc抛,由vc处理相关事件响应
状态图
-
取送车状态图
-
上门呼叫状态图
主要代码
控制器:
@property (nonatomic, strong) YICPickCarStateManager *stateManager;
_stateManager = [YICPickCarStateManager managerWithContainerView:self.orderView.topView];
// 根据具体业务进入对应的状态
[_stateManager enterState:YICPickCarWaitPickCarState.class userInfo:@{@"data": orderDetail}];
具体状态类:
#import "YICBaseViewState.h"
@interface YICXXXState : YICBaseViewState
@end
@implementation YICXXXState
- (void)didEnterWithPreviousState:(YICBaseViewState *)previousState{
[super didEnterWithPreviousState:previousState];
self.contentView = UIView.new;
self.contentView.backgroundColor = UIColor.clearColor;
[self.containerView addSubview:self.contentView];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.containerView);
}];
// 具体事件
@end