Files
admin-edu-space/example/aulas-unicaba-mvp/src/app/app.component.ts
T

71 lines
2.1 KiB
TypeScript

import { Component, OnInit, NgZone } from '@angular/core';
import { LocalNotifications } from '@capacitor/local-notifications';
import { BackgroundFetch } from '@transistorsoft/capacitor-background-fetch';
import { Network } from '@capacitor/network';
import { DataService } from './services/data';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
standalone: false,
})
export class AppComponent implements OnInit {
constructor(
private dataService: DataService,
private router: Router,
private zone: NgZone
) {}
async ngOnInit() {
this.setupBackgroundFetch();
this.setupNotificationAction();
}
private async setupBackgroundFetch() {
const status = await Network.getStatus();
// WiFi: 15 min, Mobile: 60 min
const interval = status.connectionType === 'wifi' ? 15 : 60;
BackgroundFetch.configure({
minimumFetchInterval: interval,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true
}, async (taskId: string) => {
console.log('[BackgroundFetch] Event received: ', taskId);
await this.performSync();
BackgroundFetch.finish(taskId);
}, (error: string) => {
console.error('[BackgroundFetch] FAILED: ', error);
});
}
private async performSync() {
try {
// 1. Obtener favoritos
const favs = await this.dataService.getFavorites();
// 2. Fetch y Sync (esto ya dispara notificaciones si hay cambios)
this.dataService.getHorarios().subscribe(async (newData) => {
await this.dataService.syncWithCache(newData, favs || []);
});
} catch (e) {
console.error('[BackgroundSync] Error:', e);
}
}
private setupNotificationAction() {
LocalNotifications.addListener('localNotificationActionPerformed', (notification) => {
const codigo = notification.notification.extra?.codigo;
if (codigo) {
this.zone.run(() => {
// Navegar a home y pasar el código para resaltar/scroll
this.router.navigate(['/home'], { queryParams: { highlight: codigo } });
});
}
});
}
}