From bfbfd350b144ca2fe2e3fb012a788e600b8f3d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 31 Mar 2021 10:12:23 +0200 Subject: [PATCH] MOBILE-3640 database: Location service improvements on error handling --- .../addon-mod-data-field-latlong.html | 2 +- .../data/fields/latlong/component/latlong.ts | 7 ++-- src/core/services/geolocation.ts | 34 ++++++++++++++++--- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/addons/mod/data/fields/latlong/component/addon-mod-data-field-latlong.html b/src/addons/mod/data/fields/latlong/component/addon-mod-data-field-latlong.html index 8d99619ba..cb3e8c524 100644 --- a/src/addons/mod/data/fields/latlong/component/addon-mod-data-field-latlong.html +++ b/src/addons/mod/data/fields/latlong/component/addon-mod-data-field-latlong.html @@ -11,7 +11,7 @@ °E -
+
{{ 'addon.mod_data.mylocation' | translate }} diff --git a/src/addons/mod/data/fields/latlong/component/latlong.ts b/src/addons/mod/data/fields/latlong/component/latlong.ts index 2a120d0da..ba17887d0 100644 --- a/src/addons/mod/data/fields/latlong/component/latlong.ts +++ b/src/addons/mod/data/fields/latlong/component/latlong.ts @@ -33,6 +33,7 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo north?: number; east?: number; + locationServicesEnabled = false; constructor( fb: FormBuilder, @@ -87,7 +88,7 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo /** * @inheritdoc */ - protected init(): void { + protected async init(): Promise { if (this.value) { this.updateValue(this.value); } @@ -95,6 +96,8 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo if (this.editMode) { this.addControl('f_' + this.field.id + '_0', this.north); this.addControl('f_' + this.field.id + '_1', this.east); + this.locationServicesEnabled = await CoreGeolocation.canRequest(); + } else if (this.searchMode) { this.addControl('f_' + this.field.id); } @@ -136,7 +139,7 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo * * @param error Location error. */ - protected showLocationErrorModal(error: CoreAnyError): void { + protected showLocationErrorModal(error: CoreAnyError | CoreGeolocationError): void { if (error instanceof CoreGeolocationError) { CoreDomUtils.showErrorModal(this.getGeolocationErrorMessage(error), true); diff --git a/src/core/services/geolocation.ts b/src/core/services/geolocation.ts index 84e60f0ce..aecc397cd 100644 --- a/src/core/services/geolocation.ts +++ b/src/core/services/geolocation.ts @@ -16,8 +16,9 @@ import { Injectable } from '@angular/core'; import { Coordinates } from '@ionic-native/geolocation'; import { CoreApp } from '@services/app'; -import { CoreError } from '@classes/errors/error'; +import { CoreAnyError, CoreError } from '@classes/errors/error'; import { Geolocation, Diagnostic, makeSingleton } from '@singletons'; +import { CoreUtils } from './utils/utils'; @Injectable({ providedIn: 'root' }) export class CoreGeolocationProvider { @@ -89,7 +90,6 @@ export class CoreGeolocationProvider { */ protected async doAuthorizeLocation(failOnDeniedOnce: boolean = false): Promise { const authorizationStatus = await Diagnostic.getLocationAuthorizationStatus(); - switch (authorizationStatus) { case Diagnostic.permissionStatus.DENIED_ONCE: if (failOnDeniedOnce) { @@ -116,9 +116,21 @@ export class CoreGeolocationProvider { * * @param error Error. */ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - protected isCordovaPermissionDeniedError(error?: any): boolean { - return error && 'code' in error && 'PERMISSION_DENIED' in error && error.code === error.PERMISSION_DENIED; + protected isCordovaPermissionDeniedError(error?: CoreAnyError | GeolocationPositionError): boolean { + return !!error && + typeof error == 'object' && + 'code' in error && + 'PERMISSION_DENIED' in error && + error.code === error.PERMISSION_DENIED; + } + + /** + * Prechecks if it can request location services. + * + * @return If location can be requested. + */ + async canRequest(): Promise { + return CoreUtils.promiseWorks(Diagnostic.getLocationAuthorizationStatus()); } } @@ -141,3 +153,15 @@ export class CoreGeolocationError extends CoreError { } } + +/** + * Imported interface type from Web api. + * https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError + */ +interface GeolocationPositionError { + code: number; + message: string; + PERMISSION_DENIED: number; // eslint-disable-line @typescript-eslint/naming-convention + POSITION_UNAVAILABLE: number; // eslint-disable-line @typescript-eslint/naming-convention + TIMEOUT: number; // eslint-disable-line @typescript-eslint/naming-convention +};