How to show .alerts from two actions one after the other?

Hello,

I have a reducer with some actions and in one of them I would like the user to confirm something by showing an alert (using TCAs AlertState), and after confirmation I return an effect with another action, where some work is done and finally another alert should be shown with some message. This seems to be not working in TCA 0.6.0, Xcode 12 beta 4 and iOS 14 beta 4.

Am I missing something (i.e. is such a sequence not recommended or whatever)?

If someone would like to reproduce the problem, I have modified the AlertAndSheets case study slightly:

import ComposableArchitecture
import SwiftUI

struct AlertAndSheetState: Equatable {
var alert: AlertState?
var count = 0
}

enum AlertAndSheetAction: Equatable {
case alertButtonTapped
case alertCancelTapped
case showAlert
case incrementButtonTapped
}

struct AlertAndSheetEnvironment {}

let alertAndSheetReducer = Reducer<
AlertAndSheetState, AlertAndSheetAction, AlertAndSheetEnvironment

{ state, action, _ in

switch action {

case .showAlert:
state.alert = .init(
title: "Alert!",
message: "This is an alert",
primaryButton: .cancel(),
secondaryButton: .default("Increment", send: .incrementButtonTapped)
)
return .none

case .alertButtonTapped:
state.alert = .init(
title: "Confirmation",
message: "Do you really want to show an alert?",
primaryButton: .default("Ok", send: .showAlert),
secondaryButton: .cancel()
)
return .none

case .alertCancelTapped:
state.alert = nil
return .none

case .incrementButtonTapped:
state.alert = nil
state.count += 1
return .none
}
}

struct ContentView: View {
let store: Store<AlertAndSheetState, AlertAndSheetAction>

var body: some View {
WithViewStore(self.store) { viewStore in
Form {
Text("Count: (viewStore.count)")

      Button("Alert") { viewStore.send(.alertButtonTapped) }
        .alert(
          self.store.scope(state: { $0.alert }),
          dismiss: .alertCancelTapped
        )

  }
}
.navigationBarTitle("Alerts & Action Sheets")

}
}

Looks like you've stumbled upon a bug with how we handle observing alert state. Good find! We've opened up a PR that should address the issue here: https://github.com/pointfreeco/swift-composable-architecture/pull/249

Terms of Service

Privacy Policy

Cookie Policy