Use of hmlongco/Factory Swift library to inject envionment variables
#984
pylapp
started this conversation in
Return of experience and feedbacks
Replies: 1 comment
-
|
Solution based on
/// Inject the preferred theme in the `Factory` that can be access later in `Uikit` elements (i.e. ViewController, UIView, ..)
/// and in the `SwiftUI` views via environment.
///
/// This must be created once in Uikit project using the preferred theme (here sosh theme is used)
extension Container {
var oudsTheme: Factory<OUDSTheme> {
Factory(self) { SoshTheme() }
}
}1.2. Retrieve theme in /// SwiftUI Modifier created in UiKit project based on `OUDS` and using `Factory` to inject the
/// right theme into `SwiftUi` environment.
struct OUDSThemeableModifier: ViewModifier {
// Inject here the oudsTheme added in factory before
@Injected(\.oudsTheme) private var oudsTheme
// Use OUDSThemeableView to set oudsTheme in the SwiftUI environment
func body(content: Content) -> some View {
OUDSThemeableView(theme: oudsTheme) { content }
}
}
extension View {
/// Helper must be called on the root swiftui view initialized from UiKit
func oudsThemeable() -> some View {
self.modifier(OUDSThemeableModifier())
}
}
/// In this example, the view controller contains a `UILabel` with content need to have
/// the content color based on ouds token define in `oudsTheme`.
///
/// The oudsTheme is retrieve from `Factory` injected before. So all tokens can be access here.
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
let colorScheme: ColorScheme = UITraitCollection.current.userInterfaceStyle == .dark ? .dark : .light
// Inject here the oudsTheme added in factory before
@Injected(\.oudsTheme) private var theme
override func viewDidLoad() {
super.viewDidLoad()
/// Get the color content value form ouds theme according to the `colorScheme`
label.textColor = theme.colors.contentDefault.color(for: colorScheme).uiColor
}
}2.2. Call a SwiftUI View using /// In this example, the view conrolleur contains a button. When it is clicked a `SwiftUI`view
/// is presented. To ensure the theme is set in the `SwiftUI` environment, use the
/// `OUDSThemeableModifier` modifier that will retrieve the theme "injected" into the `Factory`.
///
/// To simplify the code, the `View.oudsThemeable` helper is preferred.
class ViewController: UIViewController {
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func onClicked(_ sender: Any) {
let view = SubOptionsView().oudsThemeable()
let vc = UIHostingController(rootView: view)
self.navigationController?.pushViewController(vc, animated: true)
}
}
struct SubOptionsView: View {
@State var mail: String = ""
@State var address: String = ""
@State var phoneNumber: String = ""
// Get theme form environment
@Environment(\.theme) var theme: OUDSTheme
var body: some View {
VStack(spacing: theme.spaces.rowGapMedium) {
OUDSTextInput(label: "Email", text: $mail)
OUDSTextInput(label: "Email", text: $address)
OUDSTextInput(label: "Email", text: $phoneNumber)
OUDSButton(text: "Validate") { }
}
}
} |
Beta Was this translation helpful? Give feedback.
0 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.
-
Context
Some projects use the
hmlongco/FactorySwift library to manage their dependency injections.In strong legacy codebases, some users want to manage some objects like
OUDSThemethrough it instead of using theOUDSThemeableViewThis was anticipable because this profile of users was spotted but until now never considered.
Solution
Resources
Contact
Beta Was this translation helpful? Give feedback.
All reactions