From bb82cd8ff1ca6881ccda5b96580b2e2cdac9cbeb Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Wed, 17 Jan 2024 15:20:01 +0100 Subject: [PATCH] MOBILE-4304 sharedfiles: Update database usage --- .../sharedfiles/services/sharedfiles.ts | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/core/features/sharedfiles/services/sharedfiles.ts b/src/core/features/sharedfiles/services/sharedfiles.ts index 5d8e5231a..2cc359bb3 100644 --- a/src/core/features/sharedfiles/services/sharedfiles.ts +++ b/src/core/features/sharedfiles/services/sharedfiles.ts @@ -16,7 +16,6 @@ import { Injectable } from '@angular/core'; import { FileEntry, DirectoryEntry } from '@awesome-cordova-plugins/file/ngx'; import { Md5 } from 'ts-md5/dist/md5'; -import { SQLiteDB } from '@classes/sqlitedb'; import { CoreLogger } from '@singletons/logger'; import { CoreApp } from '@services/app'; import { CoreFile } from '@services/file'; @@ -27,6 +26,9 @@ import { CoreEvents } from '@singletons/events'; import { makeSingleton } from '@singletons'; import { APP_SCHEMA, CoreSharedFilesDBRecord, SHARED_FILES_TABLE_NAME } from './database/sharedfiles'; 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. @@ -37,13 +39,10 @@ export class CoreSharedFilesProvider { static readonly SHARED_FILES_FOLDER = 'sharedfiles'; protected logger: CoreLogger; - // Variables for DB. - protected appDB: Promise; - protected resolveAppDB!: (appDB: SQLiteDB) => void; + protected sharedFilesTable = asyncInstance>(); constructor() { this.logger = CoreLogger.getInstance('CoreSharedFilesProvider'); - this.appDB = new Promise(resolve => this.resolveAppDB = resolve); } /** @@ -58,7 +57,16 @@ export class CoreSharedFilesProvider { // Ignore errors. } - this.resolveAppDB(CoreApp.getDB()); + const database = CoreApp.getDB(); + const sharedFilesTable = new CoreDatabaseTableProxy( + { 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. */ protected async isFileTreated(fileId: string): Promise { - 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); } catch (err) { // Doesn't exist, insert it. - const db = await this.appDB; - - await db.insertRecord(SHARED_FILES_TABLE_NAME, { id: fileId }); + await this.sharedFilesTable.insert({ id: fileId }); } } @@ -259,9 +265,7 @@ export class CoreSharedFilesProvider { * @returns Resolved when unmarked. */ protected async unmarkAsTreated(fileId: string): Promise { - const db = await this.appDB; - - await db.deleteRecords(SHARED_FILES_TABLE_NAME, { id: fileId }); + await this.sharedFilesTable.deleteByPrimaryKey({ id: fileId }); } }