react-native-localize icon indicating copy to clipboard operation
react-native-localize copied to clipboard

Listeners not firing

Open SirKadogan opened this issue 4 years ago • 16 comments

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 avatar Mar 05 '20 17:03 SirKadogan

@SirKadogan Please respect the issue template.

zoontek avatar Mar 05 '20 17:03 zoontek

@zoontek sorry. I updated the issue

SirKadogan avatar Mar 05 '20 18:03 SirKadogan

@SirKadogan Does this happen on iOS or Android? On which OS version? With which device?

zoontek avatar Mar 05 '20 18:03 zoontek

This app is for Android only. The device is a Xiaomi Redmi 5 with android 8.1

SirKadogan avatar Mar 05 '20 19:03 SirKadogan

android:configChanges=”keyboard|keyboardHidden|orientation|screenSize” Replace it by android:configChanges=”keyboard|keyboardHidden|orientation|screenSize|layoutDirection|locale”

Blind12 avatar Apr 16 '20 12:04 Blind12

Thx, @Blind12! I'm gonna try it as soon as I can next week.

SirKadogan avatar Apr 23 '20 17:04 SirKadogan

but, if on ios eventListener does not work after changing the system language, what could be the problem?

marshallcool avatar May 12 '20 15:05 marshallcool

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

tastafur avatar Jun 25 '20 08:06 tastafur

Now i added android:configChanges=”keyboard|keyboardHidden|orientation|screenSize|layoutDirection|locale” this and it works better but it still fails sometimes

tastafur avatar Jun 25 '20 09:06 tastafur

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.

SirKadogan avatar Nov 06 '20 18:11 SirKadogan

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 avatar Nov 09 '20 23:11 SirKadogan

@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)

zoontek avatar Nov 10 '20 09:11 zoontek

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.

zoontek avatar Nov 10 '20 09:11 zoontek

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 avatar Mar 01 '22 08:03 mashad6

@mashad6 No. android:configChanges accepts locale, it's imho the way to go to implement this after all.

zoontek avatar Mar 01 '22 08:03 zoontek

@zoontek so there is no way i can get event if i turn on and off "automatic date & time " flag?

mashad6 avatar Mar 01 '22 10:03 mashad6