forked from inaturalist/SeekReactNative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonNamesHelpers.ts
132 lines (123 loc) · 6.23 KB
/
commonNamesHelpers.ts
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
import { getVersion } from "react-native-device-info";
import { setDisplayLanguage, localeNoHyphens } from "./languageHelpers";
import { capitalizeNames } from "./helpers";
import i18n from "../i18n";
const Realm = require( "realm" );
const realmConfig = require( "../models/index" );
const getTaxonCommonName = ( taxonID?: number ) => (
new Promise<string | null | void>( ( resolve ) => {
if ( !taxonID ) { return; }
Realm.open( realmConfig.default ).then( ( realm ) => {
// need this because realm isn't guaranteed to only contain
// one locale; we could solve this by deleting the realm database each time
// the app loads or including this searchLocale parameter
const searchLocale = localeNoHyphens( ( i18n.locale ) );
// look up common names for predicted taxon in the current locale
const commonNames = realm.objects( "CommonNamesRealm" )
.filtered( `taxon_id == ${taxonID} and locale == '${searchLocale}'` );
resolve( commonNames.length > 0 ? capitalizeNames( commonNames[0].name ) : null );
} ).catch( ( err: Error ) => {
console.log( "[DEBUG] Failed to open realm, error: ", err );
resolve( );
} );
} )
);
const addCommonNamesFromFile = ( realm, commonNamesDict: { i: number; l: string; n: string; }[], seekLocale: string ) => {
commonNamesDict.forEach( ( commonNameRow ) => {
if ( commonNameRow.l === seekLocale ) {
// only create realm objects if language matches current locale
realm.create( "CommonNamesRealm", {
taxon_id: commonNameRow.i,
locale: commonNameRow.l,
name: commonNameRow.n
}, true );
}
} );
};
const setupCommonNames = ( preferredLanguage: string ) => {
Realm.open( realmConfig.default )
.then( ( realm ) => {
realm.write( () => {
const locale = setDisplayLanguage( preferredLanguage );
// need to remove hyphens for pt-BR and es-MX
const seekLocale = localeNoHyphens( locale );
const realmLocale = realm.objects( "CommonNamesRealm" ).filtered( `locale == "${seekLocale}"` );
const userSettings = realm.objects( "UserSettingsRealm" )[0];
const prevAppVersion = userSettings?.appVersion;
const currentAppVersion = getVersion( );
// only reload common names when the app version changes or when the
// user's desired locale changes, not on each app launch
if ( prevAppVersion === currentAppVersion && realmLocale.length > 0 ) {
return;
} else {
userSettings.appVersion = currentAppVersion;
}
// otherwise, delete all existing common names from Realm and update with preferred language
realm.delete( realm.objects( "CommonNamesRealm" ) );
// load names from each file. React-native requires need to be strings
// so each file is listed here instead of some kind of loop
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-0" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-1" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-2" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-3" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-4" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-5" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-6" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-7" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-8" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-9" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-10" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-11" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-12" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-13" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-14" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-15" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-16" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-17" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-18" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-19" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-20" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-21" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-22" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-23" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-24" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-25" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-26" ).default, seekLocale );
addCommonNamesFromFile( realm,
require( "./commonNames/commonNamesDict-27" ).default, seekLocale );
} );
} ).catch( ( err: Error ) => {
console.log( "[DEBUG] Failed to setup common names: ", err );
} );
};
export {
getTaxonCommonName,
setupCommonNames
};