This repository was archived by the owner on Jul 30, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdata.js
More file actions
75 lines (69 loc) · 2.27 KB
/
Copy pathdata.js
File metadata and controls
75 lines (69 loc) · 2.27 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
// JayFit data.js — JSON export/import (backup). Data never leaves the device otherwise.
const EXPORT_VERSION = 3;
async function exportData() {
const payload = {
app: 'jayfit',
version: EXPORT_VERSION,
exportedAt: new Date().toISOString(),
settings: {
proteinGoal: getGoal(),
restSec: getRestSec(),
weightUnit: getWeightUnit(),
},
protein: await getAllProtein(db),
weight: await getAllWeight(db),
session: await getAllSessions(db),
sets: await getAllSets(db),
};
const json = JSON.stringify(payload, null, 2);
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `jayfit-backup-${todayStr()}.json`;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
async function importData(file) {
const text = await file.text();
let payload;
try {
payload = JSON.parse(text);
} catch {
alert('JSON 파일을 읽을 수 없습니다.');
return;
}
if (payload.app !== 'jayfit' || !Array.isArray(payload.protein)) {
alert('JayFit 백업 파일이 아닙니다.');
return;
}
if (!confirm('기존 기록을 모두 이 백업으로 교체합니다. 계속할까요?')) return;
for (const name of ['protein', 'weight', 'session', 'sets']) {
await clearStore(db, name);
await putAll(db, name, payload[name] || []);
}
if (payload.settings) {
if (payload.settings.proteinGoal) localStorage.setItem(GOAL_KEY, String(payload.settings.proteinGoal));
if (payload.settings.restSec) localStorage.setItem(REST_KEY, String(payload.settings.restSec));
if (payload.settings.weightUnit === 'kg' || payload.settings.weightUnit === 'lb') {
setWeightUnit(payload.settings.weightUnit);
}
}
alert('가져오기 완료!');
renderProtein();
renderWeight();
renderTrends();
}
function initData() {
document.getElementById('data-export').onclick = exportData;
document.getElementById('data-import-btn').onclick = () => {
document.getElementById('data-import-file').click();
};
document.getElementById('data-import-file').onchange = (e) => {
const file = e.target.files[0];
if (file) importData(file);
e.target.value = '';
};
}