diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 8a8506557..989bd9022 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -10,7 +10,6 @@ import { ThemeService } from 'src/app/services/theme.service'; import { environment } from 'src/environments/environment'; import { Storage } from '@ionic/storage'; import { ChatController } from './controller/chat'; -import { AngularFireMessaging } from '@angular/fire/messaging'; const CUSTOM_DATE_FORMATS: NgxMatDateFormats = { parse: { @@ -41,10 +40,11 @@ export class AppComponent { private storage: Storage, private ChatSystemService: ChatSystemService ) { + this.initializeApp(); this.storage.set('version', environment.version).then(() => {}) - ChatController.ChatSystemService = this.ChatSystemService + } /* requestPermission() { @@ -69,24 +69,13 @@ export class AppComponent { initializeApp() { this.platform.ready().then(() => { this.statusBar.styleDefault(); - /* this.splashScreen.hide(); */ - + if (this.platform.is("tablet")) { window.screen.orientation.unlock(); } else if( this.platform.is("mobile")) { window.screen.orientation.lock('portrait'); } - if (this.platform.is('desktop') || this.platform.is('mobileweb')) { - - } else { - try { - // this.sqliteservice.databaseConn(); - } catch (error) { - - } - } - }); } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8852ecf4e..2aaa685b8 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -7,7 +7,7 @@ import { StatusBar } from '@ionic-native/status-bar/ngx'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; -import { HttpClientModule } from '@angular/common/http'; +import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'; @@ -81,6 +81,7 @@ import { AngularFireModule } from '@angular/fire'; import { AngularFireMessagingModule } from '@angular/fire/messaging'; import { firebaseConfig } from '../firebase-config'; import { EditorModule } from '@tinymce/tinymce-angular'; +import { LoggingInterceptorService } from './services/logging-interceptor.service'; // import { ServiceWorkerModule } from '@angular/service-worker'; // import { AngularFireModule } from '@angular/fire'; @@ -199,6 +200,7 @@ import { FirebaseX } from '@ionic-native/firebase-x/ngx'; */ NgxExtendedPdfViewerModule, FileOpener, DocumentViewer, + { provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptorService, multi: true }, ], bootstrap: [AppComponent], diff --git a/src/app/services/background.service.ts b/src/app/services/background.service.ts index ca5021972..7a6958674 100644 --- a/src/app/services/background.service.ts +++ b/src/app/services/background.service.ts @@ -21,7 +21,14 @@ export class BackgroundService { private themeservice: ThemeService, private storageservice: StorageService, private http: HttpClient, - ) { } + ) { + + window.addEventListener('focus', (event) => { + if(this.status == 'offline') { + this.tryToReachTheServer() + } + }); + } online() { if(this.status == 'online') { @@ -68,28 +75,36 @@ export class BackgroundService { return false } + 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() + } + }) + } + } + + + async tryToReachTheServer() { let opts = { headers: {}, } try { await this.http.post(environment.apiURL + "UserAuthentication/Login", '', opts).toPromise(); + return true } catch (error) { if(error.status != 400) { - - 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() - } - }) - + return false } } } diff --git a/src/app/services/chat/rochet-chat-connector.service.ts b/src/app/services/chat/rochet-chat-connector.service.ts index 37ec3f90c..4f6b49ccd 100644 --- a/src/app/services/chat/rochet-chat-connector.service.ts +++ b/src/app/services/chat/rochet-chat-connector.service.ts @@ -5,6 +5,7 @@ import { deepFind } from 'src/plugin/deep' import { environment } from 'src/environments/environment'; import { SessionStore } from 'src/app/store/session.service'; import { chatHistory, Rooms } from 'src/app/models/chatMethod'; +import { BackgroundService } from '../background.service'; @Injectable({ providedIn: 'root' @@ -14,7 +15,7 @@ export class RochetChatConnectorService { isLogin = false; loginResponse = {} - constructor() { + constructor(private backgroundservice: BackgroundService,) { } connect() { @@ -752,6 +753,7 @@ export class RochetChatConnectorService { }, onopen: async ()=> { this.ws.connected = true + this.backgroundservice.online(); setTimeout(()=>{ this.ws.wsMsgQueue() @@ -804,6 +806,9 @@ export class RochetChatConnectorService { }, onmessage: async (event: any)=> { + + this.backgroundservice.online(); + const data = JSON.parse(event.data) for (const [key, value] of Object.entries(this.wsCallbacks)) { diff --git a/src/app/services/logging-interceptor.service.spec.ts b/src/app/services/logging-interceptor.service.spec.ts new file mode 100644 index 000000000..481c7c387 --- /dev/null +++ b/src/app/services/logging-interceptor.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { LoggingInterceptorService } from './logging-interceptor.service'; + +describe('LoggingInterceptorService', () => { + let service: LoggingInterceptorService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(LoggingInterceptorService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/logging-interceptor.service.ts b/src/app/services/logging-interceptor.service.ts new file mode 100644 index 000000000..bcd311290 --- /dev/null +++ b/src/app/services/logging-interceptor.service.ts @@ -0,0 +1,50 @@ +import { HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { finalize, tap } from 'rxjs/operators'; +import { BackgroundService } from './background.service'; + +@Injectable({ + providedIn: 'root' +}) +export class LoggingInterceptorService { + + constructor( + private backgroundservice: BackgroundService, + ) {} + + intercept(req: HttpRequest, next: HttpHandler) { + const started = Date.now(); + let ok: string; + + // extend server response observable with logging + return next.handle(req) + .pipe( + tap({ + // Succeeds when there is a response; ignore other events + next: (event) => { + + ok = event instanceof HttpResponse ? 'succeeded' : '' + + if(ok == 'succeeded' || (typeof event['status'] == 'number' && event['status'] !=0)) { + // set to online + this.backgroundservice.online(); + } + + }, + // Operation failed; error is an HttpErrorResponse + error: (error) => { + if (error.status === 0 && error.error instanceof ProgressEvent && !req.url.includes('UserAuthentication/Login')) { + // A client-side or network error occurred. Handle it accordingly. + this.backgroundservice.offline(); + } + } + }), + // Log when response observable either completes or errors + finalize(() => { + // const elapsed = Date.now() - started; + // const msg = `${req.method} "${req.urlWithParams}" + // ${ok} in ${elapsed} ms.`; + }) + ); + } +} diff --git a/version/git-version.ts b/version/git-version.ts index 90cd18ce6..9bd933ad9 100644 --- a/version/git-version.ts +++ b/version/git-version.ts @@ -1,12 +1,12 @@ export let versionData = { - "shortSHA": "f28e99133", - "SHA": "f28e991338b0829c10b3ac8d8d018b1a05662cc1", + "shortSHA": "3f65108fe", + "SHA": "3f65108fe44450cb4bc334cfdc028bc65e1a35d5", "branch": "merge-all", "lastCommitAuthor": "'Peter Maquiran'", - "lastCommitTime": "'Wed Jul 12 10:44:06 2023 +0100'", - "lastCommitMessage": "update tiny", - "lastCommitNumber": "5058", + "lastCommitTime": "'Wed Jul 12 10:45:49 2023 +0100'", + "lastCommitMessage": "merge", + "lastCommitNumber": "5061", "change": "", - "changeStatus": "On branch merge-all\nAll conflicts fixed but you are still merging.\n (use \"git commit\" to conclude merge)\n\nChanges to be committed:\n\tmodified: package-lock.json\n\tmodified: package.json\n\tmodified: src/global.scss\n\tnew file: src/style/ios.scss\n\tmodified: version/git-version.ts", + "changeStatus": "On branch merge-all\nAll conflicts fixed but you are still merging.\n (use \"git commit\" to conclude merge)\n\nChanges to be committed:\n\tmodified: src/app/app.component.ts\n\tmodified: src/app/app.module.ts\n\tmodified: src/app/services/background.service.ts\n\tmodified: src/app/services/chat/rochet-chat-connector.service.ts\n\tnew file: src/app/services/logging-interceptor.service.spec.ts\n\tnew file: src/app/services/logging-interceptor.service.ts\n\tmodified: version/git-version.ts", "changeAuthor": "peter.maquiran" } \ No newline at end of file