feat: add permission check for geoLocation setting

This commit is contained in:
Sebastian Lange
2019-01-10 13:39:43 +01:00
parent cd5ac500a0
commit d5fa2fd9a5
9 changed files with 336 additions and 336 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -13,6 +13,8 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Component, Input} from '@angular/core';
import {Geolocation} from '@ionic-native/geolocation/ngx';
import {AlertController} from '@ionic/angular';
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {
SCLanguageName,
@@ -32,8 +34,10 @@ export class SettingsItemComponent {
@Input() setting: SCSetting;
constructor(private translateService: TranslateService,
private settingsProvider: SettingsProvider) {
constructor(private alertCtrl: AlertController,
private translateService: TranslateService,
private settingsProvider: SettingsProvider,
private geoLocation: Geolocation) {
this.meta = SCSettingMeta;
this.language = translateService.currentLang as SCLanguageName;
@@ -47,19 +51,80 @@ export class SettingsItemComponent {
}
/**
* handles value changes of the setting
* Checks for user permission to use location,
* if no permission is granted, setting is set to false and an alert is presented to the user
*/
private async checkGeoLocationPermission() {
// request geoLocation to test the user permission
try {
// set enableHighAccuracy, otherwise android platform does not respond
const options = {
enableHighAccuracy: true,
};
await this.geoLocation.getCurrentPosition(options);
} catch (error) {
// if error code is 1 the user denied permission,
// other errors like 'timeout' or 'no location' will be ignored here
if (error.code === 1) {
// ios has special error message for disabled location services, for the setting we ignore it
if (error.message.toLowerCase() !== 'location services are disabled.') {
// revert setting value
this.setting.input.value = false;
await this.presentGeoLocationAlert();
}
}
}
}
/**
* Shows alert with error message on denied user permission or disabled location services
*/
private async presentGeoLocationAlert() {
const title = this.translateService.instant('settings.geoLocation.permission_denied_title');
const message = this.translateService.instant('settings.geoLocation.permission_denied_message');
await this.presentAlert(title, message);
}
/**
* Shows alert with given title and message and a 'ok' button
*
* @param title title of the alert
* @param message message of the alert
*/
async presentAlert(title: string, message: string) {
const alert = await this.alertCtrl.create({
buttons: ['OK'],
header: title,
message: message,
});
await alert.present();
}
/**
* Handles value changes of the setting
*/
async settingChanged(): Promise<void> {
switch (this.setting.name) {
case 'language':
if (this.setting.input.value !== undefined) {
if (typeof this.setting.input.value !== 'undefined'
&& SettingsProvider.validateValue(this.setting, this.setting.input.value)) {
// handle general settings, with special actions
switch (this.setting.name) {
case 'language':
this.translateService.use(this.setting.input.value.toString());
}
break;
default:
break;
case 'geoLocation':
if (this.setting.input.value) {
await this.checkGeoLocationPermission();
}
break;
default:
}
await this.settingsProvider
.setSettingValue(this.setting.categories[0], this.setting.name, this.setting.input.value);
} else {
// reset setting
this.setting.input.value =
await this.settingsProvider.getSettingValue(this.setting.categories[0], this.setting.name);
}
await this.settingsProvider
.setSettingValue(this.setting.categories[0], this.setting.name, this.setting.input.value);
}
typeOf(val: any) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -16,6 +16,7 @@ import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {RouterModule, Routes} from '@angular/router';
import {Geolocation} from '@ionic-native/geolocation/ngx';
import {IonicModule} from '@ionic/angular';
import {TranslateModule} from '@ngx-translate/core';
@@ -39,6 +40,7 @@ const settingsRoutes: Routes = [
RouterModule.forChild(settingsRoutes),
],
providers: [
Geolocation,
],
})
export class SettingsModule {}

View File

@@ -293,7 +293,7 @@ describe('SettingsProvider', () => {
inputType: 'singleChoice',
values: [true, false],
},
name: 'locationService',
name: 'geoLocation',
order: 0,
origin: {
indexed: '2018-09-11T12:30:00Z',

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018, 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.