Skip to content

Commit

Permalink
improved loading reference database and show it in dev view
Browse files Browse the repository at this point in the history
  • Loading branch information
repolevedavaj committed May 17, 2022
1 parent ba52d98 commit 211259c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 65 deletions.
115 changes: 65 additions & 50 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
"moment": "2.29.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-native": "0.68.1",
"react-native": "0.68.2",
"react-native-gesture-handler": "2.2.1",
"react-native-reanimated": "2.8.0",
"react-native-safe-area-context": "4.2.4",
"react-native-screens": "3.11.1",
"react-native-svg": "12.3.0",
"react-native-web": "0.17.7",
"sentry-expo": "4.1.1"
"sentry-expo": "4.1.1",
"expo-network": "4.2.0"
},
"devDependencies": {
"@babel/core": "7.17.10",
Expand Down
29 changes: 20 additions & 9 deletions src/services/GrowBuddyPlantsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,39 @@ import {PreferredLocation} from "../model/PreferredLocation";
import {isDefined} from "../common/Utils";
import * as FileSystem from "expo-file-system";
import Fuse from 'fuse.js'
import * as Network from 'expo-network';
import FuseResult = Fuse.FuseResult;

const REMOTE_GROW_BUDDY_PLANTS_URI = "https://raw.githubusercontent.com/Grow-Buddy/grow-buddy-plants/main/database.json";
const LOCAL_GROW_BUDDY_PLANTS_URI = `${FileSystem.documentDirectory}/plants-reference-database.json`
const LOCAL_GROW_BUDDY_PLANTS_URI = `${FileSystem.documentDirectory}/plants-reference-database.json`;

class GrowBuddyPlantsService {

referenceDatabase!: PlantInfo[];
private referenceDatabase: PlantInfo[] = [];

constructor() {
this.initializeDatabase();
this.updateAndLoadDatabase();
}

async initializeDatabase() {
await FileSystem.downloadAsync(REMOTE_GROW_BUDDY_PLANTS_URI, LOCAL_GROW_BUDDY_PLANTS_URI);
const rawReferenceDatabase = await FileSystem.readAsStringAsync(LOCAL_GROW_BUDDY_PLANTS_URI);
this.referenceDatabase = JSON.parse(rawReferenceDatabase);
async updateAndLoadDatabase() {
const networkState = await Network.getNetworkStateAsync();
if (networkState.isInternetReachable) {
await FileSystem.downloadAsync(REMOTE_GROW_BUDDY_PLANTS_URI, LOCAL_GROW_BUDDY_PLANTS_URI, {cache: false});
}

const databaseFileInfo = await FileSystem.getInfoAsync(LOCAL_GROW_BUDDY_PLANTS_URI);
if (databaseFileInfo.exists) {
const rawReferenceDatabase = await FileSystem.readAsStringAsync(LOCAL_GROW_BUDDY_PLANTS_URI);
this.referenceDatabase = JSON.parse(rawReferenceDatabase);
}
}

FileSystem.deleteAsync(LOCAL_GROW_BUDDY_PLANTS_URI);
getDatabaseSize() {
return this.referenceDatabase.length;
}

async searchForProducts(query: string): Promise<Plant[]> {
async search(query: string):
Promise<Plant[]> {
const fuse = new Fuse(this.referenceDatabase!, {keys: ['name', 'botanicalName']})
const result = fuse.search(query)

Expand Down
Loading

0 comments on commit 211259c

Please sign in to comment.