Files
doneit-web/src/app/modals/profile/edit-profile/edit-profile.page.ts
T
2023-11-09 10:28:58 +01:00

272 lines
7.9 KiB
TypeScript

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 { Camera, CameraResultType, CameraSource, Photo } from '@capacitor/camera';
import { Filesystem, Directory } from '@capacitor/filesystem';
import { File } from '@awesome-cordova-plugins/file/ngx';
import { StorageService } from 'src/app/services/storage.service';
import { AttachmentsService } from 'src/app/services/attachments.service';
@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 = "";
constructor(private modalController: ModalController,
private animationController: AnimationController,
public platform: Platform,
private BackgroundService: BackgroundService,
public ThemeService: ThemeService,
private file: File,
private storageService: StorageService,
private attachmentService: AttachmentsService
) { }
ngOnInit() {
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 = "";
})
}
/* getProfilpicture(guid) {
console.log('Get picture ', guid.path)
this.attachmentService.getUserProfilePhoto().subscribe(async (picture: any) => {
console.log('Get picture ', picture)
this.storageService.store(this.SessionStore.user.RoleID.toString() + "guid", guid.path)
this.storageService.store(this.SessionStore.user.RoleID.toString(), picture).then((value) => {
this.profilePicture = picture
this.SessionStore.user.UserPhoto = picture;
console.log('picture saved')
}).catch((error) => {
console.log('picture not saved')
});
}, ((error) => {
console.log('Error get profile picture: ', error)
}))
} */
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();
}
async takePicture() {
const capturedImage = await Camera.getPhoto({
width: 250,
height: 250,
quality: 100,
// allowEditing: true,
resultType: CameraResultType.Base64,
source: CameraSource.Camera
});
this.capturedImage = capturedImage.base64String;
var object = JSON.stringify({
"ImageBase64": this.capturedImage
}
)
console.log('ATTACHME ', object)
this.attachmentService.addUserProfilePhoto(object).subscribe((guid) => {
console.log('GUID ', guid)
console.log(this.SessionStore.user.RoleID.toString())
//get user profile picture base64
this.attachmentService.getUserProfilePhoto(guid).subscribe((base) => {
console.log('before picture saved',base)
this.storageService.store(this.SessionStore.user.RoleID.toString(), 'data:image/jpeg;base64,'+base).then((value) => {
this.profilePicture = 'data:image/jpeg;base64,' + base;
console.log('picture saved',value)
}).catch((error) => {
console.log('picture not saved')
});
},(error) => {
console.log('profile picture erro: ', error)
})
/* this.getProfilpicture(guid); */
}, ((error) => {
console.log('Erro Upload profile picture ', 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;
}
}