forked from mebjas/html5-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.ts
72 lines (60 loc) · 1.71 KB
/
storage.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
/**
* @fileoverview
* Core storage related APIs.
*
* @author mebjas <minhazav@gmail.com>
*
* The word "QR Code" is registered trademark of DENSO WAVE INCORPORATED
* http://www.denso-wave.com/qrcode/faqpatent-e.html
*/
interface PersistedData {
hasPermission: boolean;
lastUsedCameraId: string | null;
}
class PersistedDataFactory {
static createDefault(): PersistedData {
return {
hasPermission: false,
lastUsedCameraId: null
};
}
}
export class PersistedDataManager {
private data: PersistedData = PersistedDataFactory.createDefault();
private static LOCAL_STORAGE_KEY: string = "HTML5_QRCODE_DATA";
constructor() {
let data = localStorage.getItem(PersistedDataManager.LOCAL_STORAGE_KEY);
if (!data) {
this.reset();
} else {
this.data = JSON.parse(data);
}
}
public hasCameraPermissions(): boolean {
return this.data.hasPermission;
}
public getLastUsedCameraId(): string | null {
return this.data.lastUsedCameraId;
}
public setHasPermission(hasPermission: boolean) {
this.data.hasPermission = hasPermission;
this.flush();
}
public setLastUsedCameraId(lastUsedCameraId: string) {
this.data.lastUsedCameraId = lastUsedCameraId;
this.flush();
}
public resetLastUsedCameraId() {
this.data.lastUsedCameraId = null;
this.flush();
}
public reset() {
this.data = PersistedDataFactory.createDefault();
this.flush();
}
private flush(): void {
localStorage.setItem(
PersistedDataManager.LOCAL_STORAGE_KEY,
JSON.stringify(this.data));
}
}