PopupView icon indicating copy to clipboard operation
PopupView copied to clipboard

[FREQ] Problems with using `@Environment(\.colorScheme) var colorScheme`

Open Kejiasir opened this issue 1 year ago • 5 comments

When using @Environment(\.colorScheme) var colorScheme, Xcode prompts: Accessing Environment<ColorScheme>'s value outside of being installed on a View. This will always read the default value and will not update.

Sample code:

struct SamplePopupView: BottomPopup {
    
    @Environment(\.colorScheme) var colorScheme
    
    func configurePopup(popup: BottomPopupConfig) -> BottomPopupConfig {
        popup.backgroundColour(colorScheme == .light ? .black : .white)
    }
    
    func createContent() -> some View {
        // code
    }
    
}

As Xcode suggests, it is not possible to change colors based on a colorScheme.

Is there a way to fix this?

Kejiasir avatar Oct 18 '24 12:10 Kejiasir

Hey @Kejiasir,

Thanks for sharing this issue. I don't use @Environment values, so I wasn't aware of this problem. I'll try to solve it over the weekend and get back to you with a solution (hopefully).

PS. If you have some spare time, could you confirm that the problem exists in the latest framework version (3.0.0)? Thanks for your help!

FulcrumOne avatar Oct 18 '24 13:10 FulcrumOne

Hi @FulcrumOne, thank you for your reply, and thank you for open-sourcing this excellent framework. I am currently using version 2.7.1, so I haven't had time to confirm whether this problem exists in version 3.0.0.

Kejiasir avatar Oct 18 '24 14:10 Kejiasir

Hey @Kejiasir

I found a way to fix it. I'll implement it this week, till Friday probably.

FulcrumOne avatar Oct 21 '24 07:10 FulcrumOne

Wow @FulcrumOne Great, thank you very much.

Kejiasir avatar Oct 21 '24 11:10 Kejiasir

Hey @Kejiasir,

Quick update - this is a bit more complicated than I initially thought. The problem is that I've implemented it in such a way that the configurePopup() method runs outside of the view context. This causes the problem that when the view refreshes, the function remains intact, which I did as part of the optimisation.

I'm currently considering moving the method to a body declaration, but I need to spend some time thinking about how this will affect the code efficiency.

Another idea is to put Config as a @State value, which will allow it to be updated whenever some value changes (but again, I need to investigate how it will affect the code efficiency).

I'll try my best to finish the task this week.

Have a nice day, Tomasz

FulcrumOne avatar Oct 23 '24 09:10 FulcrumOne