Skip to content

Commit

Permalink
refactor: coding refine
Browse files Browse the repository at this point in the history
  • Loading branch information
shensven committed Aug 1, 2022
1 parent 3805514 commit 797b309
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Toast from './src/screens/components/Toast';
export const storage = new MMKV();
export const mmkvAppearance = storage.getString('@appearance');
export const mmkvBrandKeywords = storage.getString('@brandKeywords');
export const mmkvCopywriter = storage.getString('@copywriter');
export const mmkvCopywritings = storage.getString('@copywritings');

if (__DEV__) {
initializeMMKVFlipper({default: storage});
Expand Down
12 changes: 6 additions & 6 deletions src/atoms/appAtom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {atom} from 'jotai';
import {mmkvAppearance, mmkvBrandKeywords, mmkvCopywriter} from '../../App';
import {mmkvAppearance, mmkvBrandKeywords, mmkvCopywritings} from '../../App';

export type StatusBarStyle = 'light-content' | 'dark-content';

Expand Down Expand Up @@ -38,15 +38,15 @@ export interface Copywriter {
version: number;
bundle: {text: string}[];
}
let initCopywriter: Copywriter | undefined;
if (mmkvCopywriter) {
initCopywriter = JSON.parse(mmkvCopywriter);
let initCopywritings: Copywriter | undefined;
if (mmkvCopywritings) {
initCopywritings = JSON.parse(mmkvCopywritings);
} else {
initCopywriter = {
initCopywritings = {
version: 2022000000,
bundle: [
{text: '我开始留短发、减肥、换风格、开始往前冲,不好意思啊,这一次,${brand-zh-CN}疯狂星期四,我一定要吃。'},
],
};
}
export const atomCopywriter = atom<Copywriter>(initCopywriter!);
export const atomCopywritings = atom<Copywriter>(initCopywritings!);
14 changes: 7 additions & 7 deletions src/screens/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {StackScreenProps} from '@react-navigation/stack';
import {useUpdateEffect} from 'ahooks';
import {random} from 'lodash';
import color from 'color';
import useCopywriter from '../utils/useCopywriter';
import useCopywritings from '../utils/useCopywritings';
import useBrandKeywords from '../utils/useBrandKeywords';
import useClipboard from '../utils/useClipboard';

Expand Down Expand Up @@ -50,12 +50,12 @@ const Home: React.FC = () => {

const {copyToClipboard} = useClipboard();

const {copywriter, updateCopywriter} = useCopywriter();
const {copywritings, updateCopywritings} = useCopywritings();
const {brandKeywords} = useBrandKeywords();

const [index, setIndex] = useState<Index>({
image: random(0, 9),
text: copywriter.bundle.length > 1 ? random(0, copywriter.bundle.length - 1) : 0,
text: copywritings.bundle.length > 1 ? random(0, copywritings.bundle.length - 1) : 0,
});

const [currentCopywriter, setCurrentCopywriter] = useState({
Expand All @@ -66,14 +66,14 @@ const Home: React.FC = () => {
const refresh = () => {
setIndex({
image: random(0, 9),
text: random(0, copywriter.bundle.length - 1),
text: random(0, copywritings.bundle.length - 1),
});
};

useEffect(() => {
setCurrentCopywriter({
origin: copywriter.bundle[index.text].text,
withBrand: copywriter.bundle[index.text].text
origin: copywritings.bundle[index.text].text,
withBrand: copywritings.bundle[index.text].text
.replace(/\${brand-zh-CN}/g, brandKeywords.Chinese)
.replace(/\${brand-en-US}/g, brandKeywords.English),
});
Expand All @@ -89,7 +89,7 @@ const Home: React.FC = () => {
}, [brandKeywords]);

useEffect(() => {
updateCopywriter();
updateCopywritings();
}, []);

return (
Expand Down
8 changes: 4 additions & 4 deletions src/screens/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import color from 'color';
import IcRoundSync from './assets/icons/IcRoundSync';
import IcRoundChevronRight from './assets/icons/IcRoundChevronRight';
import IcRoundOpenInNew from './assets/icons/IcRoundOpenInNew';
import useCopywriter from '../utils/useCopywriter';
import useCopywritings from '../utils/useCopywritings';

const version = DeviceInfo.getVersion();
const buildNumber = DeviceInfo.getBuildNumber();
Expand All @@ -27,9 +27,9 @@ const Settings: React.FC = () => {
const navigation = useNavigation<ScreenNavigationProp>();

const {colors} = useTheme();
const {copywriter, updateCopywriter} = useCopywriter();
const {copywritings, updateCopywritings} = useCopywritings();

const copywriterVersion = copywriter.version.toString();
const copywriterVersion = copywritings.version.toString();

const settings = [
{
Expand All @@ -49,7 +49,7 @@ const Settings: React.FC = () => {
6,
)}.${copywriterVersion.substring(6, 8)}.${copywriterVersion.substring(8, 10)}`,
rightIcon: <IcRoundSync size={20} color={colors.onSurfaceVariant} />,
onPress: () => updateCopywriter(),
onPress: () => updateCopywritings(),
},
],
},
Expand Down
16 changes: 8 additions & 8 deletions src/utils/useCopywriter.ts → src/utils/useCopywritings.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import axios from 'axios';
import {useAtom} from 'jotai';
import {atomCopywriter} from '../atoms/appAtom';
import {atomCopywritings} from '../atoms/appAtom';
import type {Copywriter} from '../atoms/appAtom';
import {storage} from '../../App';

const useCopywriter = () => {
const [copywriter, setCopywriter] = useAtom(atomCopywriter);
const useCopywritings = () => {
const [copywritings, setCopywritings] = useAtom(atomCopywritings);

const updateCopywriter = async () => {
const updateCopywritings = async () => {
try {
const resp = await axios.get<Copywriter>('https://crazy-thursday.shensven.com/latest.json', {
params: {
timestamp: Date.now().toString(),
},
});
if (resp.status === 200) {
if (resp.data.version > copywriter.version) {
setCopywriter(resp.data);
if (resp.data.version > copywritings.version) {
setCopywritings(resp.data);
storage.set('@copywriter', JSON.stringify(resp.data));
}
}
} catch {}
};

return {copywriter, updateCopywriter};
return {copywritings, updateCopywritings};
};

export default useCopywriter;
export default useCopywritings;

0 comments on commit 797b309

Please sign in to comment.