/* * 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. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ 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 { SCSetting, SCSettingMeta, SCTranslations, } from '@openstapps/core'; import {SettingsProvider} from '../settings.provider'; @Component({ selector: 'stapps-settings-item', templateUrl: 'settings-item.html', }) export class SettingsItemComponent { isVisible = true; // limit to languages that are available in StApps Core language: keyof SCTranslations; meta = SCSettingMeta; @Input() setting: SCSetting; constructor(private alertCtrl: AlertController, private translateService: TranslateService, private settingsProvider: SettingsProvider, private geoLocation: Geolocation) { this.meta = SCSettingMeta; this.language = translateService.currentLang as keyof SCTranslations; translateService.onLangChange.subscribe((event: LangChangeEvent) => { this.isVisible = false; this.language = event.lang as keyof SCTranslations; // workaround for selected 'select option' not updating translation setTimeout(() => this.isVisible = true); }); } /** * 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 { 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; 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); } } typeOf(val: any) { return typeof (val); } }