Introduce a locale/12-hour/24-hour preference on the clock service and a shared Intl-based formatter used across the dashboard.
47 lines
1009 B
JavaScript
47 lines
1009 B
JavaScript
import Service, { service } from '@ember/service';
|
|
import { tracked } from '@glimmer/tracking';
|
|
import { DEFAULT_TIME_FORMAT } from '../utils/time-format';
|
|
|
|
export default class ClockService extends Service {
|
|
@service storage;
|
|
|
|
@tracked time = new Date();
|
|
@tracked timeFormat = DEFAULT_TIME_FORMAT;
|
|
|
|
intervalId;
|
|
ready = null;
|
|
|
|
constructor() {
|
|
super(...arguments);
|
|
this.ready = this.load();
|
|
this.start();
|
|
}
|
|
|
|
async load() {
|
|
try {
|
|
const stored = await this.storage.get('preferences', 'time-format');
|
|
if (stored) {
|
|
this.timeFormat = stored;
|
|
}
|
|
} catch (err) {
|
|
console.error('Error loading the time format:', err);
|
|
}
|
|
}
|
|
|
|
async setTimeFormat(format) {
|
|
this.timeFormat = format;
|
|
await this.storage.set('preferences', 'time-format', format);
|
|
}
|
|
|
|
start() {
|
|
this.intervalId = setInterval(() => {
|
|
this.time = new Date();
|
|
}, 2000);
|
|
}
|
|
|
|
willDestroy() {
|
|
clearInterval(this.intervalId);
|
|
super.willDestroy();
|
|
}
|
|
}
|