import { Component, OnInit } from '@angular/core'; import { AnimationController, ModalController, Platform } from '@ionic/angular'; import { FingerprintPage } from 'src/app/shared/fingerprint/fingerprint.page'; import { PinPage } from 'src/app/shared/pin/pin.page'; import { SessionStore } from 'src/app/store/session.service'; import { environment } from 'src/environments/environment'; import { BackgroundService } from 'src/app/services/background.service'; import { ThemeService } from 'src/app/services/theme.service'; import { CameraSource } from '@capacitor/camera'; import { StorageService } from 'src/app/services/storage.service'; import { CameraService } from 'src/app/infra/camera/camera.service'; import { ToastService } from 'src/app/services/toast.service'; import { HttpErrorHandle } from 'src/app/services/http-error-handle.service'; import { TracingType, XTracerAsync } from 'src/app/services/monitoring/opentelemetry/tracer'; import { UserRepositoryService } from 'src/app/module/user/data/user-repository.service'; import { isHttpError } from 'src/app/services/http.service'; import { UserProfilePicture } from 'src/app/module/user/data/datasource/user-local-repository.service'; import { Observable } from 'rxjs'; @Component({ selector: 'app-edit-profile', templateUrl: './edit-profile.page.html', styleUrls: ['./edit-profile.page.scss'], }) export class EditProfilePage implements OnInit { SessionStore = SessionStore production = environment.production environment = environment capturedImage: any; capturedImageTitle = ''; profilePicture = ""; profilePictureSubject: Observable camecaIcons = false constructor(private modalController: ModalController, private animationController: AnimationController, public platform: Platform, private BackgroundService: BackgroundService, public ThemeService: ThemeService, private storageService: StorageService, private CameraService: CameraService, private toastService: ToastService, private httpErrorHandle: HttpErrorHandle, private UserRepositoryService: UserRepositoryService ) { this.profilePictureSubject = this.UserRepositoryService.getProfilePictureLive() as any } ngOnInit() { setTimeout(() => { this.camecaIcons = true }, 100) this.getProfilpictureFromStorage(); } getProfilpictureFromStorage() { this.storageService.get(this.SessionStore.user.RoleID.toString()).then((picture) => { if(picture) { this.profilePicture = picture } else { this.profilePicture = ""; } /* console.log(picture) */ }).catch((error) => { this.profilePicture = ""; }) } close() { this.modalController.dismiss(); } async addPin() { const enterAnimation = (baseEl: any) => { const backdropAnimation = this.animationController.create() .addElement(baseEl.querySelector('ion-backdrop')!) .fromTo('opacity', '0.01', 'var(--backdrop-opacity)'); const wrapperAnimation = this.animationController.create() .addElement(baseEl.querySelector('.modal-wrapper')!) .keyframes([ { offset: 0, opacity: '1', right: '-100%' }, { offset: 1, opacity: '1', right: '0px' } ]); return this.animationController.create() .addElement(baseEl) .easing('ease-out') .duration(500) .addAnimation([backdropAnimation, wrapperAnimation]); } const leaveAnimation = (baseEl: any) => { return enterAnimation(baseEl).direction('reverse'); } const modal = await this.modalController.create({ enterAnimation, leaveAnimation, component: PinPage, cssClass: 'model profile-modal', componentProps: { } }); modal.present(); } async addFingerprint() { const enterAnimation = (baseEl: any) => { const backdropAnimation = this.animationController.create() .addElement(baseEl.querySelector('ion-backdrop')!) .fromTo('opacity', '0.01', 'var(--backdrop-opacity)'); const wrapperAnimation = this.animationController.create() .addElement(baseEl.querySelector('.modal-wrapper')!) .keyframes([ { offset: 0, opacity: '1', right: '-100%' }, { offset: 1, opacity: '1', right: '0px' } ]); return this.animationController.create() .addElement(baseEl) .easing('ease-out') .duration(500) .addAnimation([backdropAnimation, wrapperAnimation]); } const leaveAnimation = (baseEl: any) => { return enterAnimation(baseEl).direction('reverse'); } const modal = await this.modalController.create({ enterAnimation, leaveAnimation, component: FingerprintPage, cssClass: 'model profile-modal', componentProps: { } }); modal.present(); } LoginPreferenceMethod(type: 'None' | 'Password' | 'Pin' | null) { if (this.SessionStore.user.LoginPreference != type) { if (type) { this.SessionStore.setLoginPreference(type) } } else { this.SessionStore.setLoginPreference('None') } } changeTheme(name) { this.ThemeService.setTheme(name) this.BackgroundService.paint(); } CameraSource = CameraSource @XTracerAsync({name:'edit-profile/takePicture', bugPrint: true}) async uploadPicture(source: CameraSource, tracing?: TracingType) { const capturedImage = await this.CameraService.takePicture({ width: 250, height: 250, quality: 100, source: source }, tracing) if(capturedImage.isOk()) { this.capturedImage = capturedImage.value; var object = { "ImageBase64": this.capturedImage } tracing.addEvent('serialize image') const guid = await this.UserRepositoryService.addUserProfilePhoto(object) if(guid.isOk()) { tracing.addEvent('upload image') const base = await this.UserRepositoryService.getUserProfilePhoto(guid.value, tracing) if(base.isOk()) { tracing.addEvent('download image') this.profilePicture = 'data:image/jpeg;base64,' + base.value; tracing.setAttribute("picture.save", "true") } else { if(!isHttpError(base.error)) { this.toastService._badRequest('Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.') } else { this.httpErrorHandle.httpStatusHandle(base.error) } } } else { if(!isHttpError(guid.error)) { this.toastService._badRequest('Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.') } else { this.httpErrorHandle.httpStatusHandle(guid.error) } } } } b64toBlob(b64Data, contentType) { contentType = contentType || ''; var sliceSize = 512; b64Data = b64Data.replace(/^[^,]+,/, ''); b64Data = b64Data.replace(/\s/g, ''); var byteCharacters = window.atob(b64Data); var byteArrays = []; for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { var slice = byteCharacters.slice(offset, offset + sliceSize); var byteNumbers = new Array(slice.length); for (var i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } var byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } var blob = new Blob(byteArrays, { type: contentType }); return blob; } dataURItoBlob(dataURI) { // convert base64 to raw binary data held in a string // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this var byteString = atob(dataURI.split(',')[1]); // separate out the mime component var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0] // write the bytes of the string to an ArrayBuffer var ab = new ArrayBuffer(byteString.length); // create a view into the buffer var ia = new Uint8Array(ab); // set the bytes of the buffer to the correct values for (var i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } // write the ArrayBuffer to a blob, and you're done var blob = new Blob([ab], { type: mimeString }); return blob; } }