File

src/app/settings/permission/permission.component.ts

Implements

OnInit

Metadata

Index

Properties
Methods

Constructor

constructor(commonUtilService: CommonUtilService, scannerService: SunbirdQRScanner, permission: AndroidPermissionsService, appGlobalService: AppGlobalService, headerService: AppHeaderService, event: Events, telemetryGeneratorService: TelemetryGeneratorService, location: Location, appVersion: AppVersion, router: Router, platform: Platform, route: ActivatedRoute)
Parameters :
Name Type Optional
commonUtilService CommonUtilService No
scannerService SunbirdQRScanner No
permission AndroidPermissionsService No
appGlobalService AppGlobalService No
headerService AppHeaderService No
event Events No
telemetryGeneratorService TelemetryGeneratorService No
location Location No
appVersion AppVersion No
router Router No
platform Platform No
route ActivatedRoute No

Methods

generateInteractEvent
generateInteractEvent(permissionAllowed: boolean)
Parameters :
Name Type Optional
permissionAllowed boolean No
Returns : void
getNavParams
getNavParams()
Returns : void
grantAccess
grantAccess()
Returns : void
handleBackButton
handleBackButton()
Returns : void
handleHeaderEvents
handleHeaderEvents($event)
Parameters :
Name Optional
$event No
Returns : void
Async ionViewWillEnter
ionViewWillEnter()
Returns : any
ionViewWillLeave
ionViewWillLeave()
Returns : void
ngOnInit
ngOnInit()
Returns : void
Private Async requestAppPermissions
requestAppPermissions()
Returns : unknown
skipAccess
skipAccess()
Returns : void
stateChange
stateChange(event)
Parameters :
Name Optional
event No
Returns : void

Properties

appName
backButtonFunc
Type : Subscription
changePermissionAccess
Default value : false
Public commonUtilService
Type : CommonUtilService
headerObservable
Type : any
Private navParams
Type : any
Readonly permissionList
Type : []
Default value : [ AndroidPermission.CAMERA, AndroidPermission.WRITE_EXTERNAL_STORAGE, AndroidPermission.RECORD_AUDIO ]
permissionListDetails
Type : any
showProfileSettingPage
Default value : false
showTabsPage
Default value : false
import { Location } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { RouterLinks } from '@app/app/app.constant';
import { AndroidPermission, AndroidPermissionsStatus, PermissionAskedEnum } from '@app/services/android-permissions/android-permission';
import { AndroidPermissionsService } from '@app/services/android-permissions/android-permissions.service';
import { AppGlobalService } from '@app/services/app-global-service.service';
import { AppHeaderService } from '@app/services/app-header.service';
import { CommonUtilService } from '@app/services/common-util.service';
import { SunbirdQRScanner } from '@app/services/sunbirdqrscanner.service';
import { Environment, InteractSubtype, InteractType, PageId } from '@app/services/telemetry-constants';
import { TelemetryGeneratorService } from '@app/services/telemetry-generator.service';
import { AppVersion } from '@ionic-native/app-version/ngx';
import { Platform } from '@ionic/angular';
import { Events } from '@app/util/events';
import { of, Subscription } from 'rxjs';
import { mergeMap } from 'rxjs/operators';

declare const cordova;

@Component({
  selector: 'app-permission',
  templateUrl: './permission.component.html',
  styleUrls: ['./permission.component.scss'],
})
export class PermissionComponent implements OnInit {

  appName;

  permissionListDetails: any;

  readonly permissionList = [
    AndroidPermission.CAMERA,
    AndroidPermission.WRITE_EXTERNAL_STORAGE,
    AndroidPermission.RECORD_AUDIO
  ];

  changePermissionAccess = false;
  showProfileSettingPage = false;
  showTabsPage = false;
  headerObservable: any;
  private navParams: any;
  backButtonFunc: Subscription;

  constructor(
    public commonUtilService: CommonUtilService,
    private scannerService: SunbirdQRScanner,
    private permission: AndroidPermissionsService,
    private appGlobalService: AppGlobalService,
    private headerService: AppHeaderService,
    private event: Events,
    private telemetryGeneratorService: TelemetryGeneratorService,
    private location: Location,
    private appVersion: AppVersion,
    private router: Router,
    private platform: Platform,
    private route: ActivatedRoute
  ) {
    this.appVersion.getAppName().then((appName: string) => {
      this.appName = appName;
      this.permissionListDetails = [
        {
          title: this.commonUtilService.translateMessage('CAMERA'),
          path: './assets/imgs/ic_photo_camera.png',
          description: this.commonUtilService.translateMessage('CAMERA_PERMISSION_DESCRIPTION', this.appName),
          permission: false
        },
        {
          title: this.commonUtilService.translateMessage('FILE_MANAGER'),
          path: './assets/imgs/ic_folder_open.png',
          description: this.commonUtilService.translateMessage('FILE_MANAGER_PERMISSION_DESCRIPTION', this.appName),
          permission: false
        },
        {
          title: this.commonUtilService.translateMessage('MICROPHONE'),
          path: './assets/imgs/ic_keyboard_voice.png',
          description: this.commonUtilService.translateMessage('MICROPHONE_PERMISSION_DESCRIPTION', this.appName),
          permission: false
        }
      ];
    });

    this.route.queryParams.subscribe(params => {
      this.getNavParams();
    });
  }

