improve chat

This commit is contained in:
Peter Maquiran
2024-08-17 22:05:57 +01:00
parent eb615d4335
commit 650c772084
43 changed files with 712 additions and 1540 deletions
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { Camera, CameraResultType, CameraSource } from '@capacitor/camera';
import { err, ok } from 'neverthrow';
import { Camera, CameraPhoto, CameraResultType, CameraSource } from '@capacitor/camera';
import { err, ok, Result } from 'neverthrow';
/**
* Parameters for picking a picture.
@@ -13,6 +13,15 @@ type PickPictureParams = {
cameraResultType?: CameraResultType;
};
/**
* Error types for the FilePickerService.
*/
export interface FilePickerError {
type: 'PERMISSION_DENIED' | 'CANCELLED' | 'UNKNOWN';
message: string;
originalError?: any;
}
/**
* Service for handling file picking functionality.
* This service provides methods to pick a picture from the device's photo library.
@@ -22,9 +31,6 @@ type PickPictureParams = {
})
export class FilePickerService {
/**
* Creates an instance of FilePickerService.
*/
constructor() { }
/**
@@ -34,7 +40,7 @@ export class FilePickerService {
* @param {CameraResultType} [params.cameraResultType=CameraResultType.DataUrl] - The result type of the photo. Defaults to `CameraResultType.DataUrl`.
* @returns {Promise<ok<File> | err<any>>} A promise that resolves with an `ok` result containing the file or an `err` result containing the error.
*/
async getPicture({quality = 90, cameraResultType = CameraResultType.DataUrl }: PickPictureParams) {
async getPicture({quality = 90, cameraResultType = CameraResultType.DataUrl }: PickPictureParams): Promise<Result<CameraPhoto, FilePickerError>> {
try {
const file = await Camera.getPhoto({
quality: quality,
@@ -44,7 +50,25 @@ export class FilePickerService {
return ok(file);
} catch (e) {
return err(e);
if (e.message.includes('denied')) {
return err({
type: 'PERMISSION_DENIED',
message: 'Permission to access photos was denied.',
originalError: e
});
} else if (e.message.includes('User cancelled photos app')) {
return err({
type: 'CANCELLED',
message: 'User cancelled the photo selection.',
originalError: e
});
} else {
return err({
type: 'UNKNOWN',
message: 'An unknown error occurred while picking the picture.',
originalError: e
});
}
}
}
}
@@ -9,14 +9,21 @@ export class FilePickerMobileService {
constructor() { }
/**
* @example
* ```typescript
* const types = ['application/pdf', 'application/doc', 'application/docx','application/xls', 'application/xlsx', 'application/ppt','application/pptx', 'application/txt'];
* const multiple = false; // Invalid due to commas
* const readData = true; // Invalid due to commas
* ```
*/
async getFile({types, multiple, readData}): Promise<Result<PickFilesResult, any>> {
try {
const result = await FilePicker.pickFiles({
types: ['application/pdf', 'application/doc', 'application/docx','application/xls', 'application/xlsx', 'application/ppt',
'application/pptx', 'application/txt'],
multiple: false,
readData: true,
types: types,
multiple: multiple,
readData: readData,
});
return ok(result)
@@ -1,6 +1,7 @@
import { Result, ok, err } from 'neverthrow';
import { EntityTable } from 'Dexie';
import { ZodError, ZodObject, ZodSchema } from 'zod';
import { Logger } from 'src/app/services/logger/main/service';
// Define a type for the Result of repository operations
type RepositoryResult<T, E> = Result<T, Error | ZodError<E>>;
@@ -28,6 +29,9 @@ export class DexieRepository<T> {
return err(new Error('Failed to insert document: ' + error.message));
}
} else {
Logger.error(`dexie.js failed to insert into ${this.table.name}, invalid data`, {
data: document
});
return err((dataValidation as unknown as ZodError<T>))
}
}
@@ -45,6 +49,9 @@ export class DexieRepository<T> {
const ids = await this.table.bulkAdd(documents as any);
return ok(ids);
} catch (error) {
Logger.error(`dexie.js failed to insert many into ${this.table.name}, invalid data`, {
data: document
});
return err(new Error('Failed to insert multiple documents: ' + error.message));
}
}