@umang-thakkar/base-template 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -304
- package/package.json +11 -2
- package/template/.eslintrc +16 -46
- package/template/.prettierrc.js +8 -0
- package/template/App.tsx +11 -15
- package/template/Main.tsx +29 -5
- package/template/__mocks__/react-native-mmkv.ts +4 -4
- package/template/__tests__/App.test.tsx +6 -1
- package/template/__tests__/helpers.test.ts +43 -0
- package/template/babel.config.js +2 -1
- package/template/constants/API_Routes.ts +19 -0
- package/template/constants/AppConstants.tsx +1 -1
- package/template/constants/AppString.ts +36 -0
- package/template/constants/StoragePreferences.ts +16 -31
- package/template/jest.config.js +5 -0
- package/template/jest.setup.js +5 -0
- package/template/package.json +2 -3
- package/template/src/api/AxiosActions.ts +65 -28
- package/template/src/api/AxiosClient.ts +13 -7
- package/template/src/api/queries.ts +59 -29
- package/template/src/api/service.ts +23 -9
- package/template/src/components/ErrorBoundary.tsx +27 -31
- package/template/src/components/SnackbarHost.tsx +86 -0
- package/template/src/helper/colors.ts +22 -22
- package/template/src/helper/common-functions.ts +56 -10
- package/template/src/helper/matrics.ts +4 -6
- package/template/src/helper/navigation-service.ts +21 -24
- package/template/src/helper/snackbar.ts +41 -0
- package/template/src/helper/typography.ts +2 -2
- package/template/src/helper/utility-functions.ts +10 -16
- package/template/src/hooks/ThemeContext.tsx +60 -30
- package/template/src/hooks/usePaginatedAPI.ts +96 -0
- package/template/src/navigation/AppStack.tsx +36 -27
- package/template/src/navigation/screens.ts +17 -11
- package/template/src/redux/slice.ts +17 -12
- package/template/src/redux/slices/authSlice.ts +11 -14
- package/template/src/redux/store.ts +13 -9
- package/template/src/redux/types.d.ts +40 -19
- package/template/src/screens/HomeScreen.tsx +103 -29
- package/template/src/screens/LoginScreen.tsx +101 -69
- package/template/src/screens/SplashScreen.tsx +36 -25
- package/template/src/theme/darkTheme.ts +13 -6
- package/template/src/theme/index.ts +4 -3
- package/template/src/theme/lightTheme.ts +13 -5
- package/template/ios/.xcode.env.local +0 -1
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import { Pressable, StyleSheet, Text, View } from 'react-native';
|
|
3
|
+
import Animated, { FadeInDown, FadeOutDown } from 'react-native-reanimated';
|
|
4
|
+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|
5
|
+
import { subscribeSnackbar, type SnackbarMessage } from '../helper/snackbar';
|
|
6
|
+
import { useTheme } from '../hooks/ThemeContext';
|
|
7
|
+
import { Typography } from '../helper/typography';
|
|
8
|
+
import { horizontalScale, moderateScale, verticalScale } from '../helper/matrics';
|
|
9
|
+
import { colors } from '../helper/colors';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Renders messages sent with showSnackbar / showErrorSnackbar. Mounted once
|
|
13
|
+
* in App.tsx inside ThemeProvider; one message at a time, newest wins.
|
|
14
|
+
*/
|
|
15
|
+
const SnackbarHost = () => {
|
|
16
|
+
const insets = useSafeAreaInsets();
|
|
17
|
+
const { theme } = useTheme();
|
|
18
|
+
const [snack, setSnack] = useState<SnackbarMessage | null>(null);
|
|
19
|
+
|
|
20
|
+
useEffect(() => subscribeSnackbar(setSnack), []);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (!snack) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const timer = setTimeout(() => setSnack(null), snack.duration);
|
|
27
|
+
return () => clearTimeout(timer);
|
|
28
|
+
}, [snack]);
|
|
29
|
+
|
|
30
|
+
const isError = snack?.type === 'error';
|
|
31
|
+
const backgroundColor = isError ? theme.error : theme.text;
|
|
32
|
+
const color = isError ? theme.onPrimary : theme.background;
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<View
|
|
36
|
+
pointerEvents="box-none"
|
|
37
|
+
style={[styles.container, { paddingBottom: insets.bottom + verticalScale(16) }]}>
|
|
38
|
+
{snack ? (
|
|
39
|
+
<Animated.View key={snack.id} entering={FadeInDown} exiting={FadeOutDown}>
|
|
40
|
+
<Pressable
|
|
41
|
+
onPress={() => setSnack(null)}
|
|
42
|
+
accessibilityRole="alert"
|
|
43
|
+
accessibilityLiveRegion="polite"
|
|
44
|
+
style={[styles.snack, { backgroundColor }]}>
|
|
45
|
+
<Text style={[Typography.subhead, styles.title, { color }]}>{snack.title}</Text>
|
|
46
|
+
{snack.message ? (
|
|
47
|
+
<Text style={[Typography.footnote, styles.message, { color }]}>{snack.message}</Text>
|
|
48
|
+
) : null}
|
|
49
|
+
</Pressable>
|
|
50
|
+
</Animated.View>
|
|
51
|
+
) : null}
|
|
52
|
+
</View>
|
|
53
|
+
);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export default SnackbarHost;
|
|
57
|
+
|
|
58
|
+
const styles = StyleSheet.create({
|
|
59
|
+
container: {
|
|
60
|
+
position: 'absolute',
|
|
61
|
+
left: 0,
|
|
62
|
+
right: 0,
|
|
63
|
+
bottom: 0,
|
|
64
|
+
paddingHorizontal: horizontalScale(16),
|
|
65
|
+
},
|
|
66
|
+
snack: {
|
|
67
|
+
width: '100%',
|
|
68
|
+
maxWidth: 560,
|
|
69
|
+
alignSelf: 'center',
|
|
70
|
+
borderRadius: moderateScale(12),
|
|
71
|
+
paddingHorizontal: horizontalScale(16),
|
|
72
|
+
paddingVertical: verticalScale(12),
|
|
73
|
+
elevation: 4,
|
|
74
|
+
shadowColor: colors.black,
|
|
75
|
+
shadowOffset: { width: 0, height: 2 },
|
|
76
|
+
shadowOpacity: 0.2,
|
|
77
|
+
shadowRadius: 6,
|
|
78
|
+
},
|
|
79
|
+
title: {
|
|
80
|
+
fontWeight: '600',
|
|
81
|
+
},
|
|
82
|
+
message: {
|
|
83
|
+
marginTop: verticalScale(2),
|
|
84
|
+
opacity: 0.8,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
export const colors = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
2
|
+
grayColor: '#9B9B9B',
|
|
3
|
+
dividerColor: '#BDBDBD',
|
|
4
|
+
fieldColor: '#F5F5F5',
|
|
5
|
+
yellowColor: '#FDC60A',
|
|
6
|
+
navyBlue: '#3C5A99',
|
|
7
|
+
kashmir: '#5D6D7E',
|
|
8
|
+
orangeColor: '#E5634D',
|
|
9
|
+
blueColor: '#5DADE2',
|
|
10
|
+
pinkColor: '#A569BD',
|
|
11
|
+
pinkLightColor: '#FF5E80',
|
|
12
|
+
pinkDarkColor: '#F90030',
|
|
13
|
+
accentColor: '#4A90A4',
|
|
14
|
+
//App colors
|
|
15
|
+
whiteColor: '#FFFFFF',
|
|
16
|
+
purple: '#6739B7',
|
|
17
|
+
green: '#4BB543',
|
|
18
|
+
suvaGrey: '#909090',
|
|
19
|
+
airForceBlue: '#6687A5',
|
|
20
|
+
red: '#D73F5A',
|
|
21
|
+
lightGray: '#B3B3B3',
|
|
22
|
+
black: '#000000',
|
|
23
|
+
kashmirBlue: '#566A90',
|
|
24
24
|
};
|
|
@@ -1,19 +1,65 @@
|
|
|
1
|
-
import axios from
|
|
2
|
-
import { format, isValid, parseISO } from
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { format, isValid, parseISO } from 'date-fns';
|
|
3
|
+
import { AppString } from '../../constants/AppString';
|
|
4
|
+
import { pushSnackbar } from './snackbar';
|
|
3
5
|
|
|
4
|
-
|
|
6
|
+
// ---------- Feedback ----------
|
|
7
|
+
|
|
8
|
+
/** Neutral/success message at the bottom of the screen. */
|
|
9
|
+
export const showSnackbar = (title: string, message?: string) =>
|
|
10
|
+
pushSnackbar(title, { message, type: 'success' });
|
|
11
|
+
|
|
12
|
+
/** Error message. Pass the error's message, or nothing for the generic one. */
|
|
13
|
+
export const showErrorSnackbar = (message?: string) =>
|
|
14
|
+
pushSnackbar(message || AppString.somethingWentWrong, { type: 'error' });
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Best human-readable message from anything thrown by an API call:
|
|
18
|
+
* a backend error body ({ message }), an AxiosError, an Error or a string.
|
|
19
|
+
*/
|
|
20
|
+
export const getErrorMessage = (error: unknown): string => {
|
|
5
21
|
if (axios.isAxiosError(error)) {
|
|
6
22
|
return error.response?.data?.message || error.message;
|
|
7
23
|
}
|
|
8
|
-
|
|
24
|
+
if (error && typeof error === 'object' && 'message' in error) {
|
|
25
|
+
return String((error as { message: unknown }).message);
|
|
26
|
+
}
|
|
27
|
+
if (typeof error === 'string') {
|
|
28
|
+
return error;
|
|
29
|
+
}
|
|
30
|
+
return AppString.somethingWentWrong;
|
|
9
31
|
};
|
|
10
32
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
33
|
+
// ---------- Formatting ----------
|
|
34
|
+
|
|
35
|
+
/** 1234567 -> "1,234,567" */
|
|
36
|
+
export const formatWithComma = (value: number, locale = 'en-US') =>
|
|
37
|
+
new Intl.NumberFormat(locale, { maximumFractionDigits: 0 }).format(value);
|
|
38
|
+
|
|
39
|
+
/** 1234.5 -> "1,234.50" */
|
|
40
|
+
export const formatPrice = (value: number, locale = 'en-US') =>
|
|
41
|
+
new Intl.NumberFormat(locale, { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(
|
|
42
|
+
value,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/** ISO string -> formatted date (date-fns tokens), or null when invalid. */
|
|
46
|
+
export function toFormattedDate(dateString: string, formatStr: string): string | null {
|
|
47
|
+
if (!dateString) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
16
50
|
const date = parseISO(dateString);
|
|
17
|
-
if (!isValid(date))
|
|
51
|
+
if (!isValid(date)) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
18
54
|
return format(date, formatStr);
|
|
19
55
|
}
|
|
56
|
+
|
|
57
|
+
// ---------- Payloads ----------
|
|
58
|
+
|
|
59
|
+
/** Drops null, undefined and empty-string values before sending a request body/params. */
|
|
60
|
+
export const cleanPayload = <T extends Record<string, unknown>>(payload: T): Partial<T> =>
|
|
61
|
+
Object.fromEntries(
|
|
62
|
+
Object.entries(payload).filter(
|
|
63
|
+
([, value]) => value !== null && value !== undefined && value !== '',
|
|
64
|
+
),
|
|
65
|
+
) as Partial<T>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Dimensions } from
|
|
1
|
+
import { Dimensions } from 'react-native';
|
|
2
2
|
|
|
3
|
-
const { width, height } = Dimensions.get(
|
|
3
|
+
const { width, height } = Dimensions.get('window');
|
|
4
4
|
|
|
5
5
|
const guidelineBaseWidth: number = 390;
|
|
6
6
|
const guidelineBaseHeight: number = 844;
|
|
@@ -12,11 +12,9 @@ const tabletMaxHeight = 900;
|
|
|
12
12
|
const effectiveWidth = isTablet ? Math.min(width, tabletMaxWidth) : width;
|
|
13
13
|
const effectiveHeight = isTablet ? Math.min(height, tabletMaxHeight) : height;
|
|
14
14
|
|
|
15
|
-
const horizontalScale = (size: number): number =>
|
|
16
|
-
(effectiveWidth / guidelineBaseWidth) * size;
|
|
15
|
+
const horizontalScale = (size: number): number => (effectiveWidth / guidelineBaseWidth) * size;
|
|
17
16
|
|
|
18
|
-
const verticalScale = (size: number): number =>
|
|
19
|
-
(effectiveHeight / guidelineBaseHeight) * size;
|
|
17
|
+
const verticalScale = (size: number): number => (effectiveHeight / guidelineBaseHeight) * size;
|
|
20
18
|
|
|
21
19
|
const moderateScale = (size: number, factor: number = 0.5): number =>
|
|
22
20
|
size + (horizontalScale(size) - size) * factor;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// navigationService.ts
|
|
2
|
-
import * as React from
|
|
3
|
-
import { CommonActions, StackActions } from
|
|
4
|
-
import type { NavigationContainerRef } from
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { CommonActions, StackActions } from '@react-navigation/native';
|
|
4
|
+
import type { NavigationContainerRef } from '@react-navigation/native';
|
|
5
5
|
|
|
6
6
|
// Create a ref to hold navigation
|
|
7
7
|
export const navigationRef = React.createRef<NavigationContainerRef<any>>();
|
|
@@ -10,60 +10,57 @@ export const navigationRef = React.createRef<NavigationContainerRef<any>>();
|
|
|
10
10
|
* Navigate to a screen by name
|
|
11
11
|
*/
|
|
12
12
|
export function navigate(name: string, params?: object) {
|
|
13
|
-
|
|
13
|
+
navigationRef.current?.navigate(name, params);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Go back to the previous screen
|
|
18
18
|
*/
|
|
19
19
|
export function goBack() {
|
|
20
|
-
|
|
20
|
+
navigationRef.current?.goBack();
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* Reset navigation with multiple routes
|
|
25
25
|
*/
|
|
26
|
-
export function navigateAndReset(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
routes,
|
|
34
|
-
})
|
|
35
|
-
);
|
|
26
|
+
export function navigateAndReset(routes: { name: string; params?: object }[] = [], index = 0) {
|
|
27
|
+
navigationRef.current?.dispatch(
|
|
28
|
+
CommonActions.reset({
|
|
29
|
+
index,
|
|
30
|
+
routes,
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
36
33
|
}
|
|
37
34
|
|
|
38
35
|
/**
|
|
39
36
|
* Reset navigation with a single route
|
|
40
37
|
*/
|
|
41
38
|
export function navigateAndSimpleReset(name: string, index = 0) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
navigationRef.current?.dispatch(
|
|
40
|
+
CommonActions.reset({
|
|
41
|
+
index,
|
|
42
|
+
routes: [{ name }],
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
/**
|
|
51
48
|
* Push a new screen on top of the stack
|
|
52
49
|
*/
|
|
53
50
|
export function push(name: string, params?: object) {
|
|
54
|
-
|
|
51
|
+
navigationRef.current?.dispatch(StackActions.push(name, params));
|
|
55
52
|
}
|
|
56
53
|
|
|
57
54
|
/**
|
|
58
55
|
* Pop the current screen
|
|
59
56
|
*/
|
|
60
57
|
export function pop(count: number = 1) {
|
|
61
|
-
|
|
58
|
+
navigationRef.current?.dispatch(StackActions.pop(count));
|
|
62
59
|
}
|
|
63
60
|
|
|
64
61
|
/**
|
|
65
62
|
* Pop to the top of the stack
|
|
66
63
|
*/
|
|
67
64
|
export function popToTop() {
|
|
68
|
-
|
|
65
|
+
navigationRef.current?.dispatch(StackActions.popToTop());
|
|
69
66
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny snackbar store. `showSnackbar` / `showErrorSnackbar` (re-exported from
|
|
3
|
+
* common-functions.ts) can be called from anywhere: screens, mutation
|
|
4
|
+
* callbacks, axios interceptors. <SnackbarHost /> in App.tsx renders them.
|
|
5
|
+
*/
|
|
6
|
+
export type SnackbarType = 'success' | 'error' | 'info';
|
|
7
|
+
|
|
8
|
+
export interface SnackbarMessage {
|
|
9
|
+
id: number;
|
|
10
|
+
title: string;
|
|
11
|
+
message?: string;
|
|
12
|
+
type: SnackbarType;
|
|
13
|
+
duration: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type Listener = (snack: SnackbarMessage) => void;
|
|
17
|
+
|
|
18
|
+
const DEFAULT_DURATION_MS = 3000;
|
|
19
|
+
const listeners = new Set<Listener>();
|
|
20
|
+
let nextId = 1;
|
|
21
|
+
|
|
22
|
+
export const subscribeSnackbar = (listener: Listener) => {
|
|
23
|
+
listeners.add(listener);
|
|
24
|
+
return () => {
|
|
25
|
+
listeners.delete(listener);
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const pushSnackbar = (
|
|
30
|
+
title: string,
|
|
31
|
+
options: { message?: string; type?: SnackbarType; duration?: number } = {},
|
|
32
|
+
) => {
|
|
33
|
+
const snack: SnackbarMessage = {
|
|
34
|
+
id: nextId++,
|
|
35
|
+
title,
|
|
36
|
+
message: options.message,
|
|
37
|
+
type: options.type ?? 'info',
|
|
38
|
+
duration: options.duration ?? DEFAULT_DURATION_MS,
|
|
39
|
+
};
|
|
40
|
+
listeners.forEach(listener => listener(snack));
|
|
41
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { StyleSheet } from 'react-native';
|
|
2
2
|
|
|
3
|
-
export const FontWeight
|
|
3
|
+
export const FontWeight = {
|
|
4
4
|
thin: '100',
|
|
5
5
|
ultraLight: '200',
|
|
6
6
|
light: '300',
|
|
@@ -10,7 +10,7 @@ export const FontWeight: any = {
|
|
|
10
10
|
bold: '700',
|
|
11
11
|
heavy: '800',
|
|
12
12
|
black: '900',
|
|
13
|
-
};
|
|
13
|
+
} as const;
|
|
14
14
|
|
|
15
15
|
export const Typography = StyleSheet.create({
|
|
16
16
|
header: {
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
// utils.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from "react-native";
|
|
9
|
-
|
|
10
|
-
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get("window");
|
|
11
|
-
|
|
12
|
-
export const isIOS = Platform.OS === "ios";
|
|
13
|
-
export const isAndroid = Platform.OS === "android";
|
|
2
|
+
import { Dimensions, Platform, PixelRatio, Keyboard, Alert } from 'react-native';
|
|
3
|
+
|
|
4
|
+
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');
|
|
5
|
+
|
|
6
|
+
export const isIOS = Platform.OS === 'ios';
|
|
7
|
+
export const isAndroid = Platform.OS === 'android';
|
|
14
8
|
export const deviceWidth = SCREEN_WIDTH;
|
|
15
9
|
export const deviceHeight = SCREEN_HEIGHT;
|
|
16
10
|
|
|
@@ -40,8 +34,8 @@ export const hideKeyboard = () => {
|
|
|
40
34
|
export const showAlert = (
|
|
41
35
|
title: string,
|
|
42
36
|
message: string,
|
|
43
|
-
settingsText =
|
|
44
|
-
rejectText =
|
|
37
|
+
settingsText = 'Open Settings',
|
|
38
|
+
rejectText = 'Reject',
|
|
45
39
|
onSettingsPress: () => void,
|
|
46
40
|
) => {
|
|
47
41
|
Alert.alert(
|
|
@@ -51,11 +45,11 @@ export const showAlert = (
|
|
|
51
45
|
{
|
|
52
46
|
text: settingsText,
|
|
53
47
|
onPress: onSettingsPress,
|
|
54
|
-
style:
|
|
48
|
+
style: 'default',
|
|
55
49
|
},
|
|
56
50
|
{
|
|
57
51
|
text: rejectText,
|
|
58
|
-
style:
|
|
52
|
+
style: 'destructive',
|
|
59
53
|
},
|
|
60
54
|
],
|
|
61
55
|
{ cancelable: false },
|
|
@@ -1,43 +1,73 @@
|
|
|
1
|
-
import React, { createContext, useContext, useEffect, useState } from 'react';
|
|
1
|
+
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
|
|
2
2
|
import { Appearance } from 'react-native';
|
|
3
|
-
import { themes, ThemeType } from '../theme';
|
|
3
|
+
import { themes, ThemeMode, ThemeType } from '../theme';
|
|
4
|
+
import { StorageKeys, StoragePreferences } from '../../constants/StoragePreferences';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
/** What the user picked: a fixed mode, or follow the OS. */
|
|
7
|
+
export type ThemePreference = ThemeMode | 'system';
|
|
6
8
|
|
|
7
9
|
interface ThemeContextProps {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/** Colors of the active theme. Use them in styles: `{ color: theme.text }`. */
|
|
11
|
+
theme: ThemeType;
|
|
12
|
+
/** The mode actually shown. */
|
|
13
|
+
mode: ThemeMode;
|
|
14
|
+
/** The user's choice, persisted in MMKV. */
|
|
15
|
+
preference: ThemePreference;
|
|
16
|
+
setPreference: (preference: ThemePreference) => void;
|
|
17
|
+
/** Switches between light and dark (pins the choice). */
|
|
18
|
+
toggleTheme: () => void;
|
|
11
19
|
}
|
|
12
20
|
|
|
13
21
|
const ThemeContext = createContext<ThemeContextProps | undefined>(undefined);
|
|
14
22
|
|
|
23
|
+
const readSystemMode = (): ThemeMode => (Appearance.getColorScheme() === 'dark' ? 'dark' : 'light');
|
|
24
|
+
|
|
25
|
+
const readStoredPreference = (): ThemePreference => {
|
|
26
|
+
const stored = StoragePreferences.getString(StorageKeys.THEME_MODE);
|
|
27
|
+
return stored === 'light' || stored === 'dark' ? stored : 'system';
|
|
28
|
+
};
|
|
29
|
+
|
|
15
30
|
export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
const [systemMode, setSystemMode] = useState<ThemeMode>(readSystemMode);
|
|
32
|
+
// MMKV is synchronous, so the saved choice applies on the first frame.
|
|
33
|
+
const [preference, setPreferenceState] = useState<ThemePreference>(readStoredPreference);
|
|
34
|
+
const mode: ThemeMode = preference === 'system' ? systemMode : preference;
|
|
35
|
+
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
const listener = Appearance.addChangeListener(({ colorScheme }) => {
|
|
38
|
+
if (colorScheme === 'light' || colorScheme === 'dark') {
|
|
39
|
+
setSystemMode(colorScheme);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return () => listener.remove();
|
|
43
|
+
}, []);
|
|
44
|
+
|
|
45
|
+
const value = useMemo<ThemeContextProps>(() => {
|
|
46
|
+
const setPreference = (next: ThemePreference) => {
|
|
47
|
+
if (next === 'system') {
|
|
48
|
+
StoragePreferences.remove(StorageKeys.THEME_MODE);
|
|
49
|
+
} else {
|
|
50
|
+
StoragePreferences.setString(StorageKeys.THEME_MODE, next);
|
|
51
|
+
}
|
|
52
|
+
setPreferenceState(next);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
theme: themes[mode],
|
|
57
|
+
mode,
|
|
58
|
+
preference,
|
|
59
|
+
setPreference,
|
|
60
|
+
toggleTheme: () => setPreference(mode === 'light' ? 'dark' : 'light'),
|
|
61
|
+
};
|
|
62
|
+
}, [mode, preference]);
|
|
63
|
+
|
|
64
|
+
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
|
|
35
65
|
};
|
|
36
66
|
|
|
37
67
|
export const useTheme = () => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
68
|
+
const context = useContext(ThemeContext);
|
|
69
|
+
if (!context) {
|
|
70
|
+
throw new Error('useTheme must be used within ThemeProvider');
|
|
71
|
+
}
|
|
72
|
+
return context;
|
|
43
73
|
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { useInfiniteQuery, type QueryKey } from '@tanstack/react-query';
|
|
3
|
+
|
|
4
|
+
const SEARCH_DEBOUNCE_MS = 400;
|
|
5
|
+
|
|
6
|
+
interface PageContext<TParams> {
|
|
7
|
+
pageParam: number;
|
|
8
|
+
limit: number;
|
|
9
|
+
/** Debounced search term ('' when empty). */
|
|
10
|
+
search: string;
|
|
11
|
+
dynamicParams?: TParams;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Options<TPage, TItem, TParams> {
|
|
15
|
+
/** Base key. The search term and dynamicParams are appended automatically. */
|
|
16
|
+
queryKey: QueryKey;
|
|
17
|
+
queryFn: (ctx: PageContext<TParams>) => Promise<TPage>;
|
|
18
|
+
/** Flattens all loaded pages into one list. */
|
|
19
|
+
select: (pages: TPage[]) => TItem[];
|
|
20
|
+
limit?: number;
|
|
21
|
+
/** Extra values the request depends on (filters, ids). Part of the cache key. */
|
|
22
|
+
dynamicParams?: TParams;
|
|
23
|
+
/**
|
|
24
|
+
* Whether another page exists. Default: the last page returned `limit`
|
|
25
|
+
* items. Override when the API returns a total or a `hasMore` flag.
|
|
26
|
+
*/
|
|
27
|
+
hasNextPage?: (lastPage: TPage, allPages: TPage[], limit: number) => boolean;
|
|
28
|
+
firstPage?: number;
|
|
29
|
+
staleTime?: number;
|
|
30
|
+
enabled?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Infinite-scroll helper on top of useInfiniteQuery.
|
|
35
|
+
*
|
|
36
|
+
* const { data, handleScrollToBottom, handleRefetch, ... } = useTodoList();
|
|
37
|
+
* <FlatList data={data} onEndReached={handleScrollToBottom} onRefresh={handleRefetch} />
|
|
38
|
+
*/
|
|
39
|
+
export function usePaginatedAPI<TPage, TItem, TParams = undefined>({
|
|
40
|
+
queryKey,
|
|
41
|
+
queryFn,
|
|
42
|
+
select,
|
|
43
|
+
limit = 10,
|
|
44
|
+
dynamicParams,
|
|
45
|
+
hasNextPage,
|
|
46
|
+
firstPage = 1,
|
|
47
|
+
staleTime,
|
|
48
|
+
enabled = true,
|
|
49
|
+
}: Options<TPage, TItem, TParams>) {
|
|
50
|
+
const [search, setSearch] = useState('');
|
|
51
|
+
const [debouncedSearch, setDebouncedSearch] = useState('');
|
|
52
|
+
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
const timer = setTimeout(() => setDebouncedSearch(search.trim()), SEARCH_DEBOUNCE_MS);
|
|
55
|
+
return () => clearTimeout(timer);
|
|
56
|
+
}, [search]);
|
|
57
|
+
|
|
58
|
+
const query = useInfiniteQuery({
|
|
59
|
+
queryKey: [...queryKey, { search: debouncedSearch, limit, ...dynamicParams }],
|
|
60
|
+
queryFn: ({ pageParam }) =>
|
|
61
|
+
queryFn({ pageParam, limit, search: debouncedSearch, dynamicParams }),
|
|
62
|
+
initialPageParam: firstPage,
|
|
63
|
+
getNextPageParam: (lastPage, allPages, lastPageParam) => {
|
|
64
|
+
const more = hasNextPage
|
|
65
|
+
? hasNextPage(lastPage, allPages, limit)
|
|
66
|
+
: select([lastPage]).length >= limit;
|
|
67
|
+
return more ? lastPageParam + 1 : undefined;
|
|
68
|
+
},
|
|
69
|
+
select: result => select(result.pages),
|
|
70
|
+
staleTime,
|
|
71
|
+
enabled,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const handleScrollToBottom = () => {
|
|
75
|
+
if (query.hasNextPage && !query.isFetchingNextPage && !query.isFetching) {
|
|
76
|
+
query.fetchNextPage();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
data: query.data ?? [],
|
|
82
|
+
isLoading: query.isLoading,
|
|
83
|
+
isFetchingNextPage: query.isFetchingNextPage,
|
|
84
|
+
isRefreshing: query.isRefetching && !query.isFetchingNextPage,
|
|
85
|
+
hasNextPage: query.hasNextPage,
|
|
86
|
+
error: query.error,
|
|
87
|
+
handleScrollToBottom,
|
|
88
|
+
handleRefetch: () => query.refetch(),
|
|
89
|
+
search,
|
|
90
|
+
handleSearch: setSearch,
|
|
91
|
+
resetSearch: () => setSearch(''),
|
|
92
|
+
query,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default usePaginatedAPI;
|