Files
doneit-web/src/app/services/background.service.ts
T

129 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-01-18 16:24:57 +01:00
import { Injectable } from '@angular/core';
import { ThemeService } from 'src/app/services/theme.service';
import { StorageService} from 'src/app/services/storage.service';
2022-12-13 17:21:48 +01:00
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
2021-01-18 16:24:57 +01:00
@Injectable({
providedIn: 'root'
})
2021-08-27 15:30:02 +01:00
export class BackgroundService {
2021-01-18 16:24:57 +01:00
2021-10-18 17:42:25 +01:00
callBacks: {
2021-10-19 09:41:06 +01:00
type: 'Offline' | 'Online' | 'Notification',
2021-10-18 17:42:25 +01:00
object?: string
funx: Function
}[] = []
2023-01-09 17:07:02 +01:00
status: 'online'| 'offline' = 'online'
constructor(
private themeservice: ThemeService,
2022-12-13 17:21:48 +01:00
private storageservice: StorageService,
private http: HttpClient,
2024-06-19 09:03:26 +01:00
) {
2023-07-11 17:54:08 +01:00
window.addEventListener('focus', (event) => {
if(this.status == 'offline') {
this.tryToReachTheServer()
}
});
}
2021-08-27 15:35:29 +01:00
online() {
2023-01-09 17:07:02 +01:00
if(this.status == 'online') {
return false
}
this.status = 'online'
2024-06-19 09:03:26 +01:00
2022-10-11 17:07:51 +01:00
this.paint()
this.callBacks.forEach((e) => {
if (e.type == 'Online') {
e.funx()
}
})
}
2022-10-11 17:07:51 +01:00
paint() {
if (this.themeservice.currentTheme == 'gov') {
document.body.style.setProperty(`--color`, "#d9d9d9");
document.body.style.setProperty(`--color2`, "#f0f0f0");
document.body.style.setProperty(`--color3`, "#d9d9d9");
document.body.style.setProperty(`--color4`, "#d9d9d9ee");
document.body.style.setProperty(`--color5`, "#ececec");
this.storageservice.store('networkCheckStore','online');
2022-10-20 15:45:10 +01:00
} else if (this.themeservice.currentTheme == 'default') {
document.body.style.setProperty(`--color`, "#0782C9");
document.body.style.setProperty(`--color2`, "#45BAFF");
document.body.style.setProperty(`--color3`, "#0782C9");
document.body.style.setProperty(`--color4`, "#0782c9f0");
document.body.style.setProperty(`--color5`, "#45BAFF");
this.storageservice.store('networkCheckStore','online');
2022-10-20 15:45:10 +01:00
} else if (this.themeservice.currentTheme == 'doneIt') {
document.body.style.setProperty(`--color`, "#69B3E7");
document.body.style.setProperty(`--color2`, "#A5D1F1");
document.body.style.setProperty(`--color3`, "#69B3E7");
document.body.style.setProperty(`--color4`, "#69B3E7");
document.body.style.setProperty(`--color5`, "#A5D1F1");
}
2021-08-27 15:35:29 +01:00
}
2022-12-13 17:21:48 +01:00
async offline() {
2023-01-09 17:07:02 +01:00
if(this.status == 'offline') {
2023-07-26 13:06:42 +01:00
return true
2023-01-09 17:07:02 +01:00
}
2023-07-11 17:54:08 +01:00
const hasReachedTheServer = await this.tryToReachTheServer()
if(!hasReachedTheServer) {
this.status = 'offline'
document.body.style.setProperty(`--color`, "#ffb703");
document.body.style.setProperty(`--color2`, "#ffb703");
document.body.style.setProperty(`--color3`, "#ffb703");
document.body.style.setProperty(`--color4`, "#ffb703");
document.body.style.setProperty(`--color5`, "#ffb703");
this.storageservice.store('networkCheckStore','offline');
this.callBacks.forEach((e) => {
if (e.type == 'Offline') {
e.funx()
}
})
2023-07-26 13:06:42 +01:00
return true
2023-07-11 17:54:08 +01:00
}
2023-07-26 13:06:42 +01:00
return false
2023-07-11 17:54:08 +01:00
}
async tryToReachTheServer() {
2022-12-13 17:21:48 +01:00
let opts = {
headers: {},
}
try {
2024-06-19 09:03:26 +01:00
await this.http.get("assets/images/theme/gov/governoangola_A.png").toPromise();
2023-07-11 17:54:08 +01:00
return true
2022-12-13 17:21:48 +01:00
} catch (error) {
2023-07-26 13:06:42 +01:00
if(error.status === 0) {
2023-07-11 17:54:08 +01:00
return false
2024-06-19 09:03:26 +01:00
}
2023-07-26 13:06:42 +01:00
return true
2022-12-13 17:21:48 +01:00
}
2021-10-18 17:42:25 +01:00
}
2021-10-19 09:41:06 +01:00
registerBackService(type: 'Offline' | 'Online' | 'Notification', funx: Function, object = '') {
2021-10-18 17:42:25 +01:00
this.callBacks.push({
type,
funx,
object
})
2021-08-27 15:35:29 +01:00
}
2021-10-20 17:46:03 +01:00
2021-01-18 16:24:57 +01:00
}