Skip to content

Commit edc7b84

Browse files
committed
Add time intervals (add and close interval)
1 parent 1ea300d commit edc7b84

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { TimeInterval } from "../entity/time-interval";
2+
import { injecStorage } from "../storage/inject-storage";
3+
import { StorageParams } from "../storage/storage-params";
4+
import { todayLocalDate } from "../utils/common";
5+
6+
export async function closeInterval(domain:string | null): Promise<void>{
7+
if (domain == null) return;
8+
const storage = injecStorage();
9+
const timeIntervalList = await storage.getValue(StorageParams.TIMEINTERVAL_LIST) as TimeInterval[];
10+
const item = timeIntervalList.find(x => x.domain === domain && x.day == todayLocalDate());
11+
item?.closeInterval();
12+
await storage.saveValue(StorageParams.TIMEINTERVAL_LIST, timeIntervalList);
13+
}
14+
15+
export async function addInterval(domain:string | null): Promise<void>{
16+
if (domain == null) return;
17+
18+
const storage = injecStorage();
19+
const timeIntervalList = await storage.getValue(StorageParams.TIMEINTERVAL_LIST) as TimeInterval[];
20+
const item = timeIntervalList.find(x => x.domain === domain && x.day == todayLocalDate());
21+
if (item != undefined) {
22+
if (item.day == todayLocalDate())
23+
item.addInterval();
24+
else {
25+
const newInterval = new TimeInterval(todayLocalDate(), domain);
26+
newInterval.addInterval();
27+
timeIntervalList.push(newInterval);
28+
}
29+
} else {
30+
const newInterval = new TimeInterval(todayLocalDate(), domain);
31+
newInterval.addInterval();
32+
timeIntervalList.push(newInterval);
33+
}
34+
await storage.saveValue(StorageParams.TIMEINTERVAL_LIST, timeIntervalList);
35+
}
File renamed without changes.

src/entity/time-interval.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export class TimeInterval {
2+
domain: string;
3+
intervals: any;
4+
day: string;
5+
6+
constructor(day:string, domain:string) {
7+
this.domain = domain;
8+
this.intervals = [];
9+
this.day = day;
10+
}
11+
12+
addInterval() {
13+
const stringDate = this.getCurrentStringDate();
14+
this.intervals.push(stringDate + '-' + stringDate);
15+
}
16+
17+
closeInterval() {
18+
const stringDate = this.getCurrentStringDate();
19+
const currentInterval = this.intervals[this.intervals.length - 1];
20+
if (!currentInterval) {
21+
if (currentInterval.split('-')[0] == currentInterval.split('-')[1]) {
22+
this.intervals.pop();
23+
this.intervals.push(currentInterval.split('-')[0] + '-' + stringDate);
24+
}
25+
}
26+
}
27+
28+
private getCurrentStringDate():string{
29+
const date = new Date();
30+
return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
31+
}
32+
};

0 commit comments

Comments
 (0)