How to scope single property of parent state to child state? #1804
Unanswered
acosmicflamingo
asked this question in
Q&A
Replies: 1 comment 4 replies
-
You can do it in this way, although there will be some boilerplate code for the application. public struct State: Equatable {
public var userSettings: UserSettings
private var _fontManager: FontManager.State
public var fontManager: FontManager.State {
get {
var state = _fontManager
state.appFont = userSettings.appFont
return state
}
set {
_fontManager = newValue
userSettings.appFont = newValue.appFont
}
}
public init(
userSettings: UserSettings = .init(),
fontManager: FontManager.State = .init()
) {
self.userSettings = userSettings
self._fontManager = fontManager
}
} |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now I have a parent Settings.State with the property
userSettings
, and I want to scope this single value to FontManager.State, but I can't figure out how to do this (I essentially want to make fontManager updateuserSettings.appFont
so I don't really needuserSettings
actually).Although I could use getter/setter logic so I don't pass Settings.State a FontManager.State value, this doesn't seem right because I should be able to pass Settings.State.init the FontManager.State if I wanted to. However, doing so means that there will now be two copies of
userSettings
, and that is not right either (which is right now the source of my problems).The closest example I can find to what I'm trying to do comes from isowords: https://github.com/pointfreeco/isowords/blob/main/Sources/GameFeature/GameFeature.swift#L32 and https://github.com/pointfreeco/isowords/blob/main/Sources/GameCore/GameCore.swift#L534. However, I don't think I've seen any official documentation on this approach, so it could be a workaround to a problem that currently doesn't have a clean solution.
Beta Was this translation helpful? Give feedback.
All reactions