MOBILE-3640 database: Location service improvements on error handling

main
Pau Ferrer Ocaña 2021-03-31 10:12:23 +02:00
parent 1363951920
commit bfbfd350b1
3 changed files with 35 additions and 8 deletions

View File

@ -11,7 +11,7 @@
<ion-input type="text" [formControlName]="'f_'+field.id+'_1'" maxlength="10"></ion-input> <ion-input type="text" [formControlName]="'f_'+field.id+'_1'" maxlength="10"></ion-input>
<span class="placeholder-icon" item-right>°E</span> <span class="placeholder-icon" item-right>°E</span>
</div> </div>
<div class="addon-data-latlong"> <div class="addon-data-latlong" *ngIf="locationServicesEnabled">
<ion-button (click)="getLocation($event)"> <ion-button (click)="getLocation($event)">
<ion-icon name="fas-crosshairs" slot="start"></ion-icon> <ion-icon name="fas-crosshairs" slot="start"></ion-icon>
{{ 'addon.mod_data.mylocation' | translate }} {{ 'addon.mod_data.mylocation' | translate }}

View File

@ -33,6 +33,7 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo
north?: number; north?: number;
east?: number; east?: number;
locationServicesEnabled = false;
constructor( constructor(
fb: FormBuilder, fb: FormBuilder,
@ -87,7 +88,7 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected init(): void { protected async init(): Promise<void> {
if (this.value) { if (this.value) {
this.updateValue(this.value); this.updateValue(this.value);
} }
@ -95,6 +96,8 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo
if (this.editMode) { if (this.editMode) {
this.addControl('f_' + this.field.id + '_0', this.north); this.addControl('f_' + this.field.id + '_0', this.north);
this.addControl('f_' + this.field.id + '_1', this.east); this.addControl('f_' + this.field.id + '_1', this.east);
this.locationServicesEnabled = await CoreGeolocation.canRequest();
} else if (this.searchMode) { } else if (this.searchMode) {
this.addControl('f_' + this.field.id); this.addControl('f_' + this.field.id);
} }
@ -136,7 +139,7 @@ export class AddonModDataFieldLatlongComponent extends AddonModDataFieldPluginCo
* *
* @param error Location error. * @param error Location error.
*/ */
protected showLocationErrorModal(error: CoreAnyError): void { protected showLocationErrorModal(error: CoreAnyError | CoreGeolocationError): void {
if (error instanceof CoreGeolocationError) { if (error instanceof CoreGeolocationError) {
CoreDomUtils.showErrorModal(this.getGeolocationErrorMessage(error), true); CoreDomUtils.showErrorModal(this.getGeolocationErrorMessage(error), true);

View File

@ -16,8 +16,9 @@ import { Injectable } from '@angular/core';
import { Coordinates } from '@ionic-native/geolocation'; import { Coordinates } from '@ionic-native/geolocation';
import { CoreApp } from '@services/app'; 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 { Geolocation, Diagnostic, makeSingleton } from '@singletons';
import { CoreUtils } from './utils/utils';
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class CoreGeolocationProvider { export class CoreGeolocationProvider {
@ -89,7 +90,6 @@ export class CoreGeolocationProvider {
*/ */
protected async doAuthorizeLocation(failOnDeniedOnce: boolean = false): Promise<void> { protected async doAuthorizeLocation(failOnDeniedOnce: boolean = false): Promise<void> {
const authorizationStatus = await Diagnostic.getLocationAuthorizationStatus(); const authorizationStatus = await Diagnostic.getLocationAuthorizationStatus();
switch (authorizationStatus) { switch (authorizationStatus) {
case Diagnostic.permissionStatus.DENIED_ONCE: case Diagnostic.permissionStatus.DENIED_ONCE:
if (failOnDeniedOnce) { if (failOnDeniedOnce) {
@ -116,9 +116,21 @@ export class CoreGeolocationProvider {
* *
* @param error Error. * @param error Error.
*/ */
// eslint-disable-next-line @typescript-eslint/no-explicit-any protected isCordovaPermissionDeniedError(error?: CoreAnyError | GeolocationPositionError): boolean {
protected isCordovaPermissionDeniedError(error?: any): boolean { return !!error &&
return error && 'code' in error && 'PERMISSION_DENIED' in error && error.code === error.PERMISSION_DENIED; 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<boolean> {
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
};