Add an example of partial state applying

For example, I have a main view with a lot of subviews. Each subview has ViewState and Action.

enum Actions {
	enum Privacy {
		case allowToGatherCrashDiagnostics(Bool)
	}
	enum Support {
		case callSupport
	}
  case privacy(Privacy), support(Support)
}

enum States {
	struct Privacy {
		let termsOfUse: String
		let canGatherCrashDiagnostics: Bool
	}

	struct Support {
		let supportURL: String
	}
}

struct MainState {
	let privacy: States.Privacy
	let support: States.Support
}

protocol MainViewModelProtocol {
	func apply(_ state: MainState)
	var actionPublisher: AnyPublisher<Actions, Never>
}

MainViewModel exposes only two channels: input and output, right?

But how can I apply a partial state?

In examples of TCA you have

  1. ready-to-use state.
  2. reducer that applies partial states.

Yes, it is easy, but what if I want to change only one thing in state? What if external system ( push notification or any other interaction that system initiates ) wants to change a state of a view. ( I would like to change a support URL, for example. It comes from a nearby bluetooth device. )

Very vague and not very clear, but I'll try to answer:

  1. TCA doesn't have an opinion on how you structure (keep) your states, actions, environments and reducers. You can have 4 top level identifiers (state, action, env, reducer) or you can have them pseudo-namespaced under a single enum SomeFeature

  2. You're probably looking for a store scoping. This way you can tell a certain view to observe only one part of a parent view's state. Should you happen to be subscribed to PFC's video series, you can see that in more action in episode #151.

Terms of Service

Privacy Policy

Cookie Policy