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

273 lines
8.2 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';
import { CameraSource } from '@capacitor/camera';
2023-08-29 16:05:32 +01:00
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';
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-09-14 11:58:24 +01:00
capturedImage: any;
2023-08-28 17:28:09 +01:00
capturedImageTitle = '';
2023-08-29 16:05:32 +01:00
profilePicture = "";
2021-07-28 16:27:10 +01:00
profilePictureSubject: Observable<UserProfilePicture>
camecaIcons = false
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,
2023-08-29 16:05:32 +01:00
private storageService: StorageService,
private CameraService: CameraService,
private toastService: ToastService,
private httpErrorHandle: HttpErrorHandle,
private UserRepositoryService: UserRepositoryService
2023-08-29 16:05:32 +01:00
) {
this.profilePictureSubject = this.UserRepositoryService.getProfilePictureLive() as any
}
2021-07-28 16:27:10 +01:00
2023-09-09 14:40:08 +01:00
ngOnInit() {
setTimeout(() => {
this.camecaIcons = true
}, 100)
2023-09-18 05:41:33 +01:00
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) => {
2023-10-26 13:28:54 +01:00
if(picture) {
this.profilePicture = picture
} else {
this.profilePicture = "";
}
/* console.log(picture) */
2023-09-09 14:40:08 +01:00
}).catch((error) => {
2023-08-29 16:05:32 +01:00
this.profilePicture = "";
})
}
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
}
CameraSource = CameraSource
@XTracerAsync({name:'edit-profile/takePicture', bugPrint: true})
async uploadPicture(source: CameraSource, tracing?: TracingType) {
2023-08-28 17:28:09 +01:00
const capturedImage = await this.CameraService.takePicture({
2023-09-09 14:40:08 +01:00
width: 250,
height: 250,
2023-08-30 14:02:14 +01:00
quality: 100,
source: source
}, tracing)
2023-08-28 17:28:09 +01:00
if(capturedImage.isOk()) {
2023-09-09 14:40:08 +01:00
this.capturedImage = capturedImage.value;
var object = {
"ImageBase64": this.capturedImage
}
2023-09-14 11:58:24 +01:00
tracing.addEvent('serialize image')
2023-09-18 05:41:33 +01:00
const guid = await this.UserRepositoryService.addUserProfilePhoto(object)
2023-09-18 05:41:33 +01:00
if(guid.isOk()) {
tracing.addEvent('upload image')
2023-09-18 05:41:33 +01:00
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 {
2023-09-14 11:58:24 +01:00
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)
}
}
}
2023-09-09 14:40:08 +01:00
}
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
}