From f4420b7a2acd1de0b81f0c3bad24f1062185620f Mon Sep 17 00:00:00 2001 From: Ingmar Jager Date: Thu, 6 Jan 2022 15:13:05 +0100 Subject: [PATCH] Add import settings feature --- src/app/core/services/settings.service.ts | 53 +++++++++++++++++-- src/app/home/settings/settings.component.html | 7 +-- src/app/home/settings/settings.component.ts | 7 ++- src/app/home/toolbar/toolbar.component.ts | 2 +- 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/app/core/services/settings.service.ts b/src/app/core/services/settings.service.ts index 2d11d86..67b5b32 100644 --- a/src/app/core/services/settings.service.ts +++ b/src/app/core/services/settings.service.ts @@ -99,7 +99,6 @@ export class SettingsService { console.log('loaded settings: ', data); this.preferences = data; if ((this.preferences.version || 0) < defaults.version) { - // TODO migrate or replace console.log('Preferences outdates, using defaults'); this.migrate(); this.save(); @@ -150,7 +149,7 @@ export class SettingsService { export(suggestedFilename) { let filename = this.electronService.remote.dialog.showSaveDialogSync({ - title: 'Export settings to File…', + title: 'Export settings to file…', message: 'For example, save the settings in your project repository', defaultPath: suggestedFilename, filters: [ @@ -165,6 +164,55 @@ export class SettingsService { } + import() { + + let filename = this.electronService.remote.dialog.showOpenDialogSync({ + title: 'Import settings file', + filters: [ + { name: 'JSON files', extensions: ['*.json'] } + ] + }); + + if (filename && filename.length) { + + const filecontent: string = this.fs.readFileSync(filename[0], { + encoding: 'utf-8' + }); + + let loaded_preferences = undefined; + try { + loaded_preferences = JSON.parse(filecontent) + } + catch { + this.electronService.remote.dialog.showErrorBox( + `Error while loading file ${filename}`, + "Invalid json" + ); + } + + if (loaded_preferences) { + this.preferences = loaded_preferences; + if ((this.preferences.version || 0) < defaults.version) { + this.migrate(); + } + this.save(); + this.broadcastAllSettings(); + this.electronService.remote.dialog.showMessageBoxSync({ + message: "Settings loaded", + type: 'info' + }); + } + } + + } + + broadcastAllSettings() { + this.baudRateChanged.next(this.preferences.baudrate); + this.maxLinesChanged.next(this.preferences.maxLines); + this.delimiterChanged.next(this.preferences.newlineChar); + this.preferencesLoaded$.next(true); + } + save() { this.storage.set(PREFERENCES_KEY, this.preferences, (error) => { if (error) throw error; @@ -181,7 +229,6 @@ export class SettingsService { const rule = this.preferences.rules; for (let i=0; i < rule.length; i++) { if (line.match(rule[i].template)) { - // if (line.startsWith(rule[i].template)) { return rule[i].color; } } diff --git a/src/app/home/settings/settings.component.html b/src/app/home/settings/settings.component.html index 2273e8b..c5aeec0 100644 --- a/src/app/home/settings/settings.component.html +++ b/src/app/home/settings/settings.component.html @@ -6,12 +6,7 @@

Import an existing settings file (.json). For example one containing project specific matching rules.

-
- Import - - -
- +

Export settings so you can reuse them later

diff --git a/src/app/home/settings/settings.component.ts b/src/app/home/settings/settings.component.ts index 928c00d..3cf7b5e 100644 --- a/src/app/home/settings/settings.component.ts +++ b/src/app/home/settings/settings.component.ts @@ -1,6 +1,5 @@ import { Component, OnInit, NgZone, OnDestroy } from '@angular/core'; import { SettingsService, Preferences } from '../../core/services/settings.service'; -import { first } from 'rxjs/operators'; import { PluginService, Plugin, MatchRule } from '../../core/services/plugin.service'; @Component({ @@ -38,7 +37,7 @@ export class SettingsComponent implements OnInit { console.log('settingscmp init'); this.plugins = this.pluginService.plugins; - this.settingsService.isReady().pipe(first()).subscribe((ready) => { + this.settingsService.isReady().subscribe((ready) => { this.zone.run(() => { this.preferences = this.settingsService.getPreferences(); this.rules = this.preferences.rules; @@ -86,4 +85,8 @@ export class SettingsComponent implements OnInit { exportSettings() { this.settingsService.export(this.exportFilename); } + + importSettings() { + this.settingsService.import(); + } } diff --git a/src/app/home/toolbar/toolbar.component.ts b/src/app/home/toolbar/toolbar.component.ts index 309fc2e..27791a9 100644 --- a/src/app/home/toolbar/toolbar.component.ts +++ b/src/app/home/toolbar/toolbar.component.ts @@ -54,7 +54,7 @@ export class ToolbarComponent implements OnInit { this.serialService.connected$.subscribe(() => this.reset()); - this.settings.isReady().pipe(first()).subscribe(() => { + this.settings.isReady().subscribe(() => { this.baudrate = this.settings.getBaudRate(); this.delimiter = this.settings.getDelimiter(); });