This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
/
Settings.js
336 lines (288 loc) · 11.2 KB
/
Settings.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
import React, { Component } from 'react';
import {ScrollView, View, Alert, Keyboard, Platform} from 'react-native';
import { SafeAreaView } from 'react-navigation';
import Sync from '@SFJS/syncManager'
import ModelManager from '@SFJS/modelManager'
import AlertManager from '@SFJS/alertManager'
import SF from '@SFJS/sfjs'
import Auth from '@SFJS/authManager'
import Storage from "@SFJS/storageManager"
import KeysManager from '@Lib/keysManager'
import OptionsState from "@Lib/OptionsState"
import ApplicationState from "@Lib/ApplicationState"
import StyleKit from "@Style/StyleKit"
import SectionHeader from "@Components/SectionHeader";
import TableSection from "@Components/TableSection";
import SectionedAccessoryTableCell from "@Components/SectionedAccessoryTableCell";
import Abstract from "@Screens/Abstract"
import LockedView from "@Containers/LockedView";
import AuthSection from "@Screens/Settings/Sections/AuthSection"
import OptionsSection from "@Screens/Settings/Sections/OptionsSection"
import PasscodeSection from "@Screens/Settings/Sections/PasscodeSection"
import EncryptionSection from "@Screens/Settings/Sections/EncryptionSection"
import CompanySection from "@Screens/Settings/Sections/CompanySection"
export default class Settings extends Abstract {
static navigationOptions = ({ navigation, navigationOptions }) => {
let templateOptions = {
title: "Settings",
leftButton: {
title: ApplicationState.isIOS ? "Done" : null,
iconName: ApplicationState.isIOS ? null : StyleKit.nameForIcon("checkmark"),
}
}
return Abstract.getDefaultNavigationOptions({navigation, navigationOptions, templateOptions});
};
constructor(props) {
super(props);
props.navigation.setParams({
leftButton: {
title: ApplicationState.isIOS ? "Done" : null,
iconName: ApplicationState.isIOS ? null : StyleKit.nameForIcon("checkmark"),
onPress: () => {
this.dismiss();
}
}
})
if(__DEV__) {
props.navigation.setParams({
rightButton: {
title: "Destroy Data",
onPress: () => {
Storage.get().clear();
Auth.get().signout();
KeysManager.get().clearOfflineKeysAndData(true);
}
}
})
}
this.sortOptions = [
{key: "created_at", label: "Date Added"},
{key: "client_updated_at", label: "Date Modified"},
{key: "title", label: "Title"},
];
this.options = ApplicationState.getOptions();
this.constructState({});
}
loadInitialState() {
super.loadInitialState();
this.loadSecurityStatus();
}
loadSecurityStatus() {
var hasPasscode = KeysManager.get().hasOfflinePasscode();
var hasBiometrics = KeysManager.get().hasBiometrics();
var encryptedStorage = KeysManager.get().isStorageEncryptionEnabled();
this.mergeState({hasPasscode: hasPasscode, hasBiometrics: hasBiometrics, storageEncryption: encryptedStorage})
}
componentWillUnmount() {
super.componentWillUnmount();
Sync.get().removeEventHandler(this.syncEventHandler);
}
componentDidFocus() {
super.componentDidFocus();
this.loadSecurityStatus();
this.forceUpdate();
}
resaveOfflineData(callback, updateAfter = false) {
Sync.get().resaveOfflineData().then(() => {
if(updateAfter) {
this.forceUpdate();
}
callback && callback();
});
}
onSignOutPress = () => {
AlertManager.get().confirm({
title: "Sign Out?",
text: "Signing out will remove all data from this device, including notes and tags. Make sure your data is synced before proceeding.",
confirmButtonText: "Sign Out",
onConfirm: () => {
Auth.get().signout().then(() => {
this.forceUpdate();
})
}
})
}
onStorageEncryptionEnable = () => {
AlertManager.get().confirm({
title: "Enable Storage Encryption?",
text: "Storage encryption improves your security by encrypting your data on your device. It may increase app start-up time.",
confirmButtonText: "Enable",
onConfirm: () => {
this.mergeState({storageEncryptionLoading: true});
KeysManager.get().enableStorageEncryption();
this.resaveOfflineData(() => {
this.mergeState({storageEncryption: true, storageEncryptionLoading: false});
});
}
})
}
onStorageEncryptionDisable = () => {
AlertManager.get().confirm({
title: "Disable Storage Encryption?",
text: "Storage encryption improves your security by encrypting your data on your device. Disabling it can improve app start-up speed.",
confirmButtonText: "Disable",
onConfirm: () => {
this.mergeState({storageEncryptionLoading: true});
KeysManager.get().disableStorageEncryption();
this.resaveOfflineData(() => {
this.mergeState({storageEncryption: false, storageEncryptionLoading: false});
});
}
})
}
onPasscodeEnable = () => {
this.props.navigation.navigate("InputModal", {
title: "Setup Passcode",
placeholder: "Enter a passcode",
confirmPlaceholder: "Confirm passcode",
secureTextEntry: true,
requireConfirm: true,
showKeyboardChooser: true,
onSubmit: async (value, keyboardType) => {
Storage.get().setItem("passcodeKeyboardType", keyboardType);
let identifier = await SF.get().crypto.generateUUID();
SF.get().crypto.generateInitialKeysAndAuthParamsForUser(identifier, value).then((results) => {
let keys = results.keys;
let authParams = results.authParams;
// make sure it has valid items
if(_.keys(keys).length > 0) {
KeysManager.get().setOfflineAuthParams(authParams);
KeysManager.get().persistOfflineKeys(keys);
var encryptionSource = KeysManager.get().encryptionSource();
if(encryptionSource == "offline") {
this.resaveOfflineData(null, true);
}
} else {
this.mergeState({setupButtonText: SAVE_BUTTON_DEFAULT_TEXT, setupButtonEnabled: true});
Alert.alert("Passcode Error", "There was an error setting up your passcode. Please try again.");
}
});
}
});
}
onPasscodeDisable = () => {
this.handlePrivilegedAction(true, SFPrivilegesManager.ActionManagePasscode, () => {
var encryptionSource = KeysManager.get().encryptionSource();
var message;
if(encryptionSource == "account") {
message = "Are you sure you want to disable your local passcode? This will not affect your encryption status, as your data is currently being encrypted through your sync account keys.";
} else if(encryptionSource == "offline") {
message = "Are you sure you want to disable your local passcode? This will disable encryption on your data.";
}
AlertManager.get().confirm({
title: "Disable Passcode",
text: message,
confirmButtonText: "Disable Passcode",
onConfirm: async () => {
var result = await KeysManager.get().clearOfflineKeysAndData();
if(encryptionSource == "offline") {
// remove encryption from all items
this.resaveOfflineData(null, true);
}
this.mergeState({hasPasscode: false});
this.forceUpdate();
}
})
})
}
onFingerprintEnable = () => {
KeysManager.get().enableBiometrics();
this.loadSecurityStatus();
}
onFingerprintDisable = () => {
this.handlePrivilegedAction(true, SFPrivilegesManager.ActionManagePasscode, () => {
KeysManager.get().disableBiometrics();
this.loadSecurityStatus();
});
}
onSortChange = (key) => {
this.options.setSortBy(key);
this.forceUpdate();
}
onOptionSelect = (option) => {
this.options.setDisplayOptionKeyValue(option, !this.options.getDisplayOptionValue(option));
this.forceUpdate();
}
toggleSortReverse = () => {
this.options.setSortReverse(!this.options.sortReverse);
this.forceUpdate();
}
openManagePrivs = () => {
this.handlePrivilegedAction(true, SFPrivilegesManager.ActionManagePrivileges, () => {
this.props.navigation.navigate("ManagePrivileges")
})
}
render() {
if(this.state.lockContent) {
return (<LockedView />);
}
let signedIn = !Auth.get().offline();
return (
<SafeAreaView forceInset={{ bottom: 'never'}} style={[StyleKit.styles.container, StyleKit.styles.baseBackground]}>
<ScrollView style={{backgroundColor: StyleKit.variable("stylekitBackgroundColor")}} keyboardShouldPersistTaps={'always'} keyboardDismissMode={'interactive'}>
{!signedIn && !this.state.confirmRegistration &&
<AuthSection
title={"Account"}
onAuthSuccess = {() => {this.dismiss()}}
/>
}
<OptionsSection
title={"Options"}
onSignOutPress={this.onSignOutPress}
onManagePrivileges={this.openManagePrivs}
/>
<TableSection>
<SectionHeader title={"Sort Notes By"} buttonText={this.options.sortReverse ? "Disable Reverse Sort" : "Enable Reverse Sort"} buttonAction={this.toggleSortReverse} />
{this.sortOptions.map((option, i) => {
return (
<SectionedAccessoryTableCell
onPress={() => {this.onSortChange(option.key)}}
text={option.label}
key={option.key}
first={i == 0}
last={i == this.sortOptions.length - 1}
selected={() => {return option.key == this.options.sortBy}}
/>
)
})}
</TableSection>
<TableSection>
<SectionHeader title={"Note List Options"} />
<SectionedAccessoryTableCell
onPress={() => {this.onOptionSelect('hidePreviews')}}
text={"Hide note previews"}
first={true}
selected={() => {return this.options.hidePreviews}}
/>
<SectionedAccessoryTableCell
onPress={() => {this.onOptionSelect('hideTags')}}
text={"Hide note tags"}
selected={() => {return this.options.hideTags}}
/>
<SectionedAccessoryTableCell
onPress={() => {this.onOptionSelect('hideDates')}}
text={"Hide note dates"}
last={true}
selected={() => {return this.options.hideDates}}
/>
</TableSection>
<PasscodeSection
hasPasscode={this.state.hasPasscode}
hasBiometrics={this.state.hasBiometrics}
storageEncryption={this.state.storageEncryption}
storageEncryptionLoading={this.state.storageEncryptionLoading}
onStorageEncryptionEnable={this.onStorageEncryptionEnable}
onStorageEncryptionDisable={this.onStorageEncryptionDisable}
onEnable={this.onPasscodeEnable}
onDisable={this.onPasscodeDisable}
onFingerprintEnable={this.onFingerprintEnable}
onFingerprintDisable={this.onFingerprintDisable}
title={"Security"}
/>
<EncryptionSection title={"Encryption Status"}/>
<CompanySection title={"Standard Notes"}/>
</ScrollView>
</SafeAreaView>
);
}
}