forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.ts
More file actions
81 lines (66 loc) · 1.79 KB
/
tab.ts
File metadata and controls
81 lines (66 loc) · 1.79 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
import { todayLocalDate } from '../utils/date';
import { logger } from '../utils/logger';
export class Tab implements ISerializable<Tab> {
url: string = '';
favicon: string | undefined = '';
summaryTime: number = 0;
counter: number = 0;
days: TabDay[] = [];
init(url: string) {
this.url = url;
}
incSummaryTime(): void {
this.summaryTime += 1;
const day = this.days.find(x => x.date == todayLocalDate());
if (day === undefined) {
const newTab = this.addNewDay();
newTab.incSummaryTime();
} else day.incSummaryTime();
}
incCounter(): void {
this.counter += 1;
if (__DEV__) logger.log(`Counter ${this.url} - ${this.counter}`);
const day = this.days.find(x => x.date == todayLocalDate());
if (day === undefined) {
const newTab = this.addNewDay();
newTab.incCounter();
} else day.incCounter();
}
addNewDay(): TabDay {
const newTabDay = new TabDay();
newTabDay.init(todayLocalDate());
this.days.push(newTabDay);
return newTabDay;
}
deserialize(input: Tab) {
this.url = input.url;
this.counter = input.counter;
this.favicon = input.favicon;
this.summaryTime = input.summaryTime;
if (input.days?.length > 0) this.days = input.days.map(x => new TabDay().deserialize(x));
return this;
}
setFavicon(favicon: string | undefined) {
this.favicon = favicon;
}
}
export class TabDay implements ISerializable<TabDay> {
counter: number = 0;
date: string = '';
summary: number = 0;
init(date: string) {
this.date = date;
}
incSummaryTime(): void {
this.summary += 1;
}
incCounter(): void {
this.counter += 1;
}
deserialize(input: TabDay): TabDay {
this.counter = input.counter;
this.date = input.date;
this.summary = input.summary;
return this;
}
}