react-native-localize
react-native-localize copied to clipboard
Listeners not firing
Bug
I'm trying to check whether the phone is currently using auto time configurations. If the user changes those configurations, the app needs to be notified. However, my listeners are not firing.
RNLocalize functions are working properly, I can get results out of those. Just the listeners not firing. My app is for Android only.
Environment info
React native info output: System: OS: Windows 10 10.0.18362 CPU: (2) ia32 Intel(R) Pentium(R) CPU G3258 @ 3.20GHz Memory: 1.84 GB / 7.86 GB Binaries: Node: 10.18.0 - C:\Program Files (x86)\nodejs\node.EXE npm: 6.13.4 - C:\Program Files (x86)\nodejs\npm.CMD npmPackages: react: 16.8.6 => 16.8.6 react-native: 0.60.0 => 0.60.0
Library version: "^1.3.3"
Reproducible sample code
This is how I'm running it:
componentDidMount() {
//Checks if time and time zone are auto
RNLocalize.addEventListener("change", this.setTamperedClock);
}
setTamperedClock = () => {
if (!RNLocalize.usesAutoDateAndTime() || !RNLocalize.usesAutoTimeZone()) {
this.setState({ tamperedClock: true });
} else {
this.setState({ tamperedClock: false });
}
};
componentWillUnmount() {
RNLocalize.removeEventListener("change", this.setTamperedClock);
}
@SirKadogan Please respect the issue template.
@zoontek sorry. I updated the issue
@SirKadogan Does this happen on iOS or Android? On which OS version? With which device?
This app is for Android only. The device is a Xiaomi Redmi 5 with android 8.1
android:configChanges=”keyboard|keyboardHidden|orientation|screenSize”
Replace it by android:configChanges=”keyboard|keyboardHidden|orientation|screenSize|layoutDirection|locale”
Thx, @Blind12! I'm gonna try it as soon as I can next week.
but, if on ios eventListener does not work after changing the system language, what could be the problem?
i have some problem in Android not firing event, v1.3.3 react-native-localize, in debug mode work, in release mode not work, Android 10
Now i added android:configChanges=”keyboard|keyboardHidden|orientation|screenSize|layoutDirection|locale” this and it works better but it still fails sometimes
I finally got around to testing this. Changing the Manifest does help, like @tastafur said.
However, I still get inconsistent results. It triggers sometimes, but not always. Also, it seems to trigger only once.
I tested again on v1.4.3, same results.
For those who are struggling to get up to date information about time settings, I build a small sample app with a native module for that.
Here's the sample repo: https://github.com/SirKadogan/native_modules
I didn't have time to build listeners natively, but this repo shows the part where RNLocalize is NOT changing the value after the app opens.
@SirKadogan This is related to https://medium.com/@iiro.krankka/its-time-to-kiss-goodbye-to-your-implicit-broadcastreceivers-eefafd9f4f8a
I start working on the next version with explicit BroadcastReceivers
and zero listeners optimization. This is not really a big deal and should be available during the week.
It will probably looks like this:
<receiver android:name="com.zoontek.rnlocalize.IntentActionReceiver">
<intent-filter>
<!-- Add only used listeners -->
<action android:name="android.intent.action.ACTION_LOCALE_CHANGED" />
<action android:name="android.intent.action.ACTION_TIME_CHANGED" />
<action android:name="android.intent.action.ACTION_TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
A small breaking change will be needed: addEventListener("change", listener)
will be replaced with addListener("localeDidChange" | "timeZoneDidChange" | "clockDidChange")
.
Is everyone OK with that?
EDIT: Your repository code relies on async functions, when RNLocalize
does not. It might be complex to switch to async by default (think about async translation init), I'd rather stick to the listeners until JSI is stable enough (see https://blog.g2i.co/guest-blogger-parashuram-n-react-natives-new-architecture-glossary-of-terms-a90cdb45a76e)
I coud also add a simple helper if you don't use listeners at all. Something like:
// function refreshOn(...args: ("clockDidChange" | "localeDidChange" | "timeZoneDidChange")[]): void
RNLocalize.refreshOn("clockDidChange", "localeDidChange", "timeZoneDidChange");
This will add a default, unused listener, to disable the zero listener optimization.
I coud also add a simple helper if you don't use listeners at all. Something like:
// function refreshOn(...args: ("clockDidChange" | "localeDidChange" | "timeZoneDidChange")[]): void RNLocalize.refreshOn("clockDidChange", "localeDidChange", "timeZoneDidChange");
This will add a default, unused listener, to disable the zero listener optimization.
have you added this? i cant find it
@mashad6 No. android:configChanges
accepts locale
, it's imho the way to go to implement this after all.
@zoontek so there is no way i can get event if i turn on and off "automatic date & time " flag?