import { ApplicationRef, Injectable } from "@angular/core"; import PouchDB from "pouchdb"; import { Settings } from "../models/settings.model"; @Injectable({ providedIn: "root", }) export class PouchdbService { private db; constructor(private appRef: ApplicationRef) {} initPouchDb() { this.db = new PouchDB("counting.db"); } getSettings() { console.log("pouchGetSettingsCalled"); return new Promise((resolve, reject) => { this.db .get("settings") .then((settings) => { console.log(settings); let retrievedSettings = new Settings(settings._id, settings.vibration, settings.lastMac); resolve(retrievedSettings); }) .catch((error) => { console.log(error); if (error.name === "not_found" && error.reason === "missing") { let defSettings = new Settings("settings", true, ""); this.db.put(defSettings); console.log("Default Settings written to PouchDb"); resolve(defSettings); } else { console.error(error); } }); }); } saveSettings(lastMac: string, audio?: boolean, vibration?: boolean) { this.db .get("settings") .then((settings) => { settings.audio = audio === null || audio === undefined ? settings.audio : audio; settings.vibration = vibration === null || vibration === undefined ? settings.vibration : vibration; settings.lastMac = lastMac; return this.db.put(settings); }) .then(() => { console.log("settings saved"); }); } }