public struct ConversationsState: Equatable {
..........
public var conversations: IdentifiedArrayOf<ConversationResponse.Item> = []
}
public struct TabsState: Equatable {
........
public var conversations: ConversationsState
}
public let tabsReducer = Reducer<TabsState, TabsAction, TabsEnvironment> { state, action, environment in
..............................
case let .receivedSocketMessage(.success(.string(str))):
guard let data = str.data(using: .utf8) else {
return .none
}
let chatOutGoingEvent = ChatOutGoingEvent.decode(data: data)
switch chatOutGoingEvent {
case .conversation(let message):
// before update working update child my view
guard let index = state.conversations.conversations
.firstIndex(where: { $0.id == message.conversationId }) else {
return .none
}
state.conversations.conversations[index].lastMessage = message
// old code crash becz
// "use the id-based subscript, instead, for in-place modification"
// after which not working not update my child view
state.conversations.conversations[id: message.conversationId]?
.lastMessage = message
return .none
case .message(let message):
state.conversations.chatState?.messages.insert(message, at: 0)
return .none
}
}