Page MenuHomePhabricator

[native] Use currently selected theme for alerts.
ClosedPublic

Authored by patryk on Jul 20 2023, 6:03 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Nov 23, 11:36 PM
Unknown Object (File)
Sat, Nov 23, 2:36 PM
Unknown Object (File)
Sat, Nov 23, 12:53 PM
Unknown Object (File)
Sun, Nov 10, 9:17 PM
Unknown Object (File)
Sun, Nov 10, 8:20 AM
Unknown Object (File)
Fri, Nov 1, 7:56 AM
Unknown Object (File)
Fri, Nov 1, 7:56 AM
Unknown Object (File)
Fri, Nov 1, 7:56 AM
Subscribers

Details

Summary

Solution for ENG-3935.

This diff introduces a wrapper for React Native alerts, which synchronizes currently selected theme in the app with alert style. Right now userInterfaceStyle is only supported on iOS.

Light theme alert:

t1.png (956×453 px, 117 KB)

Dark theme alert:
t2.png (956×453 px, 113 KB)

Test Plan
  1. Select a location where you want to implement an alert (for example, you can add useEffect in root.react.js).
  2. Verify whether the alert theme is consistent with the app theme.
  3. Navigate to profile > appearance, alter the theme, and check if the same transformation applies to the other theme.

Diff Detail

Repository
rCOMM Comm
Branch
patryk/native-warn-user
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

patryk held this revision as a draft.
patryk retitled this revision from [native] Use currently selected theme on alerts to [native] Use currently selected theme for alerts..Jul 20 2023, 6:24 AM
patryk edited the summary of this revision. (Show Details)
patryk edited the test plan for this revision. (Show Details)
patryk added reviewers: bartek, tomek, michal, inka.
patryk edited the test plan for this revision. (Show Details)
patryk attached a referenced file: F646511: t2.png. (Show Details)
patryk attached a referenced file: F646510: t1.png. (Show Details)
patryk added inline comments.
native/utils/alert.js
8–11 ↗(On Diff #28885)

We need to store information about options argument position, according to method name. Maybe there exists a better way to avoid hardcoding positions (ex. by using reflection) but alert API probably won't change in the future.

35 ↗(On Diff #28885)

Using typeof, because Alert uses static methods, which are unavailable on Alert type.

native/utils/alert.js
35 ↗(On Diff #28885)

We could also implement this wrapper by creating a new class with static method called alert, but flow wants a type for method arguments. We could do it, but we would need to use Parameters<F> (which is available from 0.209).

[deleted] My bad, turns out if you don't reload the app manually those changes don't take effect for some reason

native/utils/alert.js
26–28 ↗(On Diff #28885)

Why if there is no userInterfaceStyle, you delete other option fields??

inka requested changes to this revision.Jul 21 2023, 3:52 AM
This revision now requires changes to proceed.Jul 21 2023, 3:52 AM
inka added 1 blocking reviewer(s): tomek.

I don't like the solution with hardcoding argument positions, but I tried to figure out a clearly better way, and couldn't find one. There are many that are about equally bad in my opinion. But I would prefer if @tomek or someone else could take look as well.

tomek requested changes to this revision.Jul 24 2023, 5:54 AM

Is there a diff where we start using this instead of the default Alert?

I don't like the solution with hardcoding argument positions, but I tried to figure out a clearly better way, and couldn't find one. There are many that are about equally bad in my opinion. But I would prefer if @tomek or someone else could take look as well.

Agree that this isn't ideal. Wondering how type-safe this solution is. Also, not sure if accessing state directly won't cause any issues (e.g. if we store Alert.alert in a variable, would it update after state change?).

How about creating a custom hook, e.g. useAlert() that returns { alert, prompt } that have the same signatures as the original ones, but have userInterfaceStyle hardcoded? In this hook we can useSelector so that the functions get updated after state change.

native/utils/alert.js
24

Is it safe to specify this for Android?

This revision now requires changes to proceed.Jul 24 2023, 5:54 AM

How about creating a custom hook

Creating a custom hook is not a viable option. Consider dev-menu.js as an example. There are instances where we use Alert outside of a React component.

Also, not sure if accessing state directly won't cause any issues (e.g. if we store Alert.alert in a variable, would it update after state change?).

I've tried storing Alert.alert in a variable and didn't notice any problems. We could potentially use a method like store.subscribe to update the variable when the state changes. However, after reviewing the example in the documentation, it seems this might not be necessary -- we would still use the store.getState() method. Also using store.subscribe would be inefficient, because we would update the variable even when we don't use alert.

I'm going to refactor the code: rather than focusing on creating the "generic code", we should focus on being type-safe (e.g. when alert arguments change, flow should throw an error).

native/utils/alert.js
24

iOS uses prompt even when you use alert (look here), and the only place where userInterfaceStyle is accessed is in prompt.

This revision is now accepted and ready to land.Jul 25 2023, 2:29 AM
native/utils/alert.js
9–10 ↗(On Diff #28987)

We're exporting this type, so it should be read-only to make sure that consumers don't modify it

This revision was landed with ongoing or failed builds.Aug 3 2023, 12:02 PM
This revision was automatically updated to reflect the committed changes.