-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppMain.tsx
141 lines (130 loc) · 3.46 KB
/
AppMain.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* eslint-disable react-native/no-inline-styles */
import {
Alert,
Image,
Platform,
SafeAreaView,
Text,
TouchableOpacity,
View,
} from "react-native";
import React, { useEffect, useState } from "react";
import {
ImageTableType,
ImagesForm,
addData,
createTable,
getData,
isExistTable,
} from "./shared/sqlite.common";
import { getLocalData } from "./shared/rnfs.common";
import { convertCsvToJson } from "./shared/csvtojson.common";
import { saveImage } from "./shared/galleryAccess.common";
import {
RewardedAd,
BannerAd,
BannerAdSize,
RewardedAdEventType,
} from "react-native-google-mobile-ads";
const reward_UnitId =
Platform.OS === "ios"
? "ca-app-pub-9711240182969577/8958353614"
: "ca-app-pub-7122371230490146/8716316086";
const AppMain = () => {
const [images, setImages] = useState<ImagesForm[] | []>([]);
const [count, setCount] = useState<number>(0);
useEffect(() => {
const load = async () => {
await getTestCsv();
};
load();
}, []);
const getTestCsv = async () => {
const csvData = await getLocalData("testCsv.csv");
if (!csvData) {
return;
}
const jsonData = (await convertCsvToJson(csvData)) as ImageTableType[];
if (jsonData.length) {
const checkImages = await isExistTable("images");
if (!checkImages) {
await createTable("images", [
{ name: "name", type: "TEXT" },
{ name: "url", type: "TEXT" },
]);
for (const iterator of jsonData) {
await addData(
"images",
["name", "url"],
[iterator.Name, iterator.url]
);
}
} else {
const res = (await getData("images")) as ImagesForm[];
if (res.length) {
setImages(res);
}
}
}
};
const incre_Count = () => {
setCount(count + 1);
if (count === 5) {
reset_Count();
}
};
const reset_Count = () => {
setCount(0);
};
const _onItem = async (url: string) => {
incre_Count();
showRewardAds();
const res = await saveImage(url);
if (res) {
Alert.alert("save image success");
}
};
const showRewardAds = () => {
const rewarded = RewardedAd.createForAdRequest(reward_UnitId);
rewarded.addAdEventListener(RewardedAdEventType.LOADED, () => {
rewarded.show();
});
rewarded.addAdEventListener(RewardedAdEventType.EARNED_REWARD, (reward) => {
console.log("User earned reward of ", reward);
// AsyncStorage.setItem(WebViewScene.dl_count_Key, '0');
});
rewarded.load();
};
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ paddingHorizontal: 20 }}>
{images &&
images.map((e, i) => {
return (
<TouchableOpacity
onPress={() => {
_onItem(e.url);
}}
style={{
padding: 10,
borderRadius: 10,
flexDirection: "row",
borderWidth: 1,
borderColor: "lightgray",
marginTop: 5,
}}
key={i}
>
<Image
source={{ uri: e.url }}
style={{ height: 60, width: 60, resizeMode: "cover" }}
/>
<Text style={{ paddingLeft: 20 }}>{e.name}</Text>
</TouchableOpacity>
);
})}
</View>
</SafeAreaView>
);
};
export default AppMain;