From a48e12276340fea00efe2693e8a95f56737c1149 Mon Sep 17 00:00:00 2001 From: Adam Verga Date: Wed, 29 Apr 2020 20:15:15 -0400 Subject: [PATCH] Settings are now more centralized in the frontend --- src/app/app.component.ts | 28 ++--- src/app/main/main.component.ts | 104 ++++++++---------- src/app/posts.services.ts | 5 + src/app/settings/settings.component.ts | 12 +- .../subscription/subscription.component.ts | 11 +- 5 files changed, 73 insertions(+), 87 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 08e81cb..0bf3165 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -62,8 +62,7 @@ export class AppComponent implements OnInit { } }); - this.loadConfig(); - this.postsService.settings_changed.subscribe(changed => { + this.postsService.config_reloaded.subscribe(changed => { if (changed) { this.loadConfig(); } @@ -77,22 +76,17 @@ export class AppComponent implements OnInit { loadConfig() { // loading config - this.postsService.loadNavItems().subscribe(res => { // loads settings - const result = !this.postsService.debugMode ? res['config_file'] : res; - this.topBarTitle = result['YoutubeDLMaterial']['Extra']['title_top']; - this.settingsPinRequired = result['YoutubeDLMaterial']['Extra']['settings_pin_required']; - const themingExists = result['YoutubeDLMaterial']['Themes']; - this.defaultTheme = themingExists ? result['YoutubeDLMaterial']['Themes']['default_theme'] : 'default'; - this.allowThemeChange = themingExists ? result['YoutubeDLMaterial']['Themes']['allow_theme_change'] : true; - this.allowSubscriptions = result['YoutubeDLMaterial']['Subscriptions']['allow_subscriptions']; + this.topBarTitle = this.postsService.config['Extra']['title_top']; + this.settingsPinRequired = this.postsService.config['Extra']['settings_pin_required']; + const themingExists = this.postsService.config['Themes']; + this.defaultTheme = themingExists ? this.postsService.config['Themes']['default_theme'] : 'default'; + this.allowThemeChange = themingExists ? this.postsService.config['Themes']['allow_theme_change'] : true; + this.allowSubscriptions = this.postsService.config['Subscriptions']['allow_subscriptions']; - // sets theme to config default if it doesn't exist - if (!localStorage.getItem('theme')) { - this.setTheme(themingExists ? this.defaultTheme : 'default'); - } - }, error => { - console.log(error); - }); + // sets theme to config default if it doesn't exist + if (!localStorage.getItem('theme')) { + this.setTheme(themingExists ? this.defaultTheme : 'default'); + } } // theme stuff diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index 38339d3..b76d6d0 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -231,80 +231,72 @@ export class MainComponent implements OnInit { async loadConfig() { // loading config - this.postsService.loadNavItems().subscribe(res => { // loads settings - const result = !this.postsService.debugMode ? res['config_file'] : res; - this.fileManagerEnabled = result['YoutubeDLMaterial']['Extra']['file_manager_enabled']; - this.downloadOnlyMode = result['YoutubeDLMaterial']['Extra']['download_only_mode']; - this.allowMultiDownloadMode = result['YoutubeDLMaterial']['Extra']['allow_multi_download_mode']; - this.audioFolderPath = result['YoutubeDLMaterial']['Downloader']['path-audio']; - this.videoFolderPath = result['YoutubeDLMaterial']['Downloader']['path-video']; - this.use_youtubedl_archive = result['YoutubeDLMaterial']['Downloader']['use_youtubedl_archive']; - this.globalCustomArgs = result['YoutubeDLMaterial']['Downloader']['custom_args']; - this.youtubeSearchEnabled = result['YoutubeDLMaterial']['API'] && result['YoutubeDLMaterial']['API']['use_youtube_API'] && - result['YoutubeDLMaterial']['API']['youtube_API_key']; - this.youtubeAPIKey = this.youtubeSearchEnabled ? result['YoutubeDLMaterial']['API']['youtube_API_key'] : null; - this.allowQualitySelect = result['YoutubeDLMaterial']['Extra']['allow_quality_select']; - this.allowAdvancedDownload = result['YoutubeDLMaterial']['Advanced']['allow_advanced_download']; - this.useDefaultDownloadingAgent = result['YoutubeDLMaterial']['Advanced']['use_default_downloading_agent']; - this.customDownloadingAgent = result['YoutubeDLMaterial']['Advanced']['custom_downloading_agent']; + this.fileManagerEnabled = this.postsService.config['Extra']['file_manager_enabled']; + this.downloadOnlyMode = this.postsService.config['Extra']['download_only_mode']; + this.allowMultiDownloadMode = this.postsService.config['Extra']['allow_multi_download_mode']; + this.audioFolderPath = this.postsService.config['Downloader']['path-audio']; + this.videoFolderPath = this.postsService.config['Downloader']['path-video']; + this.use_youtubedl_archive = this.postsService.config['Downloader']['use_youtubedl_archive']; + this.globalCustomArgs = this.postsService.config['Downloader']['custom_args']; + this.youtubeSearchEnabled = this.postsService.config['API'] && this.postsService.config['API']['use_youtube_API'] && + this.postsService.config['API']['youtube_API_key']; + this.youtubeAPIKey = this.youtubeSearchEnabled ? this.postsService.config['API']['youtube_API_key'] : null; + this.allowQualitySelect = this.postsService.config['Extra']['allow_quality_select']; + this.allowAdvancedDownload = this.postsService.config['Advanced']['allow_advanced_download']; + this.useDefaultDownloadingAgent = this.postsService.config['Advanced']['use_default_downloading_agent']; + this.customDownloadingAgent = this.postsService.config['Advanced']['custom_downloading_agent']; - if (this.fileManagerEnabled) { - this.getMp3s(); - this.getMp4s(); + if (this.fileManagerEnabled) { + this.getMp3s(); + this.getMp4s(); + } + + if (this.youtubeSearchEnabled && this.youtubeAPIKey) { + this.youtubeSearch.initializeAPI(this.youtubeAPIKey); + this.attachToInput(); + } + + // set final cache items + if (this.allowAdvancedDownload) { + if (localStorage.getItem('customArgsEnabled') !== null) { + this.customArgsEnabled = localStorage.getItem('customArgsEnabled') === 'true'; } - if (this.youtubeSearchEnabled && this.youtubeAPIKey) { - this.youtubeSearch.initializeAPI(this.youtubeAPIKey); - this.attachToInput(); + if (localStorage.getItem('customOutputEnabled') !== null) { + this.customOutputEnabled = localStorage.getItem('customOutputEnabled') === 'true'; } - // set final cache items - if (this.allowAdvancedDownload) { - if (localStorage.getItem('customArgsEnabled') !== null) { - this.customArgsEnabled = localStorage.getItem('customArgsEnabled') === 'true'; - } - - if (localStorage.getItem('customOutputEnabled') !== null) { - this.customOutputEnabled = localStorage.getItem('customOutputEnabled') === 'true'; - } - - if (localStorage.getItem('youtubeAuthEnabled') !== null) { - this.youtubeAuthEnabled = localStorage.getItem('youtubeAuthEnabled') === 'true'; - } - - // set advanced inputs - const customArgs = localStorage.getItem('customArgs'); - const customOutput = localStorage.getItem('customOutput'); - const youtubeUsername = localStorage.getItem('youtubeUsername'); - - if (customArgs && customArgs !== 'null') { this.customArgs = customArgs }; - if (customOutput && customOutput !== 'null') { this.customOutput = customOutput }; - if (youtubeUsername && youtubeUsername !== 'null') { this.youtubeUsername = youtubeUsername }; + if (localStorage.getItem('youtubeAuthEnabled') !== null) { + this.youtubeAuthEnabled = localStorage.getItem('youtubeAuthEnabled') === 'true'; } - // get downloads routine - setInterval(() => { - if (this.current_download) { - this.getCurrentDownload(); - } - }, 500); + // set advanced inputs + const customArgs = localStorage.getItem('customArgs'); + const customOutput = localStorage.getItem('customOutput'); + const youtubeUsername = localStorage.getItem('youtubeUsername'); - return true; + if (customArgs && customArgs !== 'null') { this.customArgs = customArgs }; + if (customOutput && customOutput !== 'null') { this.customOutput = customOutput }; + if (youtubeUsername && youtubeUsername !== 'null') { this.youtubeUsername = youtubeUsername }; + } - }, error => { - console.log(error); + // get downloads routine + setInterval(() => { + if (this.current_download) { + this.getCurrentDownload(); + } + }, 500); - return false; - }); + return true; } // app initialization. ngOnInit() { this.configLoad(); - this.postsService.settings_changed.subscribe(changed => { + this.postsService.config_reloaded.subscribe(changed => { if (changed) { this.loadConfig(); } diff --git a/src/app/posts.services.ts b/src/app/posts.services.ts index 0689100..98c16ba 100644 --- a/src/app/posts.services.ts +++ b/src/app/posts.services.ts @@ -77,6 +77,10 @@ export class PostsService implements CanActivate { } } }); + + this.reload_config.subscribe(yes_reload => { + if (yes_reload) { this.reloadConfig(); } + }); } canActivate(route, state): Promise { return new Promise(resolve => { @@ -103,6 +107,7 @@ export class PostsService implements CanActivate { const result = !this.debugMode ? res['config_file'] : res; if (result) { this.config = result['YoutubeDLMaterial']; + this.config_reloaded = true; } }); } diff --git a/src/app/settings/settings.component.ts b/src/app/settings/settings.component.ts index 57a14b0..b8fc2b9 100644 --- a/src/app/settings/settings.component.ts +++ b/src/app/settings/settings.component.ts @@ -51,14 +51,8 @@ export class SettingsComponent implements OnInit { } getConfig() { - this.loading_config = true; - this.postsService.loadNavItems().subscribe(res => { - this.loading_config = false; - // successfully loaded config - - this.initial_config = !this.postsService.debugMode ? res['config_file']['YoutubeDLMaterial'] : res['YoutubeDLMaterial']; - this.new_config = JSON.parse(JSON.stringify(this.initial_config)); - }); + this.initial_config = this.postsService.config; + this.new_config = JSON.parse(JSON.stringify(this.initial_config)); } settingsSame() { @@ -70,8 +64,8 @@ export class SettingsComponent implements OnInit { this.postsService.setConfig(settingsToSave).subscribe(res => { if (res['success']) { // sets new config as old config - this.postsService.settings_changed.next(true); this.initial_config = JSON.parse(JSON.stringify(this.new_config)); + this.postsService.reload_config.next(true); } }, err => { console.error('Failed to save config!'); diff --git a/src/app/subscription/subscription/subscription.component.ts b/src/app/subscription/subscription/subscription.component.ts index 3c7545f..fce7761 100644 --- a/src/app/subscription/subscription/subscription.component.ts +++ b/src/app/subscription/subscription/subscription.component.ts @@ -50,7 +50,11 @@ export class SubscriptionComponent implements OnInit { this.id = this.route.snapshot.paramMap.get('id'); this.getSubscription(); - this.getConfig(); + this.postsService.config_reloaded.subscribe(changed => { + if (changed) { + this.getConfig(); + } + }); } // set filter property to cached @@ -78,10 +82,7 @@ export class SubscriptionComponent implements OnInit { } getConfig() { - this.postsService.loadNavItems().subscribe(res => { - const result = !this.postsService.debugMode ? res['config_file'] : res; - this.use_youtubedl_archive = result['YoutubeDLMaterial']['Subscriptions']['subscriptions_use_youtubedl_archive']; - }); + this.use_youtubedl_archive = this.postsService.config['Subscriptions']['subscriptions_use_youtubedl_archive']; } goToFile(emit_obj) {