-
Hello, first of all, thank you for creating a nice framework. I am also using TCA in UIKit, but unlike SwiftUI, it doesn't usually have a binding concept, so I want to show an alert or toast with the same value when emit. What I want to ask is whether the TCA has the relate function or can do like the Pulse of the ReactorKit. The result I expect is this structure. enum Test {
struct State: Equatable {
@Pulse var alertMessage: String?
}
enum Action: Equatable {
case showAlert(message: String?)
}
struct Environment {}
static var reducer: Reducer<State, Action, Environment> {
.init { state, action, _ in
switch self {
case let .showAlert(message):
state.alertMessage = message
return .none
}
}
}
}
final class TestViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
viewStore.pulse(\.&alertMessage)
.sink { [weak self] message in
self?.showAlert(message: message)
}
.store(in: &cancellables)
}
...
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I just implemented this and it seems to work fine. import Combine
import ComposableArchitecture
import Foundation
@propertyWrapper
struct TCAPulse<Value: Equatable>: Equatable {
var value: Value {
didSet {
riseValueUpdatedCount()
}
}
var valueUpdatedCount = UInt.min
init(wrappedValue: Value) {
self.value = wrappedValue
}
var wrappedValue: Value {
get { return value }
set { value = newValue }
}
var projectedValue: TCAPulse<Value> {
return self
}
private mutating func riseValueUpdatedCount() {
valueUpdatedCount &+= 1
}
}
extension ViewStore {
func pulse<Value>(_ keyPath: KeyPath<State, TCAPulse<Value>>) -> AnyPublisher<Value, Never> {
publisher.map(keyPath).removeDuplicates {
$0.valueUpdatedCount == $1.valueUpdatedCount
}.map(\.value).eraseToAnyPublisher()
}
} |
Beta Was this translation helpful? Give feedback.
-
I'm glad you are able to recreate this concept outside the library because I'm not sure it should ship with library. In our opinion, forcing the state to reset to The In the example code you posted, the code inside |
Beta Was this translation helpful? Give feedback.
I'm glad you are able to recreate this concept outside the library because I'm not sure it should ship with library. In our opinion, forcing the state to reset to
nil
on dismissal is a good thing, and will make your domain modeling stronger if you can maintain that consistency.The
alertMessage
state should be optional so thatnil
represents no alert and non-nil
represents an alert showing. This allows you to have logic in your reducer that is dependent on whether or not the alert is showing. Otherwise you have no way of knowing.In the examp…