Skip to content

Commit d8c16eb

Browse files
authored
Merge pull request #3 from kelvinthh/dev
Dev
2 parents 0d8dfa8 + f99dddb commit d8c16eb

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ This is a mobile version of the [Image Generation Next.js](https://github.com/ke
2121
- Azure Functions for back-end hosting
2222
- Azure Storage for storing images
2323

24+
## Third-Party Libraries Used 📚
25+
- [Axios](https://axios-http.com/)
26+
- [react-native-toast-message](https://github.com/calintamas/react-native-toast-message)
27+
- [NativeWind](https://github.com/marklawlor/nativewind)
28+
- [Tailwind CSS](https://tailwindcss.com/)
29+
- [React Native Async Storage](https://github.com/react-native-async-storage/async-storage)
30+
- [react-native-dotenv](https://github.com/goatandsheep/react-native-dotenv)
31+
- [Recoil](https://recoiljs.org/)
32+
2433
## Getting Started 🚀
2534

2635
1. Make sure you have the back-end set up, this includes the Azure Functions and Storages from the [main Next.js project](https://github.com/kelvinthh/Image-Generation-Next.js).

ios/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ PODS:
447447
- React-jsi (= 0.71.6)
448448
- React-logger (= 0.71.6)
449449
- React-perflogger (= 0.71.6)
450+
- RNCAsyncStorage (1.18.1):
451+
- React-Core
450452
- Yoga (1.14.0)
451453

452454
DEPENDENCIES:
@@ -501,6 +503,7 @@ DEPENDENCIES:
501503
- React-RCTVibration (from `../node_modules/react-native/Libraries/Vibration`)
502504
- React-runtimeexecutor (from `../node_modules/react-native/ReactCommon/runtimeexecutor`)
503505
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
506+
- "RNCAsyncStorage (from `../node_modules/@react-native-async-storage/async-storage`)"
504507
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
505508

506509
SPEC REPOS:
@@ -607,6 +610,8 @@ EXTERNAL SOURCES:
607610
:path: "../node_modules/react-native/ReactCommon/runtimeexecutor"
608611
ReactCommon:
609612
:path: "../node_modules/react-native/ReactCommon"
613+
RNCAsyncStorage:
614+
:path: "../node_modules/@react-native-async-storage/async-storage"
610615
Yoga:
611616
:path: "../node_modules/react-native/ReactCommon/yoga"
612617

@@ -662,6 +667,7 @@ SPEC CHECKSUMS:
662667
React-RCTVibration: 73d201599a64ea14b4e0b8f91b64970979fd92e6
663668
React-runtimeexecutor: 8692ac548bec648fa121980ccb4304afd136d584
664669
ReactCommon: 0c43eaeaaee231d7d8dc24fc5a6e4cf2b75bf196
670+
RNCAsyncStorage: b90b71f45b8b97be43bc4284e71a6af48ac9f547
665671
Yoga: ba09b6b11e6139e3df8229238aa794205ca6a02a
666672

667673
PODFILE CHECKSUM: ebe432ad56513222ebfc09b56e491909e15bf652

package-lock.json

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"web": "expo start --web"
99
},
1010
"dependencies": {
11+
"@react-native-async-storage/async-storage": "^1.18.1",
1112
"@react-native-community/netinfo": "^9.3.9",
1213
"axios": "^1.3.5",
1314
"expo": "~48.0.10",

src/components/Body.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { fetchImages, fetchSuggestion, generateImage } from "../fetchData";
1818
import ImageItem from "../components/ImageItem";
1919
import { Toast } from "react-native-toast-message/lib/src/Toast";
2020
import internetState from "../state/internetState";
21+
import AsyncStorage from "@react-native-async-storage/async-storage";
2122

2223
const Body = () => {
2324
const [images, setImages] = useRecoilState(imagesState);
@@ -32,8 +33,10 @@ const Body = () => {
3233
const flatListRef = useRef<FlatList<ImageUrl>>(null);
3334
const isFirstRender = useRef(true);
3435

35-
useEffect(() => {
36+
// AsyncStorage key
37+
const LAST_GENERATED_TIME_KEY = "lastGeneratedTime";
3638

39+
useEffect(() => {
3740
// Prevent the toast pop up on the first render
3841
if (isFirstRender.current) {
3942
isFirstRender.current = false;
@@ -50,7 +53,35 @@ const Body = () => {
5053
}
5154
}, [hasInternet]);
5255

56+
async function setLastGeneratedTime() {
57+
const currentTime = new Date().getTime();
58+
await AsyncStorage.setItem(
59+
LAST_GENERATED_TIME_KEY,
60+
JSON.stringify(currentTime)
61+
);
62+
}
63+
64+
async function getLastGeneratedTime() {
65+
const lastGeneratedTime = await AsyncStorage.getItem(
66+
LAST_GENERATED_TIME_KEY
67+
);
68+
return lastGeneratedTime ? JSON.parse(lastGeneratedTime) : null;
69+
}
70+
5371
const handleSubmit = async (prompt: string) => {
72+
const lastGeneratedTime = await getLastGeneratedTime();
73+
const currentTime = new Date().getTime();
74+
75+
if (lastGeneratedTime && currentTime - lastGeneratedTime < 10000) {
76+
Toast.show({
77+
type: "info",
78+
text1: "Please wait",
79+
text2: "You can generate an image every 10 seconds.",
80+
position: "bottom",
81+
});
82+
return;
83+
}
84+
5485
setGenerating(true);
5586
console.log("Submitting prompt: ", prompt);
5687
Toast.show({
@@ -79,6 +110,8 @@ const Body = () => {
79110
text2: "Your image has been generated!",
80111
position: "bottom",
81112
});
113+
114+
await setLastGeneratedTime();
82115
await handleRefreshImage();
83116

84117
// Scroll the FlatList to the top
@@ -178,7 +211,7 @@ const Body = () => {
178211
>
179212
<Text className="text-white">Use suggestion!</Text>
180213
</TouchableOpacity>
181-
{(!hasInternet && !isFirstRender.current) && (
214+
{!hasInternet && !isFirstRender.current && (
182215
<Text className="w-full my-1 text-red-500 font-light italic">
183216
⛔️ Error: No internet connection.
184217
</Text>

0 commit comments

Comments
 (0)