# Programatic dismiss navigation animation based on State binding

**URL:** <https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275>\
**Category:** Swift Composable Architecture\
**Tags:** navigation\
**Created:** [August 11, 2020, 1:02am UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275 "2020-08-11T01:02:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thehavre](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thehavre/32/12825_2.png) [@thehavre](https://forums.swift.org/u/thehavre)\
**Post date:** [August 11, 2020, 1:02am UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/1 "2020-08-11T01:02:45Z")

</div>

Hi there! This post is not really a question, but more about curiosity and perception of a "problem". Maybe such a question would lead to the enhancement of TCA :)

**Foreword:**  
In the new fabulous world of _SwiftUI_ and _TCA_-like architectures in iOS development _State_ is everything. Your App is a State. Any _View_ in it is just a graphic representation of its state, which means that any change to a State causes re-rendering of a View. And vice versa - any changes/interactions with a view would cause state mutation. _TCA_ was created with this postulate in mind.

**The problem:**  
Imagine that we have 2 views (screens) in one `NavigationView` - second screen is shown after first one in a stack. And here is a "non-trivial" task - dismiss shown 2d view programmatically (get back to the 1st one), for e.g. native navigation bar is hidden, so no back button and we need to show custom one OR view should be dismissed by timer etc.

Looks like as easy peasy task according _State_ development paradigm:

- Create and store a state
- View reacts on it by showing a view for this state
- Remove a state (`state = nil`)
- View reacts on it by dismissing a view since the state is no longer exist

