Skip to content

Commit

Permalink
chore: making stuff usable again
Browse files Browse the repository at this point in the history
Signed-off-by: sarthakpranesh <[email protected]>
  • Loading branch information
sarthakpranesh committed Dec 9, 2022
1 parent b807847 commit d58b17c
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 76 deletions.
166 changes: 92 additions & 74 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BottomSheet, { BottomSheetScrollView } from "@gorhom/bottom-sheet";
import analytics from "@react-native-firebase/analytics";
import remoteConfig from "@react-native-firebase/remote-config";
import { useState, useEffect, useCallback, useRef, useMemo } from "react";
import {
StyleSheet,
Expand Down Expand Up @@ -55,6 +56,9 @@ export default function App() {
useEffect(() => {
(async () => {
await Promise.all([
remoteConfig().setDefaults({
server: "[]",
}),
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA, {
title: "Permission to use camera",
message: "We need your permission to use your camera",
Expand All @@ -63,6 +67,7 @@ export default function App() {
buttonNeutral: "Ask Me Later",
}),
]);
await remoteConfig().fetchAndActivate();
setAppIsReady(true);
await analytics().logEvent("app_open", {
time: Date.now(),
Expand Down Expand Up @@ -111,87 +116,99 @@ export default function App() {
"Looks like something bad happened, please try again!"
);
}
const details = await getPlantDetails(predictPayload.predictions[0].name);
// GyanIsDead
// const details = await getPlantDetails(predictPayload.predictions[0].name);
// setDetails({
// ...details,
// loaded: true,
// });
setDetails({
...details,
images: [],
description: "",
link: "",
loaded: true,
});
} catch (err: any) {
console.log(err.message);
}
};

const renderImages = () => {
return (
<>
<H3 text="Plant Images" />
{details.images.length === 0 ? (
<Paragraph text="Loading..." />
) : (
<FlatList
style={{
borderRadius: 8,
backgroundColor: "#F9F9F9",
marginBottom: 10,
}}
horizontal
showsHorizontalScrollIndicator={false}
data={details.images}
keyExtractor={(_, i) => `${i}`}
renderItem={({ item }) => {
return (
<Image
style={{
width: 100,
height: 160,
resizeMode: "cover",
margin: 4,
borderRadius: 8,
}}
source={{ uri: item }}
/>
);
}}
/>
)}
</>
);
};
// GyanIsDead
// const renderImages = () => {
// return (
// <>
// <H3 text="Plant Images" />
// {!details.loaded ? (
// <Paragraph text="Loading..." />
// ) : (
// <FlatList
// style={{
// borderRadius: 8,
// backgroundColor: "#F9F9F9",
// marginBottom: 10,
// }}
// horizontal
// showsHorizontalScrollIndicator={false}
// data={details.images}
// keyExtractor={(_, i) => `${i}`}
// renderItem={({ item }) => {
// return (
// <Image
// style={{
// width: 100,
// height: 160,
// resizeMode: "cover",
// margin: 4,
// borderRadius: 8,
// }}
// source={{ uri: item }}
// />
// );
// }}
// ListEmptyComponent={
// <Paragraph text="Unable to extract images from net!" />
// }
// />
// )}
// </>
// );
// };

const renderWiki = () => {
return (
<>
<H3 text="Description" />
{!details.loaded ? (
<Paragraph text="Loading..." />
) : (
<>
<Paragraph
text={
details.description.length === 0
? "Unable to extract details from Wikipedia!"
: details.description
}
/>
{details.link.length === 0 ? null : (
<TouchableOpacity
onPress={() => {
if (details.link !== "") {
Linking.openURL(details.link);
}
}}
>
<H3
style={{ color: "blue", marginTop: 0 }}
text="Open in Wikipedia"
/>
</TouchableOpacity>
)}
</>
)}
</>
);
};
// GyanIsDead
// const renderWiki = () => {
// return (
// <>
// <H3 text="Description" />
// {!details.loaded ? (
// <Paragraph text="Loading..." />
// ) : (
// <>
// <Paragraph
// text={
// details.description.length === 0
// ? "Unable to extract details from Wikipedia!"
// : details.description
// }
// />
// {details.link.length === 0 ? null : (
// <TouchableOpacity
// onPress={() => {
// if (details.link !== "") {
// Linking.openURL(details.link);
// }
// }}
// >
// <H3
// style={{ color: "blue", marginTop: 0 }}
// text="Open in Wikipedia"
// />
// </TouchableOpacity>
// )}
// </>
// )}
// </>
// );
// };

const renderOtherPrediction = () => {
if (allPredicted.length <= 1) {
Expand Down Expand Up @@ -245,8 +262,9 @@ export default function App() {
<Paragraph
text={`Accuracy: ${floatToPercentage(allPredicted[0].score)}`}
/>
{renderImages()}
{renderWiki()}
{/* GyanIsDead */}
{/* {renderImages()}
{renderWiki()} */}
{renderOtherPrediction()}
</View>
)}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@gorhom/bottom-sheet": "^4",
"@react-native-firebase/analytics": "^15.4.0",
"@react-native-firebase/app": "^15.4.0",
"@react-native-firebase/remote-config": "^16.4.6",
"cross-fetch": "^3.1.5",
"deprecated-react-native-prop-types": "^2.3.0",
"patch-package": "^6.4.7",
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<br />

### WARNING
The application in the current state is non functional as Heroku shutdown all their free services. I haven't yet found time to host the service elsewhere and update the code base.
Not all services used by this app are running. Heroku has phased out there free tier and Gyan API, one of my side projects was hosted on it which is no longer up and running. You'll find code in the app commented with `GyanIsDead` tag, in future if I host the service some place else then I'll add it back to the project.

<br />

Expand Down
41 changes: 40 additions & 1 deletion services/plantRecog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import analytics from "@react-native-firebase/analytics";
import remoteConfig from "@react-native-firebase/remote-config";
import fetch from "cross-fetch";

/*
Expand All @@ -9,12 +10,50 @@ import fetch from "cross-fetch";
* to aid the applications.
*/

const baseUrl = "https://plantrecog.up.railway.app/";
const server = {
default: true,
base: "https://plantrecog.up.railway.app/",
};

// Base selector
const baseSelection = async () => {
if (!server.default) {
return server.base;
}
const serverRaw = remoteConfig().getValue("server");
if (serverRaw.getSource() !== "remote") {
await remoteConfig().fetch(3000);
}
const serverArray = JSON.parse(serverRaw.asString());
for (const serv of serverArray) {
try {
const rawResp = await fetch(serv);
const resp = await rawResp.json();
if (resp?.message === "Success") {
server.base = serv;
server.default = false;
break;
}
} catch (err) {
console.log(err);
}
}
if (server.default) {
console.log(`Service->PlantRecog->baseSelection->unable to select server`);
await analytics().logEvent("unable_to_select_base", {
base: server.base,
came: JSON.stringify(serverArray),
});
}
return server.base;
};

// Global request builder to be used in all requests
const FetchBuilder = (route: string, conf: RequestInit | undefined) => {
return new Promise(async (resolve, reject) => {
try {
const baseUrl = await baseSelection();
console.log(`Service->PlantRecog->selected base url:`, baseUrl);
const rawResp = await fetch(baseUrl + route, conf);
const resp = await rawResp.json();
console.log(`Service->PlantRecog->${route} response:`, resp.message);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,11 @@
opencollective-postinstall "^2.0.1"
superstruct "^0.6.2"

"@react-native-firebase/remote-config@^16.4.6":
version "16.4.6"
resolved "https://registry.yarnpkg.com/@react-native-firebase/remote-config/-/remote-config-16.4.6.tgz#dec215f2448f555cdba893a31f5cdf419b47b33e"
integrity sha512-2KPUao9xby+gp+JQUmikx9N0zcCLb0+6GkgI8//sYJ6Z3EaI53kx5kJHJDgYqdjF/zFjv3rm+yhm5LAgARPMHA==

"@react-native/[email protected]":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@react-native/assets/-/assets-1.0.0.tgz#c6f9bf63d274bafc8e970628de24986b30a55c8e"
Expand Down

0 comments on commit d58b17c

Please sign in to comment.