diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 1e151e874..740a5f8c0 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -17,20 +17,32 @@ import { AppComponent } from './app.component'; import { ElectronService } from './services/electron.service'; import { PwaService } from './services/pwa.service'; import { DataService } from './services/data.service'; +import { NgxIndexedDBModule, NgxIndexedDBService } from 'ngx-indexed-db'; +import { AppConfig } from '../environments/environment'; +import { dbConfig } from './indexed-db.config'; + // AoT requires an exported function for factories export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader { return new TranslateHttpLoader(http, './assets/i18n/', '.json'); } -function isElectron(): boolean { - return !!(window && window.process && window.process.type); +/** + * Returns true if the application runs in the electron based environment. + */ +function isElectron() { + return !!(window && window.process && (window.process as any).type); } -export function DataFactory() { +/** + * Conditionally imports the necessary service based on the current environment + * @param dbService indexed db service + * @returns + */ +export function DataFactory(dbService: NgxIndexedDBService, http: HttpClient) { if (isElectron()) { return new ElectronService(); } - return new PwaService(); + return new PwaService(dbService, http); } @NgModule({ @@ -42,6 +54,10 @@ export function DataFactory() { HttpClientModule, NgxWhatsNewModule, SharedModule, + AppConfig.environment === 'WEB' + ? NgxIndexedDBModule.forRoot(dbConfig) + : [], + NgxIndexedDBModule.forRoot(dbConfig), TranslateModule.forRoot({ loader: { provide: TranslateLoader, @@ -54,6 +70,7 @@ export function DataFactory() { { provide: DataService, useFactory: DataFactory, + deps: [NgxIndexedDBService, HttpClient], }, ], bootstrap: [AppComponent], diff --git a/src/app/indexed-db.config.ts b/src/app/indexed-db.config.ts new file mode 100644 index 000000000..a09154b80 --- /dev/null +++ b/src/app/indexed-db.config.ts @@ -0,0 +1,69 @@ +import { DBConfig } from 'ngx-indexed-db'; + +/** + * Contains names of the database stores + */ +export enum DbStores { + Playlists = 'playlists', +} + +/** Defines db tables and schema */ +export const dbConfig: DBConfig = { + name: 'iptvnator', + version: 1, + objectStoresMeta: [ + { + store: DbStores.Playlists, + storeConfig: { keyPath: '_id', autoIncrement: false }, + storeSchema: [ + { + name: '_id', + keypath: '_id', + options: { unique: false }, + }, + { + name: 'filename', + keypath: 'filename', + options: { unique: false }, + }, + { name: 'title', keypath: 'title', options: { unique: false } }, + { name: 'count', keypath: 'count', options: { unique: false } }, + { + name: 'playlist', + keypath: 'playlist', + options: { unique: false }, + }, + { + name: 'importDate', + keypath: 'importDate', + options: { unique: false }, + }, + { + name: 'lastUsage', + keypath: 'lastUsage', + options: { unique: false }, + }, + { + name: 'favorites', + keypath: 'favorites', + options: { unique: false }, + }, + { + name: 'autoRefresh', + keypath: 'autoRefresh', + options: { unique: false }, + }, + { + name: 'url', + keypath: 'url', + options: { unique: false }, + }, + { + name: 'filePath', + keypath: 'filePath', + options: { unique: false }, + }, + ], + }, + ], +};