Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit b109a19

Browse files
committed
move weekdayand progressbar render logik to DrawHelper
1 parent 313c5dd commit b109a19

6 files changed

Lines changed: 104 additions & 67 deletions

File tree

src/apps/city-weather/index.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import dayjs from 'dayjs';
33
import { App } from '../app';
44
import { LastUpdated } from '../../models';
55
import { SmartDisplayController } from '../../smart-display-controller';
6-
import { StringHelper } from '../../helper';
6+
import { StringHelper, DrawHelper } from '../../helper';
77
import { OpenWeatherMapService } from './services';
88
import { CityWeatherData, CityWeatherSetting } from './models';
99

@@ -46,29 +46,37 @@ export class CityWeatherApp implements App {
4646
// refresh weather data
4747
this._service
4848
.loadData()
49-
.then(data => {
49+
.then((data) => {
5050
console.log('city weather', data);
5151
this._data.value = data;
5252
})
53-
.catch(error =>
53+
.catch((error) =>
5454
console.error("can't load openweathermap data", error)
5555
);
5656
}
5757

5858
render(): void {
59+
this.renderTemperature();
60+
61+
DrawHelper.renderPixelProgress(
62+
this.controller,
63+
this.calcCacheMinutesAge(),
64+
CityWeatherApp.MaxCacheMinutesAge
65+
);
66+
67+
this._wasRendered = true;
68+
}
69+
70+
private renderTemperature(): void {
5971
const temperature = StringHelper.roundToFixed(
6072
this._data?.value?.temperature
6173
);
6274

6375
this.controller.drawText({
6476
hexColor: '#4CFF00',
6577
text: `${temperature}°`,
66-
position: { x: 7, y: 1 }
78+
position: { x: 7, y: 1 },
6779
});
68-
69-
this.showCacheAgeProgressbar();
70-
71-
this._wasRendered = true;
7280
}
7381

7482
private calcCacheMinutesAge(): number | null {
@@ -81,21 +89,4 @@ export class CityWeatherApp implements App {
8189

8290
return diffMinutes;
8391
}
84-
85-
private showCacheAgeProgressbar(): void {
86-
const cacheMinutesAge = this.calcCacheMinutesAge();
87-
88-
if (cacheMinutesAge == null) {
89-
return;
90-
}
91-
92-
const cacheAgePercent =
93-
cacheMinutesAge / CityWeatherApp.MaxCacheMinutesAge;
94-
const xPosition = 2 + Math.round(26 * cacheAgePercent);
95-
96-
this.controller.drawPixel(
97-
{ x: xPosition, y: 7 },
98-
'#A0A0A0'
99-
);
100-
}
10192
}

src/apps/date/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import dayjs from 'dayjs';
22

33
import { App } from '../app';
4-
import { TimeApp } from '../time';
54
import { SmartDisplayController } from '../../smart-display-controller';
5+
import { DrawHelper } from '../../helper';
66

77
export class DateApp implements App {
88
private _wasRendered = false;
@@ -24,7 +24,7 @@ export class DateApp implements App {
2424
render(): void {
2525
this.renderDate();
2626

27-
TimeApp.renderWeekday(this.controller);
27+
DrawHelper.renderWeekday(this.controller);
2828

2929
this._wasRendered = true;
3030
}

src/apps/room-weather/index.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { App } from '../app';
22
import { RoomWeather } from '../../models';
33
import { SmartDisplayController } from '../../smart-display-controller';
4-
import { StringHelper } from '../../helper';
4+
import { StringHelper, DrawHelper } from '../../helper';
55

66
export class RoomWeatherApp implements App {
77
private _wasRendered = false;
@@ -27,35 +27,26 @@ export class RoomWeatherApp implements App {
2727
}
2828

2929
render(): void {
30+
this.renderTemperature();
31+
32+
DrawHelper.renderProgressbar(
33+
this.controller,
34+
this._roomWeather?.humidity,
35+
100
36+
);
37+
38+
this._wasRendered = true;
39+
}
40+
41+
private renderTemperature(): void {
3042
const temperature = StringHelper.roundToFixed(
3143
this._roomWeather?.temperature
3244
);
3345

3446
this.controller.drawText({
3547
hexColor: '#00C8C8',
3648
text: `${temperature}°`,
37-
position: { x: 7, y: 1 }
49+
position: { x: 7, y: 1 },
3850
});
39-
40-
this.showHumidityProgressbar();
41-
42-
this._wasRendered = true;
43-
}
44-
45-
private showHumidityProgressbar(): void {
46-
const humidity = this._roomWeather?.humidity;
47-
48-
if (humidity == null) {
49-
return;
50-
}
51-
52-
const percentValue = humidity / 100;
53-
const xEndPosition = 2 + Math.round(26 * percentValue);
54-
55-
this.controller.drawLine(
56-
{ x: 2, y: 7 },
57-
{ x: xEndPosition, y: 7 },
58-
'#A0A0A0'
59-
);
6051
}
6152
}

src/apps/time/index.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import dayjs from 'dayjs';
22

33
import { App } from '../app';
44
import { SmartDisplayController } from '../../smart-display-controller';
5+
import { DrawHelper } from '../../helper';
56

67
export class TimeApp implements App {
78
private showColon = true;
@@ -17,7 +18,7 @@ export class TimeApp implements App {
1718
render(): void {
1819
this.renderTime();
1920

20-
TimeApp.renderWeekday(this.controller);
21+
DrawHelper.renderWeekday(this.controller);
2122

2223
// toggle colon
2324
this.showColon = !this.showColon;
@@ -33,20 +34,4 @@ export class TimeApp implements App {
3334
position: { x: 7, y: 1 },
3435
});
3536
}
36-
37-
static renderWeekday(controller: SmartDisplayController): void {
38-
const currentWeekday = dayjs().weekday();
39-
const getXPositionByWeekDay = (weekday: number) => weekday * 4 + 2;
40-
41-
for (let weekday = 0; weekday < 7; weekday++) {
42-
const xPosition = getXPositionByWeekDay(weekday);
43-
const color = weekday === currentWeekday ? '#00C8C8' : '#A0A0A0';
44-
45-
controller.drawLine(
46-
{ x: xPosition, y: 7 },
47-
{ x: xPosition + 2, y: 7 },
48-
color
49-
);
50-
}
51-
}
5237
}

src/helper/draw-helper.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import dayjs from 'dayjs';
2+
3+
import { SmartDisplayController } from '../smart-display-controller';
4+
5+
export class DrawHelper {
6+
static renderWeekday(controller: SmartDisplayController): void {
7+
const currentWeekday = dayjs().weekday();
8+
const getXPositionByWeekDay = (weekday: number) => weekday * 4 + 2;
9+
10+
for (let weekday = 0; weekday < 7; weekday++) {
11+
const xPosition = getXPositionByWeekDay(weekday);
12+
const color = weekday === currentWeekday ? '#00C8C8' : '#A0A0A0';
13+
14+
controller.drawLine(
15+
{ x: xPosition, y: 7 },
16+
{ x: xPosition + 2, y: 7 },
17+
color
18+
);
19+
}
20+
}
21+
22+
static renderProgressbar(
23+
controller: SmartDisplayController,
24+
value: number | null | undefined,
25+
maxValue: number,
26+
hexColor = '#A0A0A0'
27+
): void {
28+
const xEndPosition = this.calcProgressXPosition(value, maxValue);
29+
30+
if (xEndPosition == null) {
31+
return;
32+
}
33+
34+
controller.drawLine(
35+
{ x: 2, y: 7 },
36+
{ x: xEndPosition, y: 7 },
37+
hexColor
38+
);
39+
}
40+
41+
static renderPixelProgress(
42+
controller: SmartDisplayController,
43+
value: number | null,
44+
maxValue: number,
45+
hexColor: string = '#A0A0A0'
46+
): void {
47+
const xPosition = this.calcProgressXPosition(value, maxValue);
48+
49+
if (xPosition == null) {
50+
return;
51+
}
52+
53+
controller.drawPixel({ x: xPosition, y: 7 }, hexColor);
54+
}
55+
56+
private static calcProgressXPosition(
57+
value: number | null | undefined,
58+
maxValue: number
59+
): number | undefined {
60+
if (value == null) {
61+
return;
62+
}
63+
64+
const percentValue = value / maxValue;
65+
const xPosition = 2 + Math.round(26 * percentValue);
66+
67+
return xPosition;
68+
}
69+
}

src/helper/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
export * from './draw-helper';
12
export * from './mqtt-helper';
23
export * from './string-helper';

0 commit comments

Comments
 (0)