diff --git a/src/app/service/api.service.ts b/src/app/service/api.service.ts index 1fedbd9..c0cd5d6 100644 --- a/src/app/service/api.service.ts +++ b/src/app/service/api.service.ts @@ -11,8 +11,6 @@ import {Database} from './db/Database'; import {DatabaseColumn} from './db/DatabaseColumn'; import {DatabaseEntry} from './db/DatabaseEntry'; import {FileGroup} from './db/FileGroup'; -import {Page} from './db/Page' -import {PageNode} from "./db/PageNode"; export class ResourceNotAvailableOfflineError extends Error { constructor(msg = 'This resource is not yet available offline on this device.') { @@ -43,11 +41,31 @@ export class ApiService { if ( !isConnected ) { this.makeOffline(); } else { - this.makeOnline(); // TODO add checks for server. + this.checkOnline().then(isOnline => { + if ( isOnline ) { + this.makeOnline(); + } else { + this.makeOffline(); + } + }); + } + }); + + this.checkOnline().then(isConnected => { + if ( !isConnected ) { + this.makeOffline(); + } else { + this.makeOnline(); } }); } + public checkOnline(): Promise { + return fetch(this.statUrl).then(resp => { + return resp && (resp.ok || resp.type === 'opaque'); + }); + } + public makeOffline() { this.offline = true; this.offline$.next(true);