Open butaminas opened 4 years ago
Same for me. Tried both a Angular Template and a few minutes ago a Core template. Both same error message.
This issue is still hunting me even after updating to 2.3.3 (which seems to fix it for some people) and the dark theme is really making my app unusable. Any progress on this?
So after looking a litthe further if found out that there is actually a new version of the Theme packages that support {N}7. I used the RC version ("@nativescript/theme": "^2.5.0"
).
An up-to-date listing of NativeScript plugins with 7.0 compatibility that we know of @nativescript/theme
After upgrading to this version the issue is gone and i can now use the light theme. Currently i can only implement a toggle button that works. Can not get the app to start in Light mode. Somehow the app loads in Dark Mode as this is currently my settings on the phone.
If i can get the app to also START in light mode i'm completely happy :)
@martijnvanschie updating to RC also does the trick for me.
You can try adding this to your info.plist:
...
<key>UIUserInterfaceStyle</key>
<string>Light</string>
...
This would force the light mode by default as mentioned here: https://docs.nativescript.org/ui/dark-mode
After working on it a bit more, I've noticed that Theme.getMode()
is not working properly. Even if I start in dark mode, I always get ns-light
.
The systemAppearanceChanged
event works fine on the other hand. But I need to know the appearance when the app is launched.
This is most likely an old issue that is mentioned here: #232 where the solution was to use Application.systemAppearance()
as an alternative, however, this also doesn't work as I end up with (CoreFoundation) *** Terminating app due to uncaught exception 'NativeScript encountered a fatal error: Uncaught TypeError: Cannot read property 'substr' of undefined
error.
Here is my workaround for the above problem. Note, this is not a solution and this still needs to be fixed but since I was struggling with this a lot it might be useful for someone else also:
import { Application, isIOS } from "@nativescript/core"
import store from "../index"
Application.on(Application.displayedEvent, (args) => {
if (isIOS) {
store.commit('generalStore/setDarkMode', Application.ios.systemAppearance)
} else {
store.commit('generalStore/setDarkMode', Application.android.systemAppearance)
}
});
P.S. I'm using nativescript-vue and Vuex but the idea is clear - you go for Application.ios.systemAppearance
or Application.android.systemAppearance
.
IMPORTANT: you need to run it in displayedEvent
or do a manual timeout, otherwise it will not work.
Thanx for the updates @butaminas.
I've been playing around some more with this code and found still strange behaviour. When changing the theme on my phone the {N} Theme stays the same. This is is my debugging code
Application.on(Application.systemAppearanceChangedEvent, (args: SystemAppearanceChangedEventData) => {
console.log('systemAppearanceChangedEvent', args.newValue);
console.log('Theme.getMode', Theme.getMode());
});
The output is al follows (When switching from Dark to Light and back)
JS: systemAppearanceChangedEvent light
JS: Theme.getMode ns-light
JS: systemAppearanceChangedEvent dark
JS: Theme.getMode ns-light
Also, setting mode to light in my main.ts requires a timeout to be set.
Below code does not set the {N} Theme
Theme.setMode(Theme.Light);
It required a (very short) timeout to make it work
setTimeout(() => {
console.log('Setting mode to light');
Theme.setMode(Theme.Light);
},100);
So i kinda have it working now, but I keep on investigating :)
PS: I'm using {N}7 Angular
@martijnvanschie it's definitely a bug that seems to be around for quite a while. From what I came to realize is that at this point, you can't use Theme.getMode()
at all as it is not functioning properly.
Currently, the only solution that is working is Application.ios.systemAppearance
and Application.android.systemAppearance
as I mentioned in the previous post. This returns the correct value at launch (at least with all the tests I was able to make) and systemAppearanceChangedEvent
also seems to be returning the correct value.
And yes, it also doesn't work for me without a delay. Setting everything to run at displayedEvent
seems to be enough of a delay for everything to work properly, so I guess it makes sense that Theme.setMode
would also require a delay to work.
If you find anything else worth mentioning - let me and the community know as I think this is uncharted waters for most of us. Hopefully these "hacks" will stay stable and do the trick until the core is fixed.
I'll take a look at that displayEvent again and test it out.
I do have another issue which is must more annoying than the hacks. When i go from light back to dark mode on my phone (Galaxy S20+) the app stays in dark mode. Even my test button to setMode()
of toggleMode()
wont work anymore, dark mode it is from that point on untill i restart the app again. Not a use case that is often performed i quess but one that should work as we developers do test the less obvious use cases :)
Think i'll raise a seperate issue for this but the issues keep stacking and it does not seem to be picked up. I understand it's busy and probably due to the {N}7 upgrade as well.
That's strange. I've tried switching dark / light mode on Xiaomi Redmi Note 8 and the appearance in the app is changing accordingly.
Will try to test this out on raw Android to see if I get different results.
Environment Provide version numbers for the following components (information can be retrieved by running
tns info
in your project folder or by inspecting thepackage.json
of the project):mounted () { console.log(Theme.getMode()) },