Using view specific state and action with FormAction

Hi all,

I am adding some of the awesome new form code to my project, however, I am hitting a snag. For each view I am keeping view specific state and actions separate from my core logic. The issue then comes when I try to add form logic to my view specific action and state.

struct FeatureState: Equatable {
    ....
}

enum FeatureAction: Equatable {
    case form(FormAction< FeatureState>)
    ....
}

struct FeatureView: View {
    struct ViewState: Equatable {
         ...
    }
    enum ViewAction: Equatable {
        case form(FormAction<ViewState>) // ??
    }
}

extension FeatureState {
    var view: FeatureView.ViewState {
        .init(...)
    }
}

extension FeatureAction {
    static func view(_ action: FeatureView.ViewAction) -> Self {
        switch action {
        ...
        case .form(let action):
            fatalError("What do I do here??"
        }
    }
}

How can I translate the FormAction to FormAction? The natural answer would seem to be using pullback:

extension FeatureAction {
    static func view(_ action: FeatureView.ViewAction) -> Self {
        switch action {
        ...
        case .form(let action):
            return .form(action.pullback(\.view))
        }
    }
}

However, this requires me to add a setter to my computed view function.

extension FeatureState {
    var view: FeatureView.ViewState {
        get { .init(...) }
        set { .... }
    }
}

This seems ok at first, but I quickly realized while I can easily reduce from FeatureState -> ViewState, I can't always go from ViewState -> FeatureState since FeatureState is naturally a superset of the data in ViewState.

For now I am just setting those properties that my form cares about, but this seems like an easy way for bugs to creep in as there is no compiler logic ensuring that I have implemented the setter fully.

What do y'all think?

Terms of Service

Privacy Policy

Cookie Policy