File

src/app/manage-learn/core/services/sharing-feature.service.ts

Index

Properties
Methods

Constructor

constructor(fileChooser: FileChooser, file: File, socialSharing: SocialSharing, fileTransfer: FileTransfer, toast: ToastService, platform: Platform, utils: UtilsService, alertController: AlertController, loader: LoaderService, filePath: FilePath, unnatiSrvc: UnnatiDataService, translate: TranslateService, androidPermissions: AndroidPermissions, fileOpener: FileOpener, network: NetworkService)
Parameters :
Name Type Optional
fileChooser FileChooser No
file File No
socialSharing SocialSharing No
fileTransfer FileTransfer No
toast ToastService No
platform Platform No
utils UtilsService No
alertController AlertController No
loader LoaderService No
filePath FilePath No
unnatiSrvc UnnatiDataService No
translate TranslateService No
androidPermissions AndroidPermissions No
fileOpener FileOpener No
network NetworkService No

Methods

directoryPath
directoryPath()
Returns : string
Async getFileUrl
getFileUrl(config, name)
Parameters :
Name Optional
config No
name No
Returns : any
regularShare
regularShare(msg)
Parameters :
Name Optional
msg No
Returns : Promise<any>
requestPermission
requestPermission()
Returns : void
sharingThroughApp
sharingThroughApp(fileName?)
Parameters :
Name Optional
fileName Yes
Returns : void

Properties

Public alertController
Type : AlertController
Public fileOpener
Type : FileOpener
Public filePath
Type : FilePath
Public loader
Type : LoaderService
Public network
Type : NetworkService
texts
Type : any
Public unnatiSrvc
Type : UnnatiDataService
import { Injectable } from '@angular/core';
import { File } from "@ionic-native/file/ngx";
import { SocialSharing } from "@ionic-native/social-sharing/ngx";
import { FileChooser } from '@ionic-native/file-chooser/ngx';
import { LoaderService, ToastService, UtilsService } from '@app/app/manage-learn/core';
import { UnnatiDataService } from '@app/app/manage-learn/core/services/unnati-data.service';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
import { FilePath } from '@ionic-native/file-path/ngx';
import { FileTransfer } from '@ionic-native/file-transfer/ngx';
import { AlertController, Platform } from '@ionic/angular';
import { TranslateService } from '@ngx-translate/core';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { NetworkService } from './network.service';

@Injectable({
  providedIn: 'root',
})
export class SharingFeatureService {
  texts: any;

  constructor(
    private fileChooser: FileChooser,
    private file: File,
    private socialSharing: SocialSharing,
    private fileTransfer: FileTransfer,
    private toast: ToastService,
    private platform: Platform,
    private utils: UtilsService,
    public alertController: AlertController,
    public loader: LoaderService,
    public filePath: FilePath,
    public unnatiSrvc: UnnatiDataService,
    private translate: TranslateService,
    private androidPermissions: AndroidPermissions,
    public fileOpener: FileOpener,
    public network :NetworkService
  ) {
    console.log('Hello SharingFeaturesProvider Provider');
  }

  sharingThroughApp(fileName?) {
    let subject = 'hi';
    let link = 'google.com';
    let message = 'hi';
    if (!fileName) {
      this.fileChooser
        .open()
        .then((uri) => {
          (<any>window).FilePath.resolveNativePath(uri, (result) => {
            let fileName = result.split('/').pop();
            let path = result.substring(0, result.lastIndexOf('/') + 1);
            this.file
              .readAsDataURL(path, fileName)
              .then((base64File) => {
                this.socialSharing
                  .share(message, subject, base64File, link)
                  .then(() => {
                    console.log('share Success');
                  })
                  .catch(() => {
                    console.log('share Failure');
                  });
              })
              .catch(() => {
                console.log('Error reading file');
              });
          });
        })
        .catch((e) => console.log(e));
    } else {
      (<any>window).FilePath.resolveNativePath(fileName, (result) => {
        let fileName = result.split('/').pop();
        let path = result.substring(0, result.lastIndexOf('/') + 1);
        this.file
          .readAsDataURL(path, fileName)
          .then((base64File) => {
            this.socialSharing
              .share('', fileName, base64File, '')
              .then(() => {
                console.log('share Success');
              })
              .catch(() => {
                console.log('share Failure');
              });
          })
          .catch(() => {
            console.log('Error reading file');
          });
      });
    }
  }

  regularShare(msg): Promise<any> {
    return this.socialSharing.share(msg, null, null, null);
  }


  async getFileUrl(config, name) {
    if(this.network.isNetworkAvailable){
      this.loader.startLoader();
      if(name?.length > 40){
        name = name.slice(0, 40) + '...';
      }
      let res = await this.unnatiSrvc.get(config).toPromise();
      if (res.result && !res.result.data && !res.result.data.downloadUrl) {
        this.toast.showMessage(this.texts['FRMELEMENTS_MSG_ERROR_WHILE_DOWNLOADING'], 'danger');
        this.loader.stopLoader();
        return;
      }
      let fileName = name.replace(/[^A-Z0-9]/ig, "_") + '.pdf';
      const ft = this.fileTransfer.create();
      setTimeout(() => {
        ft.download(res.result.data.downloadUrl, this.directoryPath() + fileName)
        .then(
          (res) => {
            this.socialSharing.share(null, null, res.nativeURL, null).then(data =>{
            },error =>{
            })
          },
          (err) => {
            this.requestPermission();
            this.toast.showMessage(this.texts['FRMELEMENTS_MSG_ERROR_WHILE_DOWNLOADING'], 'danger');
          }
        )
        .finally(() => {
          this.loader.stopLoader();
        });
      }, 1000)
    }else{
      this.toast.showMessage('FRMELEMNTS_MSG_OFFLINE', 'danger');
    }
   
  }
  directoryPath(): string {
    if (this.platform.is('ios')) {
      return this.file.documentsDirectory;
    } else {
      return this.file.externalDataDirectory;
    }
  }

  requestPermission() {
    if (this.platform.is('android')) {
      this.androidPermissions.requestPermissions([
        this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,
        this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE,
      ]);
    }
  }
}

results matching ""

    No results matching ""