-
Notifications
You must be signed in to change notification settings - Fork 2
/
intermittent_fasting.js
189 lines (150 loc) · 5.12 KB
/
intermittent_fasting.js
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: blue; icon-glyph: magic;
// Concentric circle V3
const canvSize = 282;
const canvTextSize = 40;
const canvas = new DrawContext();
canvas.opaque = false
const widgetBGColor = new Color('000'); //Widget background color
const circleTextColor = new Color('#fff'); //Widget text color
const bgCircleColorFasting = new Color('00382c')
const bgCircleColoreatingEnd = new Color('382e00') // bg circle color, full circle 382e00
const progressCircleColoreatingEnd = new Color('24ffd7')
const progressCircleColorFasting = new Color('FFD723')
const argsParam = args.widgetParameter
const fields = argsParam ? argsParam.split(',') : []
const time = fields[1] ? fields[1].split(':') : '13:00'
const device = Device
const lang = device.language()
const canvWidth = 24; // circle thickness
const canvRadius = 160; // circle radius
canvas.size = new Size(canvSize, canvSize);
canvas.respectScreenScale = true;
const eatingTime = new Date()
eatingTime.setHours(time[0])
eatingTime.setMinutes(time[1])
let times = [
[time[0], time[1]], [addHours(eatingTime, fields[0]).getHours(), addHours(eatingTime, fields[0]).getMinutes()]
]
const headerGer = isEatingTime() ? 'Fastenzeit beginnt in:' : 'Fastenzeit endet in:'
const headerEng = isEatingTime() ? 'Fasting period starting in:' : 'Fasting period ends in:'
let totalEatingMins = fields[0] * 60
let totalFastingMins = (24 - fields[0]) * 60
let minsRemainingCircle = isEatingTime() ? Math.floor((getMins() / totalEatingMins * 100) * 3.6) : Math.floor((getMins() / totalFastingMins * 100) * 3.6)
let dayRadiusOffset = 60;
/*
START Widget Layout
*/
let widget = new ListWidget();
widget.setPadding(0, 5, 1, 0);
makeCircle(dayRadiusOffset,
isEatingTime() ? bgCircleColoreatingEnd : bgCircleColorFasting,
isEatingTime() ? progressCircleColorFasting : progressCircleColoreatingEnd,
Math.floor(minsRemainingCircle),
circleTextColor)
drawText(
lang === 'de' ? headerGer : headerEng,
circleTextColor,
20, 22
)
drawText(
getMinOutput(),
circleTextColor,
140, 40
)
drawText(
lang === 'de' ? 'Stunden' : 'Hours',
circleTextColor,
200, 22
)
widget.backgroundColor = widgetBGColor
widget.addImage(canvas.getImage())
Script.setWidget(widget);
widget.presentSmall();
Script.complete();
/*
END Widget Layout
*/
function makeCircle(radiusOffset, bgCircleColor, fgCircleColor, degree, txtColor) {
let ctr = new Point(canvSize / 2, canvSize / 2)
// Outer circle
CoordOffset = 0
RadiusOffset = 0
bgx = ctr.x - (canvRadius - radiusOffset);
bgy = ctr.y - (canvRadius - radiusOffset);
bgd = 2 * (canvRadius - radiusOffset);
bgr = new Rect(
bgx + CoordOffset,
bgy + CoordOffset + 20,
bgd,
bgd
);
canvas.setStrokeColor(bgCircleColor);
canvas.setLineWidth(canvWidth);
canvas.strokeEllipse(bgr);
// Inner circle
canvas.setFillColor(fgCircleColor);
for (t = 0; t < degree; t++) {
rect_x = ctr.x + (canvRadius - radiusOffset) * sinDeg(t) - canvWidth / 2;
rect_y = ctr.y - (canvRadius - radiusOffset) * cosDeg(t) - canvWidth / 2;
rect_r = new Rect(
rect_x,
rect_y + 20,
canvWidth,
canvWidth
);
canvas.fillEllipse(rect_r);
}
}
function drawText(txt, txtColor, txtOffset, fontSize) {
const txtRect = new Rect(
canvTextSize / 2 - 20,
txtOffset - canvTextSize / 2,
canvSize,
canvTextSize
);
canvas.setTextColor(txtColor);
canvas.setFont(Font.boldSystemFont(fontSize));
canvas.setTextAlignedCenter()
canvas.drawTextInRect(txt, txtRect)
}
function sinDeg(deg) {
return Math.sin((deg * Math.PI) / 180);
}
function cosDeg(deg) {
return Math.cos((deg * Math.PI) / 180);
}
function pad(num) {
return ("0" + parseInt(num)).substr(-2);
}
function getMinOutput() {
let now = new Date()
const nextShip = times.find(it => it[0] > now.getHours() || it[0] == now.getHours() && it[1] >= now.getMinutes()) || times[0];
let hours = nextShip[0] - now.getHours();
let minutes = nextShip[1] - now.getMinutes();
if (minutes < 0) { minutes += 60; hours -= 1 }
if (hours < 0) { hours += 24; }
const seconds = 60 - now.getSeconds();
return `${pad(hours)}:${pad(minutes)}`;
}
function getMins() {
let now = new Date()
const nextShip = times.find(it => it[0] > now.getHours() || it[0] == now.getHours() && it[1] >= now.getMinutes()) || times[0];
let hours = nextShip[0] - now.getHours();
let minutes = nextShip[1] - now.getMinutes();
if (minutes < 0) { minutes += 60; hours -= 1 }
if (hours < 0) { hours += 24; }
const seconds = 60 - now.getSeconds();
return hours * 60 + minutes;
}
function isEatingTime() {
let now = new Date()
const nextShip = times.findIndex(it => it[0] > now.getHours() || it[0] == now.getHours() && it[1] >= now.getMinutes()) || times[0];
return nextShip === 1 ? true : false;
}
function addHours(date, h) {
newDate = new Date()
newDate.setTime(date.getTime() + (h * 60 * 60 * 1000));
return newDate;
}