Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. import {Component} from '@angular/core';
  2. import {FpBaseComponent} from '../utils/utils';
  3. import {Http} from "@angular/http";
  4. import {ProfileService} from "../../services/profile-service";
  5. import {AlertController} from "ionic-angular";
  6. import {TranslateService} from "@ngx-translate/core";
  7. import {File} from '@ionic-native/file';
  8. import {Device} from "@ionic-native/device";
  9. import exampleProfilesJson from '../../assets/json/profile.index.json';
  10. import {isNullOrUndefined} from "util";
  11.  
  12. @Component({
  13. selector: 'fp-example-profiles',
  14. templateUrl: 'fp-example-profiles.html',
  15. inputs: ['data']
  16. })
  17. export class FpExampleProfilesComponent extends FpBaseComponent {
  18.  
  19. exampleProfiles: any = exampleProfilesJson;
  20. folderPath: string;
  21.  
  22. constructor(private http: Http, private profileService: ProfileService, private alertCtrl: AlertController, private translate: TranslateService, private file: File, private device: Device) {
  23. super();
  24. }
  25.  
  26. public showConfirmation(selectedValue: string) {
  27. this.retrieveTranslationForAlert().then((translationsList) => {
  28. let alert = this.alertCtrl.create({
  29. title: translationsList[0],
  30. message: translationsList[1],
  31. buttons: [
  32. {
  33. text: translationsList[2],
  34. handler: () => {
  35. this.changeProfile(selectedValue)
  36. }
  37. },
  38. {
  39. text: translationsList[3]
  40. }]
  41. });
  42. alert.present();
  43. });
  44. }
  45.  
  46. public changeProfile(selectedValue: string) {
  47. this.http.get(selectedValue).toPromise().then((response) => {
  48. this.profileService.mergeProfile(response.json());
  49. });
  50. }
  51.  
  52.  
  53. public changeExampleProfilePath() {
  54. let baseDir: string;
  55. if (this.device.platform == "Android") {
  56. baseDir = this.file.externalRootDirectory;
  57. } else {
  58. baseDir = this.file.cacheDirectory;
  59. }
  60.  
  61. this.file.checkDir(baseDir, this.folderPath)
  62. .then(() => {
  63. this.handleCheckDir(baseDir);
  64. })
  65. .catch((error) => {
  66. if (this.folderPath == "" || isNullOrUndefined(this.folderPath)) {
  67. this.handleCheckDir(baseDir,true);
  68. }
  69. console.log("Error checking dir: ", error);
  70. });
  71. }
  72.  
  73. private handleCheckDir(baseDir: string, noFolder?: boolean) {
  74. let fullDir;
  75. if(noFolder){
  76. fullDir = baseDir;
  77. } else {
  78. fullDir = baseDir + "/" + this.folderPath;
  79. }
  80.  
  81. this.file.resolveDirectoryUrl(fullDir).then(() => {
  82. this.file.readAsText(fullDir, "profile.index.json")
  83. .then((fileText) => {
  84. this.exampleProfiles = JSON.parse(fileText);
  85. })
  86. .catch((error) => {
  87. this.errorAlert("The index file doesn't exist! Please create it.");
  88. console.log("Error: Could not getFile: ", error);
  89. });
  90. }).catch((error) => {
  91. this.errorAlert("This directory doesn't exist! Please create the folder you selected or select another.");
  92. console.log("Error: Could not resolveDirectoryUrl: ", error);
  93. });
  94. }
  95.  
  96. private errorAlert(message: string) {
  97. let alert = this.alertCtrl.create({
  98. title: "Error",
  99. message: message,
  100. buttons: [
  101. {
  102. text: "Ok",
  103. }]
  104. });
  105. alert.present();
  106. }
  107.  
  108. private retrieveTranslationForAlert(): Promise<Array<string>> {
  109. let translations = new Array<string>();
  110. let promise = new Promise<Array<string>>((resolve, reject) => {
  111. this.translate.get("i18.settings.select-example-profile-prompt.title").toPromise().then((alertTitle) => {
  112. translations.push(alertTitle);
  113. this.translate.get("i18.settings.select-example-profile-prompt.message").toPromise().then((alertMessage) => {
  114. translations.push(alertMessage);
  115. this.translate.get("i18.settings.select-example-profile-prompt.yes").toPromise().then((alertYes) => {
  116. translations.push(alertYes);
  117. this.translate.get("i18.settings.select-example-profile-prompt.no").toPromise().then((alertNo) => {
  118. translations.push(alertNo);
  119. resolve(translations);
  120. });
  121. });
  122. });
  123. });
  124. });
  125. return promise;
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement