-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathFolderPDFSyncSettings.svelte
More file actions
400 lines (391 loc) · 17.2 KB
/
FolderPDFSyncSettings.svelte
File metadata and controls
400 lines (391 loc) · 17.2 KB
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
<script lang="ts">
import { CheckBox } from '@nativescript-community/ui-checkbox';
import { Label } from '@nativescript-community/ui-label';
import { prompt } from '@nativescript-community/ui-material-dialogs';
import { TextField, TextFieldProperties } from '@nativescript-community/ui-material-textfield';
import { ApplicationSettings, Color, ObservableArray, View } from '@nativescript/core';
import { showError } from '@shared/utils/showError';
import { closeModal } from '@shared/utils/svelte/ui';
import { Template } from '@nativescript-community/svelte-native/components';
import { get, writable } from 'svelte/store';
import { l, lc } from '~/helpers/locale';
import { getPDFDefaultExportOptions } from '~/services/pdf/PDFCanvas';
import { LocalFolderPDFSyncServiceOptions } from '~/services/sync/LocalFolderPDFSyncService';
import { SERVICES_SYNC_COLOR } from '~/services/sync/types';
import { ALERT_OPTION_MAX_HEIGHT, FILENAME_DATE_FORMAT, FILENAME_USE_DOCUMENT_NAME, SETTINGS_FILE_NAME_FORMAT, SETTINGS_FILE_NAME_USE_DOCUMENT_NAME } from '~/utils/constants';
import { checkOrDownloadOCRLanguages, createView, getNameFormatHTMLArgs, openLink, pickColor, requestNotificationPermission, showAlertOptionSelect, showSliderPopover } from '~/utils/ui';
import { colors, windowInset } from '~/variables';
import CActionBar from '../common/CActionBar.svelte';
import FolderTextView from '../common/FolderTextView.svelte';
import ListItemAutoSize from '../common/ListItemAutoSize.svelte';
import OcrSettingsBottomSheet from '../ocr/OCRSettingsBottomSheet.svelte';
import PdfSyncSettingsView from './PDFSyncSettingsView.svelte';
import { requestExactAlarmPermission } from '@nativescript-community/perms';
import { checkAlarmPermission } from '~/services/sync/BaseSyncService';
// technique for only specific properties to get updated on store change
$: ({ colorOnSurfaceVariant, colorOutline, colorPrimary } = $colors);
const tMargin = '4 10 4 10';
const pdfExportSettings = getPDFDefaultExportOptions();
export let data: LocalFolderPDFSyncServiceOptions = {} as any;
const store = writable(
Object.assign(
{
exportOptions: pdfExportSettings,
autoSync: false,
enabled: true,
OCREnabled: false,
useFoldersStructure: false,
OCRDataType: 'best',
OCRLanguages: [],
fileNameFormat: ApplicationSettings.getString(SETTINGS_FILE_NAME_FORMAT, FILENAME_DATE_FORMAT),
useDocumentName: ApplicationSettings.getBoolean(SETTINGS_FILE_NAME_USE_DOCUMENT_NAME, FILENAME_USE_DOCUMENT_NAME),
color: SERVICES_SYNC_COLOR['folder_pdf'] as string | Color
},
data
)
);
DEV_LOG && console.log('FolderImageSyncSettings', JSON.stringify(data), JSON.stringify(get(store)));
// let folderPathName = data.folderPathName;
const variant = 'outline';
async function save() {
const result = get(store);
DEV_LOG && console.log('save', JSON.stringify(result));
if (result.localFolderPath) {
if (__ANDROID__) {
await requestNotificationPermission();
}
if (result.OCREnabled && result.OCRLanguages.length) {
await checkOrDownloadOCRLanguages({
dataType: result.OCRDataType,
languages: result.OCRLanguages,
shouldConfirm: false
});
}
closeModal(result);
} else {
showError(lc('missing_export_folder'), { showAsSnack: true });
}
}
const items = new ObservableArray([
{
type: 'color'
},
{
type: 'switch',
id: 'enabled',
title: lc('enabled'),
value: $store.enabled
},
{
id: 'selectFolder',
type: 'selectFolder',
text: $store.localFolderPath
},
{
type: 'switch',
id: 'autoSync',
title: lc('auto_sync'),
description: lc('local_auto_sync_desc'),
value: $store.autoSync
},
{
id: 'setting',
key: 'syncThrottleSeconds',
title: lc('sync_throttle_seconds'),
description: lc('sync_throttle_desc'),
valueType: 'number',
type: 'prompt',
textFieldProperties: {
keyboardType: 'number',
autocapitalizationType: 'none'
} as TextFieldProperties,
rightValue: () => $store.syncThrottleSeconds || 0,
default: 0,
validate: checkAlarmPermission
},
{
type: 'switch',
id: 'useDocumentName',
title: lc('filename_use_document_name'),
value: $store.useDocumentName
},
{
id: 'setting',
key: 'fileNameFormat',
useHTML: true,
title: lc('filename_date_format'),
description: lc('filename_date_format_desc'),
full_description: lc('filename_date_format_fulldesc', ...getNameFormatHTMLArgs()),
onLinkTap: ({ link }) => {
openLink(link);
},
valueType: 'string',
textFieldProperties: {
autocapitalizationType: 'none',
autocorrect: false
} as TextFieldProperties,
rightValue: () => $store.fileNameFormat,
type: 'prompt'
},
{
type: 'switch',
id: 'useFoldersStructure',
title: lc('use_folder_structure'),
description: lc('use_folder_structure_desc'),
value: $store.useFoldersStructure
},
{
type: 'sectionheader',
title: lc('pdf_settings')
},
{
type: 'pdfoptions'
},
{
type: 'sectionheader',
title: lc('ocr_settings')
},
{
type: 'switch',
id: 'OCREnabled',
title: lc('ocr_enabled'),
value: $store.OCREnabled
},
{
type: 'ocroptions'
}
]);
function getTitle(item) {
return item.title;
}
function getDescription(item) {
return typeof item.description === 'function' ? item.description(item) : item.description;
}
function updateItem(item, key = 'id') {
const index = items.findIndex((it) => it[key] === item[key]);
if (index !== -1) {
items.setItem(index, item);
}
}
let checkboxTapTimer;
function clearCheckboxTimer() {
if (checkboxTapTimer) {
clearTimeout(checkboxTapTimer);
checkboxTapTimer = null;
}
}
let ignoreNextOnCheckBoxChange = false;
async function onCheckBox(item, event, pdfOption?: string) {
if (ignoreNextOnCheckBoxChange || item.value === event.value) {
return;
}
const value = event.value;
item.value = value;
clearCheckboxTimer();
try {
ignoreNextOnCheckBoxChange = true;
if (pdfOption) {
$store.exportOptions[pdfOption] = value;
} else {
$store[item.key || item.id] = value;
}
} catch (error) {
showError(error);
} finally {
ignoreNextOnCheckBoxChange = false;
}
}
async function onTap(item, event) {
try {
if (item.type === 'checkbox' || item.type === 'switch') {
// we dont want duplicate events so let s timeout and see if we clicking diretly on the checkbox
const checkboxView: CheckBox = ((event.object as View).parent as View).getViewById('checkbox');
clearCheckboxTimer();
checkboxTapTimer = setTimeout(() => {
checkboxView.checked = !checkboxView.checked;
}, 10);
return;
}
switch (item.id) {
case 'store_setting':
case 'setting': {
if (item.type === 'prompt') {
const result = await prompt({
title: getTitle(item),
message: item.useHTML ? item.description : item.full_description || item.description,
okButtonText: l('save'),
cancelButtonText: l('cancel'),
autoFocus: true,
textFieldProperties: item.textFieldProperties,
defaultText: (typeof item.rightValue === 'function' ? item.rightValue() : item.default) + '',
view: item.useHTML
? createView(
Label,
{
padding: '10 20 0 20',
textWrap: true,
color: colorOnSurfaceVariant as any,
html: item.full_description || item.description
},
item.onLinkTap
? {
linkTap: item.onLinkTap
}
: undefined
)
: undefined
});
DEV_LOG && console.log('prompt result', item.key, item.valueType, result);
if (result && !!result.result && result.text.length > 0) {
const newValue = item.valueType === 'string' ? result.text : parseInt(result.text, 10);
if (item.validate) {
const validated = await item.validate(newValue);
if (!validated) {
return;
}
}
$store[item.key] = newValue;
updateItem(item);
}
} else if (item.type === 'slider') {
DEV_LOG && console.log('showSlidersPopover', event.object, item.currentValue());
await showSliderPopover({
anchor: event.object,
value: item.currentValue(),
...item,
onChange(value) {
if (item.transformValue) {
value = item.transformValue(value, item);
} else {
value = Math.round(value / item.step) * item.step;
}
if (item.valueType === 'string') {
$store[item.key] = value + '';
} else {
$store[item.key] = value;
}
updateItem(item);
}
});
} else {
const result = await showAlertOptionSelect(
{
height: Math.min(item.values.length * 56, ALERT_OPTION_MAX_HEIGHT),
rowHeight: item.autoSizeListItem ? undefined : 56,
...item,
options: item.values.map((k) => ({
name: k.title || k.name,
data: k.value,
boxType: 'circle',
type: 'checkbox',
value: (item.currentValue?.() ?? item.currentValue) === k.value
}))
},
{
title: item.title,
message: item.full_description
}
);
DEV_LOG && console.log('result?.data', result?.data);
if (result?.data !== undefined) {
if (item.onResult) {
item.onResult(result.data);
} else {
if (item.valueType === 'string') {
$store[item.key] = result?.data;
} else {
$store[item.key] = parseInt(result?.data, 10);
}
}
updateItem(item);
}
}
break;
}
}
} catch (err) {
showError(err);
}
}
function selectTemplate(item, index, items) {
if (item.type) {
if (item.type === 'prompt' || item.type === 'slider') {
return 'default';
}
return item.type;
}
if (item.icon) {
return 'leftIcon';
}
return 'default';
}
async function changeColor(item, event) {
try {
const newColor = await pickColor($store.color, { anchor: event.object });
if (newColor) {
$store.color = newColor.hex;
updateItem(item, 'type');
}
} catch (error) {
showError(error);
}
}
function onTextChange(e) {
if (e.object.text) {
(e.object as TextField).setSelection(e.object.text.length);
}
}
async function onFolderSelect(item, event) {
item.text = $store.localFolderPath = event.text;
updateItem(item);
}
</script>
<page actionBarHidden={true}>
<gridlayout class="pageContent" rows="auto,*">
<collectionview id="folderPDFSettings" ios:autoReloadItemOnLayout={true} itemTemplateSelector={selectTemplate} {items} row={1} android:paddingBottom={$windowInset.bottom}>
<Template key="color" let:item>
<ListItemAutoSize fontSize={20} subtitle={lc('sync_service_color_desc')} title={lc('color')} on:tap={(e) => changeColor(item, e)}>
<absolutelayout backgroundColor={$store.color} borderColor={colorOutline} borderRadius="50%" borderWidth={2} col={1} height={40} marginLeft={10} width={40} />
</ListItemAutoSize>
</Template>
<Template key="textfield" let:item>
<gridlayout columns="*" margin={tMargin} row={3} rows="auto" on:tap={(e) => item.onTap(item, e)} prop:rightDrawer>
<textfield isUserInteractionEnabled={false} text={item.text} {variant} {...item.textFieldProperties} on:loaded={onTextChange} />
<mdbutton
class="icon-btn"
color={colorOnSurfaceVariant}
horizontalAlignment="right"
isUserInteractionEnabled={false}
text={item.icon}
variant="text"
verticalAlignment="middle"
visibility={item.icon ? 'visible' : 'hidden'} />
</gridlayout>
</Template>
<Template key="switch" let:item>
<ListItemAutoSize leftIcon={item.icon} subtitle={getDescription(item)} title={getTitle(item)} on:tap={(event) => onTap(item, event)}>
<switch id="checkbox" checked={item.value} col={1} marginLeft={10} verticalAlignment="center" on:checkedChange={(e) => onCheckBox(item, e)} />
</ListItemAutoSize>
</Template>
<Template key="pdfoptions" let:item>
<PdfSyncSettingsView {store} on:uppdate={() => updateItem({ type: 'pdfoptions' }, 'type')} />
</Template>
<Template key="selectFolder" let:item>
<FolderTextView text={item.text} on:folder={(e) => onFolderSelect(item, e)} />
</Template>
<Template let:item>
<ListItemAutoSize rightValue={item.rightValue} subtitle={getDescription(item)} title={getTitle(item)} on:tap={(event) => onTap(item, event)} />
</Template>
<Template key="sectionheader" let:item>
<label class="sectionHeader" text={item.title} />
</Template>
<Template key="ocroptions">
<OcrSettingsBottomSheet onlySettings={true} bind:dataType={$store.OCRDataType} bind:languages={$store.OCRLanguages} />
</Template>
</collectionview>
<CActionBar canGoBack modalWindow={true} title={lc('pdf_sync_settings')}>
<mdbutton text={lc('save')} variant="text" verticalAlignment="middle" on:tap={save} />
<!-- <mdbutton class="actionBarButton" text={lc('save')} variant="text" on:tap={save} /> -->
</CActionBar>
</gridlayout>
</page>