forked from Sefaria/Sefaria-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsPage.js
467 lines (440 loc) · 15.9 KB
/
SettingsPage.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
'use strict';
import PropTypes from 'prop-types';
import React, { useContext, useState, useRef, useEffect } from 'react';
import {
StyleSheet,
Text,
TouchableOpacity,
TouchableWithoutFeedback,
View,
ScrollView,
Alert,
Platform,
PermissionsAndroid,
} from 'react-native';
import VersionNumber from 'react-native-version-number';
import NetInfo from "@react-native-community/netinfo";
import ProgressBar from './ProgressBar';
import {
CategoryColorLine,
ConditionalProgressWrapper,
CloseButton,
ButtonToggleSetNew,
LibraryNavButton,
SefariaProgressBar,
SystemButton,
} from './Misc.js';
import { GlobalStateContext, DispatchContext, STATE_ACTIONS, getTheme } from './StateManager';
import styles from './Styles';
import strings from './LocalizedStrings';
import {
PackagesState,
Tracker as DownloadTracker,
downloadPackage,
checkUpdatesFromServer,
promptLibraryUpdate,
wereBooksDownloaded,
markLibraryForDeletion,
Package,
deleteBooks,
doubleDownload, getLocalBookList,
isDownloadAllowed,
FILE_DIRECTORY, downloadUpdate
} from './DownloadControl';
import Sefaria from "./sefaria";
import {FileSystem} from "react-native-unimodules";
const DEBUG_MODE = false;
/**
*
* @param {array} options
* @param {func} onPress
* @param {array} values. optional list of values that should be passed to onPress if present. should be same length as options
*/
const generateOptions = (options, onPress, values=[]) => Sefaria.util.zip([options, values]).map(([o,v]) => ({
name: o,
text: strings[o],
value: v,
onPress: () => { onPress(typeof v == 'undefined' ? o : v); },
}));
const getIsDisabledObj = () => {
const isDisabledObj = {};
for (let [pkgName, pkgObj] of Object.entries(PackagesState)) {
isDisabledObj[pkgName] = !!pkgObj.supersededByParent;
}
return isDisabledObj;
};
const onPressDisabled = (child, parent) => {
Alert.alert(
strings.alreadyDownloaded,
`${strings.areIncludedIn} "${parent}"`,
[
{text: strings.ok, onPress: () => {}}
]
);
};
let packageDownloadLock = {locked: false};
const usePkgState = () => {
const { interfaceLanguage, downloadNetworkSetting } = useContext(GlobalStateContext);
const [isDisabledObj, setIsDisabledObj] = useState(getIsDisabledObj()); // React Hook
const onPackagePress = async (pkgObj) => {
const onPressActive = async (pkgName) => {
const removePackage = async () => {
DownloadTracker.cancelDownload();
const booksToDelete = await PackagesState[pkgName].unclick();
setIsDisabledObj(getIsDisabledObj());
await deleteBooks(booksToDelete);
};
const addPackage = async () => {
const netState = await NetInfo.fetch();
if (!isDownloadAllowed(netState, downloadNetworkSetting)) {
Alert.alert(
"Download Blocked by Network",
`Current network setting forbids download. Please change settings or connect to internet and try again.`,
[{text: strings.ok}]
);
return
}
await PackagesState[pkgName].markAsClicked();
await DownloadTracker.startDownloadSession(pkgName);
setIsDisabledObj(getIsDisabledObj());
try {
await downloadPackage(pkgName, downloadNetworkSetting);
} catch (e) {
console.log(e)
}
};
if (PackagesState[pkgName].clicked) {
Alert.alert(
strings.delete,
strings.areYouSureDeletePackage,
[{text: strings.yes, onPress: removePackage}, {text: strings.no}]
)
}
else {
if (packageDownloadLock.locked || DownloadTracker.downloadInProgress()) {
doubleDownload();
return
}
packageDownloadLock.locked = true;
try {
await addPackage();
} finally {
packageDownloadLock.locked = false;
}
}
};
const parent = pkgObj.parent;
const shortIntLang = interfaceLanguage.slice(0,2);
//NOTE: onPressDisabled() takes pkgNames in curr intLang while onPress() takes eng
if (pkgObj.supersededByParent) {
onPressDisabled(pkgObj[shortIntLang], PackagesState[parent].jsonData[shortIntLang]); }
else {
await onPressActive(pkgObj.name);
}
};
return {
isDisabledObj,
setIsDisabledObj,
onPackagePress,
};
};
function abstractUpdateChecker(disableUpdateComponent, networkMode) {
async function f() {
if (DownloadTracker.downloadInProgress()) {
doubleDownload();
return
}
disableUpdateComponent(true);
try {
const [totalDownloads, newBooks] = await checkUpdatesFromServer();
if (totalDownloads.length > 0) {
promptLibraryUpdate(totalDownloads, newBooks, networkMode);
}
else {
Alert.alert(
strings.libraryUpToDate,
strings.libraryUpToDateMessage,
[
{text: strings.ok}
])}
} catch (e) {
console.log(e); // todo: proper error handling
}
finally {
disableUpdateComponent(false);
}
}
return f
}
const SettingsPage = ({ close, logout, openUri }) => {
const [numPressesDebug, setNumPressesDebug] = useState(0);
const { themeStr, interfaceLanguage, isLoggedIn, downloadNetworkSetting } = useContext(GlobalStateContext);
const { isDisabledObj, setIsDisabledObj, onPackagePress } = usePkgState();
const theme = getTheme(themeStr);
const [updatesDisabled, setUpdatesDisabled] = useState(false);
const checkUpdatesForSettings = abstractUpdateChecker(setUpdatesDisabled, downloadNetworkSetting);
const deleteLibrary = async () => {
DownloadTracker.cancelDownload(true);
const booksToDelete = await markLibraryForDeletion();
setIsDisabledObj(getIsDisabledObj);
await deleteBooks(booksToDelete);
};
const langStyle = interfaceLanguage === "hebrew" ? styles.heInt : styles.enInt;
return (
<View style={[styles.menu, theme.menu]}>
<CategoryColorLine category={"Other"} />
<View style={[styles.header, theme.header]}>
<CloseButton onPress={close} />
<Text style={[langStyle, styles.settingsHeader, theme.text]}>{strings.settings.toUpperCase()}</Text>
</View>
<ScrollView contentContainerStyle={styles.menuContent}>
<ButtonToggleSection
langStyle={langStyle}
/>
<View style={[styles.readerDisplayOptionsMenuDivider, styles.settingsDivider, theme.readerDisplayOptionsMenuDivider]}/>
{wereBooksDownloaded() ?
<View>
<SystemButton
onPress={checkUpdatesForSettings}
text={updatesDisabled ? strings.checking : strings.checkForUpdates}
isLoading={updatesDisabled}
/>
<SystemButton
onPress={() => {
Alert.alert(
strings.deleteLibrary,
strings.confirmDeleteLibraryMessage,
[{text: strings.yes, onPress: deleteLibrary}, {text: strings.no}]
)
}}
text={strings.deleteLibrary}
/>
{DEBUG_MODE ? <TouchableOpacity style={styles.button} onPress={() => {
console.log('pressed Mess up Library');
getLocalBookList().then(books => {
deleteBooks(books).then(() => console.log('finished messing up library'));
})
}}>
<Text style={[langStyle, styles.buttonText]}>Mess up Library</Text>
</TouchableOpacity> : null}
</View>
: null
}
{
DEBUG_MODE ?
<View>
<TouchableOpacity style={styles.button} onPress={() => FileSystem.getInfoAsync(FILE_DIRECTORY).then(x => console.log(
`${x.filter(f => f.endsWith('zip')).length} files on disk`
))}>
<Text style={[langStyle, styles.buttonText]}>Check Disk</Text>
</TouchableOpacity>
</View>
: null
}
<OfflinePackageList isDisabledObj={isDisabledObj} onPackagePress={onPackagePress} />
<View style={[styles.readerDisplayOptionsMenuDivider, styles.settingsDivider, styles.underOfflinePackages, theme.readerDisplayOptionsMenuDivider]}/>
<TouchableWithoutFeedback onPress={() => {
if (numPressesDebug < 6) { setNumPressesDebug(prev => prev + 1); }
else {
Sefaria.debugNoLibrary = !Sefaria.debugNoLibrary;
setNumPressesDebug(0);
Alert.alert(
'Debug No Offline Library Mode',
`You've just ${Sefaria.debugNoLibrary ? "enabled" : "disabled"} debugging without the offline library. You can change this by tapping 'System' 7 times.`,
[
{text: 'OK', onPress: ()=>{}},
]
);
}
}}>
<Text style={[langStyle, styles.settingsSectionHeader, theme.tertiaryText]}>{strings.system}</Text>
</TouchableWithoutFeedback>
{ isLoggedIn ?
<SystemButton onPress={logout} text={strings.logout} isHeb={interfaceLanguage === "hebrew"} />
: null
}
<SystemButton onPress={()=>{ openUri('https://www.sefaria.org/terms'); }} text={strings.termsAndPrivacy} isHeb={interfaceLanguage === "hebrew"} />
<View style={{marginTop: 10}}>
<Text style={[langStyle, styles.settingsSectionHeader, theme.tertiaryText]}>
{`${strings.appVersion}: ${VersionNumber.appVersion}`}
</Text>
</View>
</ScrollView>
</View>
);
};
SettingsPage.propTypes = {
close: PropTypes.func.isRequired,
logout: PropTypes.func.isRequired,
openUri: PropTypes.func.isRequired,
};
const ButtonToggleSection = ({ langStyle }) => {
const dispatch = useContext(DispatchContext);
const globalState = useContext(GlobalStateContext);
const theme = getTheme(globalState.themeStr);
const setInterfaceLanguage = language => {
dispatch({
type: STATE_ACTIONS.setInterfaceLanguage,
value: language,
time: Sefaria.util.epoch_time(),
});
};
// not synced with web settings
const setTextLanguage = language => {
dispatch({
type: STATE_ACTIONS.setTextLanguage,
value: language,
})
};
const setEmailFrequency = freq => {
dispatch({
type: STATE_ACTIONS.setEmailFrequency,
value: freq,
time: Sefaria.util.epoch_time(),
});
};
const setPreferredCustom = custom => {
dispatch({
type: STATE_ACTIONS.setPreferredCustom,
value: custom,
time: Sefaria.util.epoch_time(),
});
};
const setDownloadNetworkSetting = wifiOnly => {
dispatch({
type: STATE_ACTIONS.setDownloadNetworkSetting,
value: wifiOnly,
});
// changing network settings mid-download is problematic. Best is to drop download and request a new one
if (DownloadTracker.hasEventListener()) {
DownloadTracker.cancelDownload(true);
DownloadTracker.removeEventListener();
downloadUpdate(wifiOnly, false).then(() => {})
}
};
const setReadingHistory = isReadingHistory => {
const dispatcher = () => (
dispatch({
type: STATE_ACTIONS.setReadingHistory,
value: isReadingHistory,
time: Sefaria.util.epoch_time(),
})
);
if (!isReadingHistory && globalState.readingHistory) {
Alert.alert(
strings.delete,
strings.turningThisFeatureOff,
[
{text: strings.cancel, onPress: ()=>{}},
{
text: strings.delete,
onPress: dispatcher,
style: 'destructive',
}
]
);
} else { dispatcher(); }
};
const setGroggerActive = isActive => {
dispatch({
type: STATE_ACTIONS.setGroggerActive,
value: isActive,
});
};
const options = {
interfaceLanguageOptions: generateOptions(['english', 'hebrew'], setInterfaceLanguage),
textLanguageOptions: generateOptions(['english', 'bilingual', 'hebrew'], setTextLanguage),
emailFrequencyOptions: generateOptions(['daily', 'weekly', 'never'], setEmailFrequency),
preferredCustomOptions: generateOptions(['sephardi', 'ashkenazi'], setPreferredCustom),
downloadNetworkSettingOptions: generateOptions(['wifiOnly', 'mobileNetwork'], setDownloadNetworkSetting),
readingHistoryOptions: generateOptions(['onFem', 'offFem'], setReadingHistory, [true, false]),
groggerActiveOptions: generateOptions(['on', 'off'], setGroggerActive),
};
/* stateKey prop is used for testing */
const toggleButtons = ['textLanguage', 'interfaceLanguage', 'emailFrequency', 'readingHistory', 'preferredCustom', 'downloadNetworkSetting'];
if (Sefaria.isGettinToBePurimTime()) { toggleButtons.push('groggerActive'); }
return (
<View>
{toggleButtons.map(s => (
<View style={styles.settingsSection} key={s} stateKey={s}>
<View>
<Text style={[langStyle, styles.settingsSectionHeader, theme.tertiaryText]}>{strings[s]}</Text>
</View>
<ButtonToggleSetNew
options={options[`${s}Options`]}
lang={globalState.interfaceLanguage}
active={globalState[s]} />
</View>))}
</View>
);
};
const OfflinePackageList = ({ isDisabledObj, onPackagePress }) => {
return (
<View style={styles.settingsOfflinePackages}>
{
/*
* Note the usage of Global state here. Having a context manager or reducer (useContext or useReducer) might
* be a better way to handle this data. Or possibly a Prop on a higher level component.
*
* This works because after initialization we change state on ReaderApp and force a re-render. Clicking a package
* works because we change state on a click to PackageComponent. Essentially the changes to packages are caught
* in the re-render. We aren't tuned in to changes to PackagesState or the Package objects directly and are
* relying on peripheral changes in order to render the correct result.
*/
Object.values(PackagesState).sort((a, b) => a.order - b.order).map(p => {
return (
<View key={p.name}>
<PackageComponent onPackagePress={onPackagePress} packageObj={p} isDisabledObj={isDisabledObj}/>
</View>
)
})
}
</View>
);
};
const PackageComponent = ({ packageObj, onPackagePress, isDisabledObj }) => {
const isSelected = packageObj.clicked;
// clicking multiple times in succession causes issues and is likely a mistake. Give a small timeout to prevent double clicks
const [doubleTap, setDoubleTap] = useState(false);
const preventDoubleTap = () => {
setDoubleTap(true);
setTimeout(() => setDoubleTap(false), 1500);
};
const onPress = doubleTap ? () => {} : async () => {
preventDoubleTap();
await onPackagePress(packageObj);
};
return (
<View key={packageObj.name}>
<LibraryNavButton
catColor={Sefaria.palette.categoryColor(packageObj.jsonData.color)}
enText={packageObj.name}
heText={packageObj.jsonData['he']}
count={`${Math.round(packageObj.jsonData['size'] / 1e6)}mb` /* NOTE: iOS uses 1e6 def of mb*/ }
onPress={onPress}
onPressCheckBox={onPress}
checkBoxSelected={0+isSelected}
buttonStyle={{margin: 0, padding: 0, opacity: isDisabledObj[packageObj.en] ? 0.6 : 1.0}}
withArrow={false}
/>
<ConditionalProgressWrapper
conditionMethod={ (state, props) => state && state.downloadNotification === props.packageName}
initialValue={DownloadTracker.getDownloadStatus()}
downloader={DownloadTracker}
listenerName={`PackageComponent${packageObj.name}`}
packageName={packageObj.name}
>
<SefariaProgressBar
download={DownloadTracker}
identity={'SettingsPage'}
downloadSize={packageObj.jsonData.size} />
</ConditionalProgressWrapper>
</View>
)
};
PackageComponent.propTypes = {
packageObj: PropTypes.instanceOf(Package).isRequired,
onPackagePress: PropTypes.func.isRequired,
isDisabledObj: PropTypes.object
};
export default SettingsPage;