src/app/settings/data-sync/data-sync.component.ts
                OnInit
                OnDestroy
    
| selector | app-data-sync | 
            
| styleUrls | ./data-sync.component.scss | 
            
| templateUrl | ./data-sync.component.html | 
            
                        Properties | 
                
                        
  | 
                
                        Methods | 
                
                        
  | 
                
constructor(telemetryService: TelemetryService, archiveService: ArchiveService, zone: NgZone, changeDetectionRef: ChangeDetectorRef, social: SocialSharing, commonUtilService: CommonUtilService, telemetryGeneratorService: TelemetryGeneratorService, location: Location, platform: Platform, appHeaderService: AppHeaderService)
                     | 
                |||||||||||||||||||||||||||||||||
| 
                             
                                    Parameters :
                                     
                    
  | 
                
| Private generateInteractEvent | 
                    
                    generateInteractEvent(interactType: string, subtype: string, size: number)
                 | 
            
| 
                    
                     
                        Returns :          
                    void
                     | 
            
| Private generateSyncTypeInteractTelemetry | ||||||
                    
                    generateSyncTypeInteractTelemetry(dataSyncType: string)
                 | 
            ||||||
| 
                     
                        Parameters :
                        
                         
                    
 
                        Returns :          
                    void
                     | 
            
| Private handleBackButton | 
                    
                    handleBackButton()
                 | 
            
| 
                     
                        Returns :          
                void
                     | 
            
| Private handleNavBackButton | ||||||
                    
                    handleNavBackButton(eventName: any)
                 | 
            ||||||
| 
                     
                        Parameters :
                        
                         
                    
 
                        Returns :          
                    void
                     | 
            
| Private Async init | 
                    
                    init()
                 | 
            
| 
                     
                        Returns :          
                any
                     | 
            
| ionViewWillEnter | 
ionViewWillEnter()
                 | 
            
| 
                     
                        Returns :          
                void
                     | 
            
| ngOnDestroy | 
ngOnDestroy()
                 | 
            
| 
                     
                        Returns :          
                void
                     | 
            
| ngOnInit | 
ngOnInit()
                 | 
            
| 
                     
                        Returns :          
                void
                     | 
            
| onSelected | 
onSelected()
                 | 
            
| 
                     
                        Returns :          
                void
                     | 
            
| Async onSyncClick | 
                    
                    onSyncClick()
                 | 
            
| 
                     
                        Returns :          
                any
                     | 
            
| Async shareTelemetry | 
                    
                    shareTelemetry()
                 | 
            
| 
                     
                        Returns :      
                unknown
                     | 
            
| backButtonFunc | 
                        Type :     Subscription
                     | 
                
| Optional dataSyncType | 
                        Type :         TelemetryAutoSyncModes
                     | 
                
| headerObservable | 
                        Type :         any
                     | 
                
| Optional lastSyncDateTime | 
                        Type :     Observable<string | undefined>
                     | 
                
| loader | 
                        Type :         any
                     | 
                
| OPTIONS | 
                        Default value : TelemetryAutoSyncModes
                     | 
                
| Public zone | 
                        Type :     NgZone
                     | 
                
