HomeiOS Developmentios - Utilizing @Atmosphere inside a ViewModifier causes a deep SwiftUI crash...

ios – Utilizing @Atmosphere inside a ViewModifier causes a deep SwiftUI crash — is my theme setup essentially incorrect?


I exploit a customized AppTheme sort and inject it by way of a customized EnvironmentKey. Views can entry the theme utilizing @Atmosphere(.appTheme) or override it explicitly.

Whereas this work high quality typically it somhow causes mysterios crashed deep inside SwiftUI inside code. The crash does not occur inside my code and Xcode doesn’t present a significant stack hint.

The crash solely occurs when utilizing the setting worth contained in the modifier (see demo code beneath). Utilizing @Atmosphere(.appTheme) immediately inside views works completely.

The theme does not change dynamically and is a plain struct. I solely need globally accessible type definitions.

So the query is: Is there something essentially incorrect with utilizing a customized theme by way of @Atmosphere inside a ViewModifier?

I can’t inform whether or not:

  • my common theme structure is flawed,
  • I’m unintentionally triggering some SwiftUI limitation concerning Atmosphere inside modifiers,
  • or it is a SwiftUI bug.

I’m not attempting to implement dynamic theme switching or something complicated — I solely need a central place for kinds that I can entry throughout the app with out manually passing the theme down in every single place.

Earlier than attempting to revamp the whole lot, I need to know whether or not this sample is meant to work, or whether or not SwiftUI has a identified limitation right here.

Any insights appreciated.

Code:

// Summary AppTheme construct on tokens
struct AppThemeTextTokens {
    var main: Coloration
    var font: Font
}

struct AppTheme {
    var textual content: AppThemeTextTokens
    //...
    
    static let `default` = AppTheme(...)
}


// Concrete, app specifix app theme
extension AppTheme {
    static let theme = AppTheme(
        textual content: .init(main: .main, font: .largeTitle)
    )
    
    //...
}


// App Theme EnvironmentKey
non-public struct AppThemeKey: EnvironmentKey {
    static var defaultValue: AppTheme = AppTheme.default
}

extension EnvironmentValues {
    var appTheme: AppTheme {
        get { self[AppThemeKey.self] }
        set { self[AppThemeKey.self] = newValue }
    }
}


// Modifier to use theme to Textual content views
struct AppThemeTextModifier: ViewModifier {
    let theme: AppTheme?
    @Atmosphere(.appTheme) non-public var envTheme   //  some View {
        let theme = theme ?? envTheme               //  some View {
        modifier(AppThemeTextModifier(theme: theme))
    }
}


// Inject theme to @Atmosphere
struct RootView: View {
    let theme = PTAppTheme.theme
    
    var physique: some View {
        SomeSubViews()
            .setting(.appTheme, theme)
    }
}

// Theme utilization by way of @Atmosphere
struct DeepDownSubview: View {
    @Atmosphere(.appTheme) non-public var theme
    
    var physique: some View {
        Textual content("Hi there World")
            .textAppTheme()
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments