Files
doneit-web/src/app/modals/profile/edit-profile/edit-profile.page.ts
T

251 lines
7.4 KiB
TypeScript
Raw Normal View History

2021-07-28 16:27:10 +01:00
import { Component, OnInit } from '@angular/core';
2023-08-28 17:28:09 +01:00
import { AnimationController, ModalController, Platform } from '@ionic/angular';
2021-07-28 16:27:10 +01:00
import { FingerprintPage } from 'src/app/shared/fingerprint/fingerprint.page';
import { PinPage } from 'src/app/shared/pin/pin.page';
2021-08-27 17:11:05 +01:00
import { SessionStore } from 'src/app/store/session.service';
2021-08-31 12:02:45 +01:00
import { environment } from 'src/environments/environment';
2021-10-20 17:46:03 +01:00
import { BackgroundService } from 'src/app/services/background.service';
import { ThemeService } from 'src/app/services/theme.service';
2023-08-28 17:28:09 +01:00
import { Camera, CameraResultType, CameraSource, Photo } from '@capacitor/camera';
import { Filesystem, Directory } from '@capacitor/filesystem';
import { File } from '@awesome-cordova-plugins/file/ngx';
2023-08-29 16:05:32 +01:00
import { StorageService } from 'src/app/services/storage.service';
2023-09-09 14:40:08 +01:00
import { AttachmentsService } from 'src/app/services/attachments.service';
2023-08-28 17:28:09 +01:00
2021-07-28 16:27:10 +01:00
@Component({
selector: 'app-edit-profile',
templateUrl: './edit-profile.page.html',
styleUrls: ['./edit-profile.page.scss'],
})
export class EditProfilePage implements OnInit {
2021-08-27 17:11:05 +01:00
SessionStore = SessionStore
2021-08-31 12:02:45 +01:00
production = environment.production
2023-01-25 15:49:16 +01:00
environment = environment
2023-08-28 17:28:09 +01:00
capturedImage = '';
capturedImageTitle = '';
2023-08-29 16:05:32 +01:00
profilePicture = "";
2021-07-28 16:27:10 +01:00
2023-08-28 17:28:09 +01:00
constructor(private modalController: ModalController,
2021-09-02 12:17:14 +01:00
private animationController: AnimationController,
public platform: Platform,
2022-10-11 17:07:51 +01:00
private BackgroundService: BackgroundService,
2023-08-28 17:28:09 +01:00
public ThemeService: ThemeService,
private file: File,
2023-08-29 16:05:32 +01:00
private storageService: StorageService,
2023-09-09 14:40:08 +01:00
private attachmentService: AttachmentsService
2023-08-29 16:05:32 +01:00
2023-08-28 17:28:09 +01:00
) { }
2021-07-28 16:27:10 +01:00
2023-09-09 14:40:08 +01:00
ngOnInit() {
this.getProfilpictureFromStorage()
2023-08-29 16:05:32 +01:00
}
2023-09-09 14:40:08 +01:00
getProfilpictureFromStorage() {
2023-08-29 16:05:32 +01:00
this.storageService.get(this.SessionStore.user.RoleID.toString()).then((picture) => {
console.log(picture)
this.profilePicture = picture
2023-09-09 14:40:08 +01:00
}).catch((error) => {
2023-08-29 16:05:32 +01:00
this.profilePicture = "";
})
}
2023-09-09 14:40:08 +01:00
getProfilpicture(guid) {
console.log('Get picture ', guid.path)
this.attachmentService.downloadFile(guid.path).subscribe(async (picture: any) => {
let downloadFile = 'data:image/jpeg;base64,' + btoa(new Uint8Array(picture.body).reduce((data, byte) => data + String.fromCharCode(byte), ''));
console.log('Get picture ', downloadFile)
this.storageService.store(this.SessionStore.user.RoleID.toString()+"guid", guid.path)
this.storageService.store(this.SessionStore.user.RoleID.toString(), downloadFile).then((value) => {
console.log('picture saved')
}).catch((error) => {
console.log('picture not saved')
});
}, ((error) => {
console.log('Error get profile picture: ', error)
}))
}
2021-07-28 16:27:10 +01:00
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();
}
2021-08-27 17:11:05 +01:00
LoginPreferenceMethod(type: 'None' | 'Password' | 'Pin' | null) {
2021-07-28 16:27:10 +01:00
2021-08-27 17:11:05 +01:00
if (this.SessionStore.user.LoginPreference != type) {
if (type) {
this.SessionStore.setLoginPreference(type)
2021-07-28 16:27:10 +01:00
}
} else {
2021-08-27 17:11:05 +01:00
this.SessionStore.setLoginPreference('None')
2021-07-28 16:27:10 +01:00
}
}
2021-10-20 17:46:03 +01:00
changeTheme(name) {
2021-10-25 13:54:34 +01:00
this.ThemeService.setTheme(name)
2022-10-11 17:07:51 +01:00
this.BackgroundService.paint();
2021-10-20 17:46:03 +01:00
}
2023-08-28 17:28:09 +01:00
async takePicture() {
const capturedImage = await Camera.getPhoto({
2023-09-09 14:40:08 +01:00
width: 250,
height: 250,
2023-08-30 14:02:14 +01:00
quality: 100,
2023-08-28 17:28:09 +01:00
// allowEditing: true,
resultType: CameraResultType.Base64,
source: CameraSource.Camera
});
this.capturedImageTitle = SessionStore.user.Profile;
2023-09-09 14:40:08 +01:00
this.capturedImage = 'data:image/jpeg;base64,' + capturedImage.base64String;
2023-08-28 17:28:09 +01:00
2023-09-09 14:40:08 +01:00
const blob = this.dataURItoBlob(this.capturedImage)
2023-08-28 17:28:09 +01:00
2023-09-09 14:40:08 +01:00
const formData = new FormData();
formData.append("blobFile", blob);
this.attachmentService.uploadFile(formData).subscribe((guid) => {
console.log('GUID ', guid)
console.log(this.SessionStore.user.RoleID.toString())
this.getProfilpicture(guid);
}, ((error) => {
console.log('Erro Upload profile picture ',error)
}))
}
2023-08-28 17:28:09 +01:00
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;
}
2023-09-09 14:40:08 +01:00
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;
}
2021-07-28 16:27:10 +01:00
}