import { AppHeaderService } from './../../../services/app-header.service';
import { Location } from '@angular/common';
import { ChangeDetectorRef, Component, Inject, NgZone, OnInit, OnDestroy } from '@angular/core';
import {
  CommonUtilService, Environment,
  ImpressionType,
  InteractSubtype, InteractType, PageId, TelemetryGeneratorService
} from '@app/services';
import { SocialSharing } from '@ionic-native/social-sharing/ngx';
import { Platform } from '@ionic/angular';
import { Observable, Subscription } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import {
  ArchiveObjectType, ArchiveService,
  ObjectNotFoundError, TelemetryAutoSyncModes, TelemetryImpressionRequest, TelemetryService
} from 'sunbird-sdk';
declare const cordova;
@Component({
  selector: 'app-data-sync',
  templateUrl: './data-sync.component.html',
  styleUrls: ['./data-sync.component.scss'],
})
export class DataSyncComponent implements OnInit, OnDestroy {
  lastSyncDateTime?: Observable<string | undefined>;
  dataSyncType?: TelemetryAutoSyncModes;
  OPTIONS = TelemetryAutoSyncModes;
  backButtonFunc: Subscription;
  loader: any;
  headerObservable: any;
  constructor(
    @Inject('TELEMETRY_SERVICE') private telemetryService: TelemetryService,
    @Inject('ARCHIVE_SERVICE') private archiveService: ArchiveService,
    public zone: NgZone,
    private changeDetectionRef: ChangeDetectorRef,
    private social: SocialSharing,
    private commonUtilService: CommonUtilService,
    private telemetryGeneratorService: TelemetryGeneratorService,
    private location: Location,
    private platform: Platform,
    private appHeaderService: AppHeaderService
  ) {
    this.lastSyncDateTime = this.telemetryService.lastSyncedTimestamp().pipe(
      map((ts) => {
        if (ts) {
          return window.dayjs(ts).format('DD/MM/YYYY, hh:mm a');
        }
        return undefined;
      }),
      tap(() => {
        this.changeDetectionRef.detectChanges();
      })
    );
  }
  ngOnDestroy(): void {
    if (this.headerObservable) {
      this.headerObservable.unsubscribe();
    }
  }
  private async init() {
    this.zone.run(async () => {
      this.dataSyncType = (
        await this.telemetryService.autoSync.getSyncMode().toPromise() as TelemetryAutoSyncModes | undefined
      ) || TelemetryAutoSyncModes.ALWAYS_ON;
    });
  }
  ngOnInit() {
    this.init();
    const telemetryImpressionRequest = new TelemetryImpressionRequest();
    telemetryImpressionRequest.type = ImpressionType.VIEW;
    telemetryImpressionRequest.pageId = PageId.SETTINGS_DATASYNC;
    telemetryImpressionRequest.env = Environment.SETTINGS;
    this.telemetryGeneratorService.generateImpressionTelemetry(
      ImpressionType.VIEW, '',
      PageId.SETTINGS_DATASYNC,
      Environment.SETTINGS, '', '', ''
    );
    this.handleBackButton();
  }
  ionViewWillEnter() {
    this.headerObservable = this.appHeaderService.headerEventEmitted$.subscribe(eventName => {
      this.handleNavBackButton(eventName);
    });
  }
  onSelected() {
    if (this.dataSyncType) {
      this.generateSyncTypeInteractTelemetry(this.dataSyncType);
      this.telemetryService.autoSync.setSyncMode(this.dataSyncType).toPromise();
    }
  }
  private generateSyncTypeInteractTelemetry(dataSyncType: string) {
    const value = new Map();
    value['dataSyncType'] = dataSyncType;
    this.telemetryGeneratorService.generateInteractTelemetry(
      InteractType.TOUCH,
      InteractSubtype.DATA_SYNC_TYPE,
      Environment.SETTINGS,
      PageId.SETTINGS_DATASYNC,
      undefined,
      value
    );
  }
  async shareTelemetry() {
    const loader = await this.commonUtilService.getLoader();
    await loader.present();
    this.telemetryGeneratorService.generateInteractTelemetry(InteractType.TOUCH,
      InteractSubtype.SHARE_TELEMETRY_CLICKED,
      Environment.SETTINGS,
      PageId.SETTINGS_DATASYNC);
    try {
      await this.telemetryService.sync({
        ignoreAutoSyncMode: false,
        ignoreSyncThreshold: true
      }).toPromise();
    } catch (e) {
    }
    
    const folderPath = this.platform.is('ios') ? cordova.file.cacheDirectory : cordova.file.externalDataDirectory;
    return this.archiveService.export(
      {
        objects: [{ type: ArchiveObjectType.TELEMETRY }],
        filePath: folderPath + '/tmp'
      })
      .toPromise()
      .then(async (r) => {
        await loader.dismiss();
        return this.social.share('', '', r.filePath, '');
      })
      .catch(async (e) => {
        await loader.dismiss();
        if (e instanceof ObjectNotFoundError) {
          this.commonUtilService.showToast('SHARE_TELEMETRY_NO_DATA_FOUND');
        } else {
          this.commonUtilService.showToast('SHARE_TELEMETRY_FAILED');
        }
      });
  }
  async onSyncClick() {
    this.loader = await this.commonUtilService.getLoader();
    await this.loader.present();
    this.generateInteractEvent(InteractType.TOUCH, InteractSubtype.SYNC_NOW_CLICKED, null);
    this.telemetryService.sync({
      ignoreAutoSyncMode: true,
      ignoreSyncThreshold: true
    }).subscribe();
    sbsync.onSyncSucces(async (syncStat) => {
      if (syncStat.telemetry_error) {
        if (this.loader) {
          await this.loader.dismiss();
        }
        this.commonUtilService.showToast('DATA_SYNC_FAILURE');
        return;
      } else if (!syncStat.syncedEventCount) {
        if (this.loader) {
          await this.loader.dismiss();
        }
        this.commonUtilService.showToast('DATA_SYNC_NOTHING_TO_SYNC');
        return;
      }
      this.generateInteractEvent(InteractType.OTHER, InteractSubtype.MANUALSYNC_SUCCESS, syncStat.syncedFileSize);
      if (this.loader) {
        await this.loader.dismiss();
      }
      this.commonUtilService.showToast('DATA_SYNC_SUCCESSFUL');
    }, async (error) => {
      if (this.loader) {
        await this.loader.dismiss();
      }
      this.commonUtilService.showToast('DATA_SYNC_FAILURE');
    });
  }
  private generateInteractEvent(interactType: string, subtype: string, size: number) {
    /*istanbul ignore else */
      this.telemetryGeneratorService.generateInteractTelemetry(
        interactType,
        subtype,
        Environment.SETTINGS,
        PageId.SETTINGS_DATASYNC,
        undefined,
        size ? { SizeOfFileInKB: (size / 1000) + '' } : undefined
      );
  }
  private handleBackButton() {
    this.backButtonFunc = this.platform.backButton.subscribeWithPriority(10, () => {
      this.telemetryGeneratorService.generateBackClickedTelemetry(PageId.SETTINGS_DATASYNC, Environment.SETTINGS, false);
      this.location.back();
      this.backButtonFunc.unsubscribe();
    });
  }
  private handleNavBackButton(eventName: any) {
    if (eventName.name === 'back') {
      this.telemetryGeneratorService.generateBackClickedNewTelemetry(false, Environment.SETTINGS, PageId.SETTINGS_DATASYNC);
      this.location.back();
    }
  }
}
    <ion-content class="ion-padding">
  <div class="label PT16" role="heading" aria-level="1">
    {{ 'AUTOMATIC_SYNC' | translate }}
  </div>
  <ion-list lines="none" (ionChange)="onSelected()">
    <ion-radio-group [(ngModel)]="dataSyncType">
      <ion-item class="ds-item">
        <ion-radio class="ds-m0" [value]="OPTIONS.OFF" slot="start"></ion-radio>
        <ion-label class="ds-m0">{{ 'OFF' | translate }}</ion-label>
      </ion-item>
      <ion-item class="ds-item">
        <ion-radio class="ds-m0" [value]="OPTIONS.OVER_WIFI" slot="start"></ion-radio>
        <ion-label class="ds-m0">{{ 'OVER_WIFI' | translate }}</ion-label>
      </ion-item>
      <ion-item class="ds-item">
        <ion-radio class="ds-m0" [value]="OPTIONS.ALWAYS_ON" slot="start"></ion-radio>
        <ion-label class="ds-m0">{{ 'ALWAYS_ON' | translate }}</ion-label>
      </ion-item>
    </ion-radio-group>
  </ion-list>
  <ion-label *ngIf="(lastSyncDateTime | async) as lastSyncDateTime">
    {{ 'LAST_SYNC' | translate }} {{lastSyncDateTime}}
  </ion-label>
  <ion-button expand="block" class="MT16 btn" (click)="onSyncClick()">
    {{ 'SYNC_NOW' | translate }}
  </ion-button>
  <hr>
  <ion-button (click)="shareTelemetry()" class="ion-no-padding btn" fill="clear">
    {{ 'SHARE_TELEMETRY' | translate }}
  </ion-button>
</ion-content>
    
                    ./data-sync.component.scss
                
@import "src/assets/styles/_variables.scss";
.label {
    font-size: 0.938rem;
}
.ds-m0 {
    margin-top: 0px !important;
    margin-bottom: 0px !important;
    font-size: 1rem;
}
.ds-item{
    --padding-start: 0px
}
hr {
    -webkit-margin-before: 16px;
    height: 0.125rem;
    border-width: 0;
    -webkit-box-sizing: content-box;
    box-sizing: content-box;
    background-color: map-get($colors, medium_gray);
}
.item-block {
    min-height: unset;
    height: 3rem;
}
.item-radio input[type="radio"] {
    display: none;
}
ion-radio {
    width: 3rem;
    height: 3rem;
}
.btn {
    font-size: 0.875rem;
}
.item-md {
    padding: 0 !important;
}