Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Component} from '@angular/core';
- import {FpBaseComponent} from '../utils/utils';
- import {Http} from "@angular/http";
- import {ProfileService} from "../../services/profile-service";
- import {AlertController} from "ionic-angular";
- import {TranslateService} from "@ngx-translate/core";
- import {File} from '@ionic-native/file';
- import {Device} from "@ionic-native/device";
- import exampleProfilesJson from '../../assets/json/profile.index.json';
- import {isNullOrUndefined} from "util";
- @Component({
- selector: 'fp-example-profiles',
- templateUrl: 'fp-example-profiles.html',
- inputs: ['data']
- })
- export class FpExampleProfilesComponent extends FpBaseComponent {
- exampleProfiles: any = exampleProfilesJson;
- folderPath: string;
- constructor(private http: Http, private profileService: ProfileService, private alertCtrl: AlertController, private translate: TranslateService, private file: File, private device: Device) {
- super();
- }
- public showConfirmation(selectedValue: string) {
- this.retrieveTranslationForAlert().then((translationsList) => {
- let alert = this.alertCtrl.create({
- title: translationsList[0],
- message: translationsList[1],
- buttons: [
- {
- text: translationsList[2],
- handler: () => {
- this.changeProfile(selectedValue)
- }
- },
- {
- text: translationsList[3]
- }]
- });
- alert.present();
- });
- }
- public changeProfile(selectedValue: string) {
- this.http.get(selectedValue).toPromise().then((response) => {
- this.profileService.mergeProfile(response.json());
- });
- }
- public changeExampleProfilePath() {
- let baseDir: string;
- if (this.device.platform == "Android") {
- baseDir = this.file.externalRootDirectory;
- } else {
- baseDir = this.file.cacheDirectory;
- }
- this.file.checkDir(baseDir, this.folderPath)
- .then(() => {
- this.handleCheckDir(baseDir);
- })
- .catch((error) => {
- if (this.folderPath == "" || isNullOrUndefined(this.folderPath)) {
- this.handleCheckDir(baseDir,true);
- }
- console.log("Error checking dir: ", error);
- });
- }
- private handleCheckDir(baseDir: string, noFolder?: boolean) {
- let fullDir;
- if(noFolder){
- fullDir = baseDir;
- } else {
- fullDir = baseDir + "/" + this.folderPath;
- }
- this.file.resolveDirectoryUrl(fullDir).then(() => {
- this.file.readAsText(fullDir, "profile.index.json")
- .then((fileText) => {
- this.exampleProfiles = JSON.parse(fileText);
- })
- .catch((error) => {
- this.errorAlert("The index file doesn't exist! Please create it.");
- console.log("Error: Could not getFile: ", error);
- });
- }).catch((error) => {
- this.errorAlert("This directory doesn't exist! Please create the folder you selected or select another.");
- console.log("Error: Could not resolveDirectoryUrl: ", error);
- });
- }
- private errorAlert(message: string) {
- let alert = this.alertCtrl.create({
- title: "Error",
- message: message,
- buttons: [
- {
- text: "Ok",
- }]
- });
- alert.present();
- }
- private retrieveTranslationForAlert(): Promise<Array<string>> {
- let translations = new Array<string>();
- let promise = new Promise<Array<string>>((resolve, reject) => {
- this.translate.get("i18.settings.select-example-profile-prompt.title").toPromise().then((alertTitle) => {
- translations.push(alertTitle);
- this.translate.get("i18.settings.select-example-profile-prompt.message").toPromise().then((alertMessage) => {
- translations.push(alertMessage);
- this.translate.get("i18.settings.select-example-profile-prompt.yes").toPromise().then((alertYes) => {
- translations.push(alertYes);
- this.translate.get("i18.settings.select-example-profile-prompt.no").toPromise().then((alertNo) => {
- translations.push(alertNo);
- resolve(translations);
- });
- });
- });
- });
- });
- return promise;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement