聊天页状态机(ChatViewModel)
聊天页状态机:ChatViewModel 属于 ViewModel(状态机与业务编排)。本页的 props / events / behaviors 来自 sdk-imkit.json → globalUIKit,声明与示例来自 bindings.ios.uikit,不手写漂移。
契约属性(props)
| 属性 | 类型 | 可选 | 说明 |
|---|---|---|---|
client | IMPClient | 否 | IMLib 客户端(唯一数据源) |
conversationType | IMPConversationType | 否 | 会话类型(单聊 / 群聊 / 聊天室 / 系统) |
targetId | string | 否 | 会话目标 ID(单聊为对方 userId) |
options | {pageSize:int,autoRead:bool} | 是 | 页面选项(由契约声明的可选字段,例如搜索栏 / 网络横幅 / 历史 / 已读 / typing) |
事件(events)
| 事件 | 参数 | 说明 |
|---|---|---|
messagesChanged | change | 消息集合变化(增量) |
sendStateChanged | localId,state | 单条消息发送状态变化 |
typingChanged | userId,typing | 对方输入状态变化 |
行为(behaviors)
- loadHistory(beforeSeq)/loadMore
- sendText/sendContent/resend/recall
- markRead 去重与节流
- 合并 MESSAGES/READ_RECEIPT/MESSAGE_RECALLED 事件
- draft 保存/恢复
公开符号与声明
| 语言 | 声明 |
|---|---|
| Swift | IMPChatViewModel(client: IMPClient, conversationType: IMPConversationType, targetId: String, options: IMPChatViewModelOptions?) |
| Objective-C | - (instancetype)initWithClient:(IMPOSClient *)client conversationType:(NSInteger)conversationType targetId:(NSString *)targetId options:(IMPOSGlobalChatViewModelOptions *)options; |
使用示例(Swift / Objective-C)
IMPChatViewModel(client: client, conversationType: .privateChat, targetId: "user2", options: nil)IMPOSGlobalChatViewModel *chatViewModel =
[[IMPOSGlobalChatViewModel alloc] initWithClient:client conversationType:1 /* 单聊 */ targetId:@"user2" options:nil];示例口径
示例由绑定签名与契约类型生成:构造函数参数、协议方法、属性名均来自契约;实际业务中把示例变量替换为宿主实例即可。
在架构中的位置
| 项 | 值 |
|---|---|
| 层 | ViewModel(状态机与业务编排) |
| 依赖 | GlobalUIKit -> IMKit -> IMLib(公开接口) |
| 相关组件 | ChatViewController、ChatDataSource、ChatCellModel |
错误处理
ViewModel 层通过事件上报错误(如 error(err));错误码与处理建议见 Global IM UIKit 错误处理、状态码(运行时 43 条)与 状态码全量对照(440 条)。