Skip to content

Commit

Permalink
Add import settings feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ijager committed Jan 6, 2022
1 parent 20f22bb commit f4420b7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
53 changes: 50 additions & 3 deletions src/app/core/services/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export class SettingsService {
console.log('loaded settings: ', data);
this.preferences = <Preferences>data;
if ((this.preferences.version || 0) < defaults.version) {
// TODO migrate or replace
console.log('Preferences outdates, using defaults');
this.migrate();
this.save();
Expand Down Expand Up @@ -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: [
Expand All @@ -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 = <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;
Expand All @@ -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;
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/app/home/settings/settings.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
<p>
Import an existing settings file (.json). For example one containing project specific matching rules.
</p>
<div class="p-inputgroup">
<span class="p-inputgroup-addon" style="line-height: 1;"> Import</span>
<input pInputText type="file">
<button pButton class="p-button-info" label="import settings" icon="pi pi-download" (click)="importSettings()"></button>
</div>

<button pButton class="p-button-info"label="import settings" icon="pi pi-download" (click)="importSettings()"></button>
<p>
Export settings so you can reuse them later
</p>
Expand Down
7 changes: 5 additions & 2 deletions src/app/home/settings/settings.component.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -86,4 +85,8 @@ export class SettingsComponent implements OnInit {
exportSettings() {
this.settingsService.export(this.exportFilename);
}

importSettings() {
this.settingsService.import();
}
}
2 changes: 1 addition & 1 deletion src/app/home/toolbar/toolbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit f4420b7

Please sign in to comment.