Skip to content
Snippets Groups Projects

[Messenger example] create new group

3 files
+ 86
3
Compare changes
  • Side-by-side
  • Inline

Files

import AppCore
import AppCore
import ComposableArchitecture
import ComposableArchitecture
import Foundation
import Foundation
 
import XXMessengerClient
import XXModels
import XXModels
public struct NewGroupComponent: ReducerProtocol {
public struct NewGroupComponent: ReducerProtocol {
public struct State: Equatable {
public struct State: Equatable {
public init(
public init(
contacts: IdentifiedArrayOf<XXModels.Contact> = []
contacts: IdentifiedArrayOf<XXModels.Contact> = [],
 
members: IdentifiedArrayOf<XXModels.Contact> = []
) {
) {
self.contacts = contacts
self.contacts = contacts
 
self.members = members
}
}
public var contacts: IdentifiedArrayOf<XXModels.Contact>
public var contacts: IdentifiedArrayOf<XXModels.Contact>
 
public var members: IdentifiedArrayOf<XXModels.Contact>
}
}
public enum Action: Equatable {
public enum Action: Equatable {
case start
case start
case didFetchContacts([XXModels.Contact])
case didFetchContacts([XXModels.Contact])
 
case didSelectContact(XXModels.Contact)
case didFinish
case didFinish
}
}
public init() {}
public init() {}
 
@Dependency(\.app.messenger) var messenger: Messenger
@Dependency(\.app.dbManager.getDB) var db: DBManagerGetDB
@Dependency(\.app.dbManager.getDB) var db: DBManagerGetDB
@Dependency(\.app.mainQueue) var mainQueue: AnySchedulerOf<DispatchQueue>
@Dependency(\.app.mainQueue) var mainQueue: AnySchedulerOf<DispatchQueue>
@Dependency(\.app.bgQueue) var bgQueue: AnySchedulerOf<DispatchQueue>
@Dependency(\.app.bgQueue) var bgQueue: AnySchedulerOf<DispatchQueue>
@@ -29,10 +35,12 @@ public struct NewGroupComponent: ReducerProtocol {
@@ -29,10 +35,12 @@ public struct NewGroupComponent: ReducerProtocol {
public func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
public func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
switch action {
case .start:
case .start:
 
let myId = try? messenger.e2e.tryGet().getContact().getId()
return Effect
return Effect
.catching { try db() }
.catching { try db() }
.flatMap { $0.fetchContactsPublisher(.init()) }
.flatMap { $0.fetchContactsPublisher(.init()) }
.assertNoFailure()
.assertNoFailure()
 
.map { $0.filter { $0.id != myId } }
.map(Action.didFetchContacts)
.map(Action.didFetchContacts)
.subscribe(on: bgQueue)
.subscribe(on: bgQueue)
.receive(on: mainQueue)
.receive(on: mainQueue)
@@ -42,6 +50,14 @@ public struct NewGroupComponent: ReducerProtocol {
@@ -42,6 +50,14 @@ public struct NewGroupComponent: ReducerProtocol {
state.contacts = IdentifiedArray(uniqueElements: contacts)
state.contacts = IdentifiedArray(uniqueElements: contacts)
return .none
return .none
 
case .didSelectContact(let contact):
 
if state.members.contains(contact) {
 
state.members.remove(contact)
 
} else {
 
state.members.append(contact)
 
}
 
return .none
 
case .didFinish:
case .didFinish:
return .none
return .none
}
}
Loading