MOBILE-4304 sharedfiles: Update database usage

main
Noel De Martin 2024-01-17 15:20:01 +01:00
parent 7e2d2fb74b
commit bb82cd8ff1
1 changed files with 18 additions and 14 deletions

View File

@ -16,7 +16,6 @@ import { Injectable } from '@angular/core';
import { FileEntry, DirectoryEntry } from '@awesome-cordova-plugins/file/ngx'; import { FileEntry, DirectoryEntry } from '@awesome-cordova-plugins/file/ngx';
import { Md5 } from 'ts-md5/dist/md5'; import { Md5 } from 'ts-md5/dist/md5';
import { SQLiteDB } from '@classes/sqlitedb';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { CoreApp } from '@services/app'; import { CoreApp } from '@services/app';
import { CoreFile } from '@services/file'; import { CoreFile } from '@services/file';
@ -27,6 +26,9 @@ import { CoreEvents } from '@singletons/events';
import { makeSingleton } from '@singletons'; import { makeSingleton } from '@singletons';
import { APP_SCHEMA, CoreSharedFilesDBRecord, SHARED_FILES_TABLE_NAME } from './database/sharedfiles'; import { APP_SCHEMA, CoreSharedFilesDBRecord, SHARED_FILES_TABLE_NAME } from './database/sharedfiles';
import { CorePath } from '@singletons/path'; import { CorePath } from '@singletons/path';
import { asyncInstance } from '@/core/utils/async-instance';
import { CoreDatabaseTable } from '@classes/database/database-table';
import { CoreDatabaseCachingStrategy, CoreDatabaseTableProxy } from '@classes/database/database-table-proxy';
/** /**
* Service to share files with the app. * Service to share files with the app.
@ -37,13 +39,10 @@ export class CoreSharedFilesProvider {
static readonly SHARED_FILES_FOLDER = 'sharedfiles'; static readonly SHARED_FILES_FOLDER = 'sharedfiles';
protected logger: CoreLogger; protected logger: CoreLogger;
// Variables for DB. protected sharedFilesTable = asyncInstance<CoreDatabaseTable<CoreSharedFilesDBRecord>>();
protected appDB: Promise<SQLiteDB>;
protected resolveAppDB!: (appDB: SQLiteDB) => void;
constructor() { constructor() {
this.logger = CoreLogger.getInstance('CoreSharedFilesProvider'); this.logger = CoreLogger.getInstance('CoreSharedFilesProvider');
this.appDB = new Promise(resolve => this.resolveAppDB = resolve);
} }
/** /**
@ -58,7 +57,16 @@ export class CoreSharedFilesProvider {
// Ignore errors. // Ignore errors.
} }
this.resolveAppDB(CoreApp.getDB()); const database = CoreApp.getDB();
const sharedFilesTable = new CoreDatabaseTableProxy<CoreSharedFilesDBRecord>(
{ cachingStrategy: CoreDatabaseCachingStrategy.None },
database,
SHARED_FILES_TABLE_NAME,
);
await sharedFilesTable.initialize();
this.sharedFilesTable.setInstance(sharedFilesTable);
} }
/** /**
@ -199,9 +207,9 @@ export class CoreSharedFilesProvider {
* @returns Resolved if treated, rejected otherwise. * @returns Resolved if treated, rejected otherwise.
*/ */
protected async isFileTreated(fileId: string): Promise<CoreSharedFilesDBRecord> { protected async isFileTreated(fileId: string): Promise<CoreSharedFilesDBRecord> {
const db = await this.appDB; const sharedFile = await this.sharedFilesTable.getOneByPrimaryKey({ id: fileId });
return db.getRecord(SHARED_FILES_TABLE_NAME, { id: fileId }); return sharedFile;
} }
/** /**
@ -216,9 +224,7 @@ export class CoreSharedFilesProvider {
await this.isFileTreated(fileId); await this.isFileTreated(fileId);
} catch (err) { } catch (err) {
// Doesn't exist, insert it. // Doesn't exist, insert it.
const db = await this.appDB; await this.sharedFilesTable.insert({ id: fileId });
await db.insertRecord(SHARED_FILES_TABLE_NAME, { id: fileId });
} }
} }
@ -259,9 +265,7 @@ export class CoreSharedFilesProvider {
* @returns Resolved when unmarked. * @returns Resolved when unmarked.
*/ */
protected async unmarkAsTreated(fileId: string): Promise<void> { protected async unmarkAsTreated(fileId: string): Promise<void> {
const db = await this.appDB; await this.sharedFilesTable.deleteByPrimaryKey({ id: fileId });
await db.deleteRecords(SHARED_FILES_TABLE_NAME, { id: fileId });
} }
} }