Unfortunately, it's not so obvious in _TCA_. But why? Let's move to the concrete examples using _TCA_ and take the _NavigateAndLoad_ case study as an example (I've removed Activity Indicator view and delay functionality)

Refresh [**NavigateAndLoad screen**](https://github.com/pointfreeco/swift-composable-architecture/blob/main/Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-LoadThenNavigate.swift) in memory

And let's add programmatic _Close_ feature to the [**Counter screen**](https://github.com/pointfreeco/swift-composable-architecture/blob/main/Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-Counter.swift)

```swift
enum CounterAction: Equatable {
....
  case close
}

let counterReducer = Reducer<CounterState, CounterAction, CounterEnvironment> { state, action, _ in
....
  case .close:
    return .none
  }
}

struct CounterView: View {
  let store: Store<CounterState, CounterAction>

  var body: some View {
    WithViewStore(self.store) { viewStore in
      ZStack {
      Color(.red)
      HStack {
         ....
        **Button("Close") { viewStore.send(.close) }**
      }
      }
    }
  }
}

```

Creator/Presenter of a screen should also be responsible for killing/removing Counter. Which means that `NavigateAndLoad` screen should handle `close` action. And it's 2 possible ways to do so

```swift
.....
        case .optionalCounter(.close):
          state.isNavigationActive = false
          return .none
.....

```

OR

```swift
.....
        case .optionalCounter(.close):
          return Effect(value: .setNavigation(isActive: false))
.....

```

I'd prefer the second one.

**The Issue:**  
Unfortunately, in such implementation, pop animation in `NavigationView` is with side effect: `Counter` view removes from screen (exchanged with blank `EmptyView`) and then pop animation invokes.

![giphy](https://global.discourse-cdn.com/swift/original/2X/f/f71616e7bd63f3650292e7da95009aecd2688ff7.gif)

It's happening because of `IfLetStore` in `NavigationLink`s destination:

```swift
NavigationLink(
            destination: IfLetStore(
              self.store.scope(
                state: { $0.optionalCounter }, action: NavigateAndLoadAction.optionalCounter),
              then: CounterView.init(store:)
            ),
            isActive: viewStore.binding(
              get: { $0.isNavigationActive },
              send: NavigateAndLoadAction.setNavigation(isActive:)
            )
          ) {
            HStack {
              Text("Load optional counter")
            }
          }

```

When `CounterState` becomes nil, `IfLetStores` reacts on it and removes view from screen.

**The Question**  
Is it OK? 😃 I mean, it can be fixed by using `presentationMode.wrappedValue.dismiss()` in Close button instead of sending `close` action directly to the store, but is it how it should be done? According `SwiftUI` state paradigm - removing of a state should properly remove a view. But in _TCA_ we need to remove (change) a view programatically and then state can react on it. In other words, we need to simulate users touch on native back button.

What do you think, guys? Should this be fixed or it's completely normal? Let's discuss :)

---

<div class="post-metadata">

**Author:** ![thehavre](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thehavre/32/12825_2.png) [@thehavre](https://forums.swift.org/u/thehavre)\
**Post date:** [August 11, 2020, 10:40am UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/2 "2020-08-11T10:40:47Z")

</div>

By the way, the same issue applies to the transition animations related to the `IfLetStore`.  
When I need to move from Login to the Main flow by swapping views with animation transition, I can't do it, because changing of views is based on state changing:

```swift
case .authorised:
  state.login = nil
  state.main = MainState()

case .logout:
  state. main = nil
  state.login = LoginState()

```

Even if `Transition` is specified for both views and swap actions wrapped in the `withAnimation` closure, disappear animation for `nil`ed state can't be performed.

---

<div class="post-metadata">

**Author:** ![toph42](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/toph42/32/7807_2.png) [@toph42](https://forums.swift.org/u/toph42)\
**Post date:** [August 11, 2020, 8:18pm UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/3 "2020-08-11T20:18:16Z")

</div>

I certainly can’t offer any insight, but I would like to ask for clarification: What is TCA? I found other questions here referencing it, but none with enough context to clue me into what it is. It seems to have something to do with reactive design like SwiftUI.

Edit: I’m guessing the “CA” is “Composable Architecture” based on the “Swift Composable Architecture” tag on this and other related topics.

---

<div class="post-metadata">

**Author:** ![thehavre](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thehavre/32/12825_2.png) [@thehavre](https://forums.swift.org/u/thehavre)\
**Post date:** [August 11, 2020, 8:23pm UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/4 "2020-08-11T20:23:51Z")

</div>

Yeah, u r totally right :)

> **[GitHub - pointfreeco/swift-composable-architecture: A library for building...](https://github.com/pointfreeco/swift-composable-architecture)**
>
> A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind. - GitHub - pointfreeco/swift-composable-architecture: A library for bu...

---

<div class="post-metadata">

**Author:** ![atfelix](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/atfelix/32/9310_2.png) [@atfelix](https://forums.swift.org/u/atfelix)\
**Post date:** [August 11, 2020, 8:29pm UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/5 "2020-08-11T20:29:03Z")

</div>

TCA = The Composable Architecture

From the [FAQ](https://github.com/pointfreeco/swift-composable-architecture/blob/830854166fd3969771d1a0f389e479311af61002/README.md#faq):

 ![Screen Shot 2020-08-11 at 4.27.50 PM](https://global.discourse-cdn.com/swift/original/2X/1/1a5a6f21b0ee14e9d3133a017ec840fe6c5616f6.png)

---

<div class="post-metadata">

**Author:** ![Namedix](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Namedix](https://forums.swift.org/u/Namedix)\
**Post date:** [September 21, 2020, 8:47am UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/6 "2020-09-21T08:47:07Z")

</div>

@thehavre, @stephencelis do you have any solution for this problem?

---

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [September 21, 2020, 11:39am UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/7 "2020-09-21T11:39:12Z")

</div>

Here's another way to dismiss a navigation/model from within the child. You can hold a boolean in the child's state to determine whether or not it is presented:

```swift
struct ChildState {
  var isPresented = true 
  ...
}

```

And you can have an action that causes it to flip to `false`:

```swift
case .closeButtonTapped:
  state.isPresented = false
  return .none

```

And then finally you can use the `PresentationMode` environment variable to dismiss when the `isPresented` boolean flips:

```swift
struct ChildView {
  @Environment(\.presentationMode) var presentationMode
  let store: Store<ChildState, ChildAction>

  var body: some View {
    WithViewStore(self.store) { viewStore in
      VStack {
        ...
      }
      .onChange(viewStore.isPresented) { isPresented in
        if !isPresented {
          self.presentationMode.dismiss()
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Namedix](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Namedix](https://forums.swift.org/u/Namedix)\
**Post date:** [September 21, 2020, 12:01pm UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/8 "2020-09-21T12:01:00Z")

</div>

> [@mbrandonw](#):
>
> `e.dismiss()`

And what would popToRoot look like, we are currently trying to deal with this problem and we still have a situation that state is already nil and gets actions from the navigation link. Of course we use the IfLetStore. Child reducer is before main reducer.

---

<div class="post-metadata">

**Author:** ![thehavre](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thehavre/32/12825_2.png) [@thehavre](https://forums.swift.org/u/thehavre)\
**Post date:** [September 27, 2020, 5:05pm UTC](https://forums.swift.org/t/programatic-dismiss-navigation-animation-based-on-state-binding/39275/9 "2020-09-27T17:05:14Z")

</div>

Yeah, I see. So the problem of changing state instead of asking view to be dismissed immediately is formally fixed. But the overall general problem is still the same: we need to ask SwiftUI manually to dismiss view to send signal to the parent view/state that it was dismissed.

Seems that this is mixed issue of SwiftUI development paradigm and TCA ☹
