Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to persist theme selection #941

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ jest.mock("expo-asset", () => {
}
return { ...actual, Asset, getManifestBaseUrl: () => "" };
});

jest.mock("@react-native-async-storage/async-storage", () =>
require("@react-native-async-storage/async-storage/jest/async-storage-mock")
);
4 changes: 4 additions & 0 deletions packages/maps/jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ jest.mock("expo-asset", () => {
}
return { ...actual, Asset, getManifestBaseUrl: () => "" };
});

jest.mock("@react-native-async-storage/async-storage", () =>
require("@react-native-async-storage/async-storage/jest/async-storage-mock")
);
3 changes: 2 additions & 1 deletion packages/theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"color": "^4.2.3",
"deepmerge": "^4.3.1",
"lodash.isobject": "^3.0.2",
"react-native-typography": "^1.4.1"
"react-native-typography": "^1.4.1",
"@react-native-async-storage/async-storage": "1.21.0"
},
"eslintIgnore": [
"node_modules/",
Expand Down
58 changes: 45 additions & 13 deletions packages/theme/src/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import React from "react";
import { Dimensions, Platform } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import createThemeValuesProxy from "./createThemeValuesProxy";
import DefaultTheme from "./DefaultTheme";
import { Breakpoints, ReadTheme, ValidatedTheme } from "./types";
import {
Breakpoints,
ChangeThemeOptions,
ReadTheme,
ValidatedTheme,
} from "./types";

const SAVED_SELECTED_THEME_KEY = "saved_selected_theme";

type ThemeContextType = {
theme: ReadTheme;
Expand Down Expand Up @@ -34,18 +42,9 @@ const Provider: React.FC<React.PropsWithChildren<ProviderProps>> = ({
Dimensions.get("window").width
);

React.useEffect(() => {
const listener = Dimensions.addEventListener("change", ({ window }) =>
setDeviceWidth(window.width)
);
return () => {
listener.remove();
};
});

const changeTheme = React.useCallback(
(themeName: string) => {
const theme = themes.find((theme) => theme.name === themeName);
(themeName: string, options?: ChangeThemeOptions) => {
const theme = themes.find((t) => t.name === themeName);
if (!theme) {
console.warn(
"Theme with name",
Expand All @@ -55,6 +54,12 @@ const Provider: React.FC<React.PropsWithChildren<ProviderProps>> = ({
return;
YoussefHenna marked this conversation as resolved.
Show resolved Hide resolved
}
setCurrentTheme(theme);

if (options?.persistent === true) {
AsyncStorage.setItem(SAVED_SELECTED_THEME_KEY, themeName).catch((e) => {
console.warn("Failed to persist selected theme", e);
});
}
},
[themes, setCurrentTheme]
);
Expand Down Expand Up @@ -98,6 +103,30 @@ const Provider: React.FC<React.PropsWithChildren<ProviderProps>> = ({
[currentTheme, deviceWidth, breakpoints]
);

React.useEffect(() => {
const listener = Dimensions.addEventListener("change", ({ window }) =>
setDeviceWidth(window.width)
);
return () => {
listener.remove();
};
});

React.useEffect(() => {
const run = async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider if the UI shakes slightly during initialization due to the absence of a wait state when loading the theme.

👉 Example: the UI may briefly display with theme1 before switching to theme2, causing a flicker.

If thorough testing shows no issue, you can disregard this comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since storage is async, the only way to fix an eventual flicker would be to render nothing and wait on the storage to give back data. Let see if the current approach is working and fix it if it causes flickers!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about the same thing, but saw no flicker during testing. We can always come back to it and fix if the issue does arise at some point.

const savedSelectedThemeName = await AsyncStorage.getItem(
SAVED_SELECTED_THEME_KEY
);
if (savedSelectedThemeName) {
changeTheme(savedSelectedThemeName);
}
};
run();

// This should only run once, ignore deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<ThemeContext.Provider value={{ theme: proxiedTheme, changeTheme }}>
{children}
Expand All @@ -110,7 +139,10 @@ const useTheme = (): ReadTheme => {
return theme;
};

const useChangeTheme = (): ((themeName: string) => void) => {
const useChangeTheme = (): ((
themeName: string,
options?: ChangeThemeOptions
) => void) => {
const { changeTheme } = React.useContext(ThemeContext);
return changeTheme;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/theme/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ export type ColorPalettes = {
export type Breakpoints = {
[key: string]: number;
};

export type ChangeThemeOptions = { persistent?: boolean };
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3371,6 +3371,13 @@
resolved "https://registry.yarnpkg.com/@react-google-maps/marker-clusterer/-/marker-clusterer-2.16.1.tgz#882878a7b49ce9ae44c7b02ff6fbc4c2aeb77c95"
integrity sha512-jOuyqzWLeXvQcoAu6TCVWHAuko+sDt0JjawNHBGqUNLywMtTCvYP0L0PiqJZOUCUeRYGdUy0AKxQ+30vAkvwag==

"@react-native-async-storage/async-storage@1.21.0":
version "1.21.0"
resolved "https://registry.yarnpkg.com/@react-native-async-storage/async-storage/-/async-storage-1.21.0.tgz#d7e370028e228ab84637016ceeb495878b7a44c8"
integrity sha512-JL0w36KuFHFCvnbOXRekqVAUplmOyT/OuCQkogo6X98MtpSaJOKEAeZnYO8JB0U/RIEixZaGI5px73YbRm/oag==
dependencies:
merge-options "^3.0.4"

"@react-native-community/cli-clean@12.3.6":
version "12.3.6"
resolved "https://registry.yarnpkg.com/@react-native-community/cli-clean/-/cli-clean-12.3.6.tgz#e8a7910bebc97266fd5068649013a03958021fc4"
Expand Down Expand Up @@ -9117,7 +9124,7 @@ is-plain-obj@^1.0.0, is-plain-obj@^1.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
integrity sha512-yvkRyxmFKEOQ4pNXCmJG5AEQNlXJS5LaONXo5/cLdTZdWvsZ1ioJEonLGAosKlMWE8lwUy/bJzMjcw8az73+Fg==

is-plain-obj@^2.0.0:
is-plain-obj@^2.0.0, is-plain-obj@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==
Expand Down Expand Up @@ -10731,6 +10738,13 @@ merge-descriptors@1.0.1:
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61"
integrity sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==

merge-options@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/merge-options/-/merge-options-3.0.4.tgz#84709c2aa2a4b24c1981f66c179fe5565cc6dbb7"
integrity sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==
dependencies:
is-plain-obj "^2.1.0"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
Expand Down
Loading