Hi @victor I think that won't work in my case. I'll extend my question with more code.
First I have this type as my CustomAlertView's state
public struct CustomAlertState: Equatable {
let title: String
let subtitle: String
let actions: [CustomAlertAction]
public init(
title: String,
subtitle: String,
actions: [CustomAlertAction]
) {
self.title = title
self.subtitle = subtitle
self.actions = actions
}
Notice the CustomAlertAction
type, who has all info needed by the CustomAlertView's buttons
public struct CustomAlertAction {
let title: String
var action: () -> Void
public init(
title: String,
action: @escaping () -> Void
) {
self.title = title
self.action = action
}
}
And in the CustomAlertView I have several CustomAlertButton
like this
struct CustomAlertButton: View {
let action: CustomAlertAction
var body: some View {
Button {
action.action() // Naming could be better
} label: {
// This is the UI look
}
}
}
In my CustomAlertView these buttons are created using a simple loop calling CustomAlertButton(action: action)
Notice that all these views and states are disconnected from TCA at all, and there is no reference to any reducer, store or binding. I'm doing this way because CustomAlertView should work within a pure SwiftUI context too.
Now, when trying to create my array of CustomAlertAction
in my reducer, that's when all my problems appear. Here's a more accurate version of my code:
struct State: Equatable {
var customAlertState: CustomAlertState?
}
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
swith action {
// Some other actions here
case .showError:
return .task {
let action = CustomAlertAction(title: "Action 1", action: {
// ACTION BLOCK
// How to call .doSomething action from here?
// Can I access the store from here?
})
let state = CustomAlertState(title: String, actions: [action])
return .showCustomAlert(state)
}
case .showCustomAlert(let customAlertState):
// This triggers the alert display on the view
state.customAlertState = customAlertState
return .none
}
case .doSomething:
// This action should be called from the CustomAlert
return .none
}
}
As you can see, since the action block is a () -> Void
block, can't do any return inside the ACTION BLOCK
. Obviously, I could create a TCA aware CustomAlertAction, something like this
public struct CustomAlertAction<Action> {
let title: String
var action: () -> Action
public init(
title: String,
action: @escaping () -> Action
) {
self.title = title
self.action = action
}
}
But then I'll have to create TCA aware versions for CustomAlertState and CustomAlertView, duplicating all my UI code in order to keep compatibility with pure SwiftUI.
Not sure if what I want is achievable or if I need to go full TCA with my CustomAlertView.
I think all my problems come because I'm setting the CustomAlertState
within the Reducer, I'm I correct?
I hope this give you all the context needed.
Thanks.