  getNavParams() {
    this.navParams = this.router.getCurrentNavigation().extras.state;
    console.log(this.navParams);
  }

  async ionViewWillEnter() {
    this.permission.checkPermissions(this.permissionList).subscribe((res: { [key: string]: AndroidPermissionsStatus }) => {
      this.permissionListDetails[0].permission = res[AndroidPermission.CAMERA].hasPermission;
      this.permissionListDetails[1].permission = res[AndroidPermission.WRITE_EXTERNAL_STORAGE].hasPermission;
      this.permissionListDetails[2].permission = res[AndroidPermission.RECORD_AUDIO].hasPermission;
    });

    if (this.navParams) {
      this.changePermissionAccess = Boolean(this.navParams.changePermissionAccess);
      this.showProfileSettingPage = Boolean(this.navParams.showProfileSettingPage);
      this.showTabsPage = Boolean(this.navParams.showTabsPage);
    }

    this.headerService.showHeaderWithBackButton();
    this.event.subscribe('event:showScanner', (data) => {
      if (data.pageName === PageId.PERMISSION) {
        this.scannerService.startScanner(PageId.PERMISSION, true);
      }
    });

    this.headerObservable = this.headerService.headerEventEmitted$.subscribe(eventName => {
      this.handleHeaderEvents(eventName);
    });
    this.handleBackButton();
  }

  handleBackButton() {
    this.backButtonFunc = this.platform.backButton.subscribeWithPriority(10, () => {
      this.telemetryGeneratorService.generateBackClickedTelemetry(PageId.PERMISSION, Environment.ONBOARDING, false);
      this.location.back();
      this.backButtonFunc.unsubscribe();
    });
  }

  ngOnInit() {
    this.telemetryGeneratorService.generatePageViewTelemetry(PageId.PERMISSION,
      Environment.ONBOARDING, '');
  }

  ionViewWillLeave() {
    if (this.backButtonFunc) {
      this.backButtonFunc.unsubscribe();
    }

    if (this.headerObservable) {
      this.headerObservable.unsubscribe();
    }
  }

  grantAccess() {
    this.appGlobalService.setIsPermissionAsked(PermissionAskedEnum.isCameraAsked, true);
    this.appGlobalService.setIsPermissionAsked(PermissionAskedEnum.isRecordAudioAsked, true);
    this.appGlobalService.setIsPermissionAsked(PermissionAskedEnum.isStorageAsked, true);
    this.generateInteractEvent(true);
    // If user given camera access and the showScannerPage is ON
    this.requestAppPermissions().then((status) => {
      if (this.showProfileSettingPage) {
        // check if profileSetting page config. is ON
        const navigationExtras: NavigationExtras = { state: { hideBackButton: false } };
        this.router.navigate([`/${RouterLinks.PROFILE_SETTINGS}`], navigationExtras);
      } else {
        const navigationExtras: NavigationExtras = { state: { loginMode: 'guest' } };
        this.router.navigate(['/tabs'], navigationExtras);
      }
    });
  }

  skipAccess() {
    this.generateInteractEvent(false);
    if (this.showProfileSettingPage) {
      const navigationExtras: NavigationExtras = { state: { hideBackButton: false } };
      this.router.navigate([`/${RouterLinks.PROFILE_SETTINGS}`], navigationExtras);
    } else {
      const navigationExtras: NavigationExtras = { state: { loginMode: 'guest' } };
      this.router.navigate(['/tabs'], navigationExtras);
    }
  }

  private async requestAppPermissions() {
    return this.permission.checkPermissions(this.permissionList).pipe(
      mergeMap((statusMap: { [key: string]: AndroidPermissionsStatus }) => {
        const toRequest: AndroidPermission[] = [];

        for (const permission in statusMap) {
          if (!statusMap[permission].hasPermission) {
            const values = new Map();
            values['permission'] = permission;
            values['permissionStatus'] = statusMap[permission];
            this.telemetryGeneratorService.generateInteractTelemetry(
              InteractType.OTHER,
              InteractSubtype.PERMISSION_POPUP,
              Environment.HOME,
              PageId.ONBOARDING_LANGUAGE_SETTING,
              undefined,
              values
            );
            toRequest.push(permission as AndroidPermission);
          }
        }

        if (!toRequest.length) {
          return of(undefined);
        }
        return this.permission.requestPermissions(toRequest);
      })
    ).toPromise();
  }

  handleHeaderEvents($event) {
    if ($event.name === 'back') {
      this.telemetryGeneratorService.generateBackClickedTelemetry(PageId.PERMISSION, Environment.ONBOARDING, true);
      this.location.back();
    }
  }

  generateInteractEvent(permissionAllowed: boolean) {
    const values = new Map();
    values['permissionAllowed'] = permissionAllowed;
    this.telemetryGeneratorService.generateInteractTelemetry(
      InteractType.TOUCH,
      (permissionAllowed) ? InteractSubtype.GRANT_ACCESS_CLICKED : InteractSubtype.SKIP_CLICKED,
      Environment.ONBOARDING,
      PageId.PERMISSION,
      undefined,
      values);
  }

  stateChange(event) {
    this.telemetryGeneratorService.generateInteractTelemetry(
      InteractType.TOUCH,
      InteractSubtype.APP_PERMISSION_SETTING_CLICKED,
      Environment.ONBOARDING,
      PageId.PERMISSION
    );
    this.location.back();
    cordova.plugins.diagnostic.switchToSettings('application_details', () => {
      console.log('opened settings');
    },
      (err) => {
        console.log('failed to open settings' + err);
      }
    );
  }
}
<ion-content class="ion-padding" *ngIf="!changePermissionAccess">
  <p class="permission-title">{{'PERMISSION_REQUIRED_TITLE' | translate: {'%s': appName} }}</p>
  <div class="permission-container">
    <div *ngFor="let item of permissionListDetails" class="item-row d-flex mb-16">
      <div class="icon-col pr-16">
        <img src="{{item?.path}}" alt="" />
      </div>
      <div class="pl-16">
        <p class="title m-0 mb-8">{{item?.title}}</p>
        <p class="description">{{item?.description}}</p>
      </div>
    </div>
  </div>
</ion-content>

<ion-footer class="ion-padding" *ngIf="!changePermissionAccess" style="background: white;">
  <ion-button expand="block" (click)="grantAccess()">{{'GRANT_ACCESS' | translate}}</ion-button>
  <ion-button expand="block" fill="outline" class="mt-8 btn-outline" (click)="skipAccess()">{{'NOT_NOW' | translate}}
  </ion-button>
</ion-footer>

<!-- For Edit permission page -->
<ion-content *ngIf="changePermissionAccess" class="changePermissionAccess ion-padding-vertical">
  <div>
    <p class="permission-title" class="pl-16">{{'PERMISSION_EDIT_TITLE' | translate:{'%s': appName} }}</p>
  </div>
  <div class="permission-container">
    <div *ngFor="let item of permissionListDetails" role="switch" tabindex="0" class="item-row mb-16 d-flex" (click)="stateChange($event)">
      <div style="flex-grow:1000;" class="pl-16">
        <p class="title m-0 mb-8">{{item?.title}}</p>
        <p class="description">{{item?.description}}</p>
      </div>
      <div class="toggle-col">
        <ion-toggle class="permission-toggle" color="success" mode="ios" [(ngModel)]="item.permission"
          >
        </ion-toggle>
      </div>
    </div>
  </div>
</ion-content>

./permission.component.scss

@import "src/assets/styles/variables";
@import "src/assets/styles/_custom-mixins";
:host {
  .permission-title {
    color: map-get($colors, primary_black);
    font-size: 1rem;
    line-height: 1.375rem;
  }
  .permission-container {
    .item-row {
      .icon-col {
        @include ltr() {
          border-right: 1px solid $gray-100;
        }

        @include rtl() {
          border-left: 1px solid $gray-100;
        }
        min-width: 2.813rem;
        text-align: end;

        ion-icon {
          font-size: 3rem;
        }
      }

      .title {
        font-size: $font-size-base;
        color: map-get($colors, primary_black);
        font-weight: bold;
        line-height: 1.188rem;
      }
      .description {
        line-height: 1.188rem;
        font-size: $font-size-base;
        color: $gray-400;
        margin: 0px !important;
      }
    }
  }

  .pl-16 {
    @include padding(null, null, null, 16px);
  }
  .pt-0 {
    @include padding(0 !important, null, null, null);
  }
  .pr-16 {
    @include padding(null, 16px, null, null);
  }

  .btn-outline {
    letter-spacing: normal;
    border-width: 0.063rem;
    --border-width: 0.063rem;
  }
}

.permission-toggle {
  --background: #{map-get($colors, light_gray_toggle) !important};
  --background-checked: #{map-get($colors, secondary)};
}

ion-toggle.ion-color-success.permission-toggle {
  --ion-color-base: #{map-get($colors, toggle_green) !important};
}

.toggle-inner {
  top: 0px !important;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""