From 372d74ec71656cf9aee60fa204d293077dbaa661 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Sat, 22 Mar 2025 08:46:36 +0800 Subject: [PATCH] Enhance settings management and localStorage integration - Added functionality to load settings from localStorage in the SettingsManager, ensuring user preferences are retained across sessions. - Updated the state management to initialize settings from localStorage, improving user experience. - Refactored the UpdateService to streamline update notification preferences. - Improved migration logic in storageHelpers to prevent duplicate migrations and ensure data integrity. - Removed unnecessary console logs for cleaner output in various modules. --- static/js/api/loraApi.js | 1 - static/js/managers/SettingsManager.js | 29 +++++++++++++++++++--- static/js/managers/UpdateService.js | 6 +---- static/js/state/index.js | 13 +++++++--- static/js/utils/storageHelpers.js | 35 ++++++++++++++++++++------- static/js/utils/uiHelpers.js | 2 +- 6 files changed, 63 insertions(+), 23 deletions(-) diff --git a/static/js/api/loraApi.js b/static/js/api/loraApi.js index 69aba234..95066e65 100644 --- a/static/js/api/loraApi.js +++ b/static/js/api/loraApi.js @@ -63,7 +63,6 @@ export async function loadMoreLoras(resetPage = false, updateFolders = false) { } const data = await response.json(); - console.log('Received data:', data); if (data.items.length === 0 && pageState.currentPage === 1) { const grid = document.getElementById('loraGrid'); diff --git a/static/js/managers/SettingsManager.js b/static/js/managers/SettingsManager.js index ad716b40..fd14fe54 100644 --- a/static/js/managers/SettingsManager.js +++ b/static/js/managers/SettingsManager.js @@ -2,7 +2,7 @@ import { modalManager } from './ModalManager.js'; import { showToast } from '../utils/uiHelpers.js'; import { state } from '../state/index.js'; import { resetAndReload } from '../api/loraApi.js'; -import { setStorageItem } from '../utils/storageHelpers.js'; +import { setStorageItem, getStorageItem } from '../utils/storageHelpers.js'; export class SettingsManager { constructor() { @@ -10,9 +10,24 @@ export class SettingsManager { this.isOpen = false; // Add initialization to sync with modal state + this.currentPage = document.body.dataset.page || 'loras'; + + // Ensure settings are loaded from localStorage + this.loadSettingsFromStorage(); + this.initialize(); } + loadSettingsFromStorage() { + // Get saved settings from localStorage + const savedSettings = getStorageItem('settings'); + + // Apply saved settings to state if available + if (savedSettings) { + state.global.settings = { ...state.global.settings, ...savedSettings }; + } + } + initialize() { if (this.initialized) return; @@ -101,8 +116,16 @@ export class SettingsManager { // Apply frontend settings immediately this.applyFrontendSettings(); - // Reload the loras without updating folders - await resetAndReload(false); + if (this.currentPage === 'loras') { + // Reload the loras without updating folders + await resetAndReload(false); + } else if (this.currentPage === 'recipes') { + // Reload the recipes without updating folders + await window.recipeManager.loadRecipes(); + } else if (this.currentPage === 'checkpoints') { + // Reload the checkpoints without updating folders + await window.checkpointsManager.loadCheckpoints(); + } } catch (error) { showToast('Failed to save settings: ' + error.message, 'error'); } diff --git a/static/js/managers/UpdateService.js b/static/js/managers/UpdateService.js index 0f2b2e8d..aa15fd83 100644 --- a/static/js/managers/UpdateService.js +++ b/static/js/managers/UpdateService.js @@ -8,15 +8,11 @@ export class UpdateService { this.latestVersion = "v0.0.0"; // Initialize with default values this.updateInfo = null; this.updateAvailable = false; - this.updateNotificationsEnabled = getStorageItem('show_update_notifications') !== 'false'; + this.updateNotificationsEnabled = getStorageItem('show_update_notifications'); this.lastCheckTime = parseInt(getStorageItem('last_update_check') || '0'); } initialize() { - // Initialize update preferences from localStorage - const showUpdates = getStorageItem('show_update_notifications'); - this.updateNotificationsEnabled = showUpdates === null || showUpdates === 'true'; - // Register event listener for update notification toggle const updateCheckbox = document.getElementById('updateNotifications'); if (updateCheckbox) { diff --git a/static/js/state/index.js b/static/js/state/index.js index eeeb699b..63a9b446 100644 --- a/static/js/state/index.js +++ b/static/js/state/index.js @@ -1,11 +1,16 @@ // Create the new hierarchical state structure +import { getStorageItem } from '../utils/storageHelpers.js'; + +// Load settings from localStorage or use defaults +const savedSettings = getStorageItem('settings', { + blurMatureContent: true, + show_only_sfw: false +}); + export const state = { // Global state global: { - settings: { - blurMatureContent: true, - show_only_sfw: false - }, + settings: savedSettings, loadingManager: null, observer: null, }, diff --git a/static/js/utils/storageHelpers.js b/static/js/utils/storageHelpers.js index c7654515..970d5491 100644 --- a/static/js/utils/storageHelpers.js +++ b/static/js/utils/storageHelpers.js @@ -74,6 +74,12 @@ export function removeStorageItem(key) { * This should be called once during application initialization */ export function migrateStorageItems() { + // Check if migration has already been performed + if (localStorage.getItem(STORAGE_PREFIX + 'migration_completed')) { + console.log('Lora Manager: Storage migration already completed'); + return; + } + // List of known keys used in the application const knownKeys = [ 'nsfwBlurLevel', @@ -93,18 +99,29 @@ export function migrateStorageItems() { // Migrate each known key knownKeys.forEach(key => { - const value = localStorage.getItem(key); - if (value !== null) { - try { - // Try to parse as JSON first - const parsedValue = JSON.parse(value); - setStorageItem(key, parsedValue); - } catch (e) { - // If not JSON, store as is - setStorageItem(key, value); + const prefixedKey = STORAGE_PREFIX + key; + + // Only migrate if the prefixed key doesn't already exist + if (localStorage.getItem(prefixedKey) === null) { + const value = localStorage.getItem(key); + if (value !== null) { + try { + // Try to parse as JSON first + const parsedValue = JSON.parse(value); + setStorageItem(key, parsedValue); + } catch (e) { + // If not JSON, store as is + setStorageItem(key, value); + } + + // We can optionally remove the old key after migration + localStorage.removeItem(key); } } }); + // Mark migration as completed + localStorage.setItem(STORAGE_PREFIX + 'migration_completed', 'true'); + console.log('Lora Manager: Storage migration completed'); } \ No newline at end of file diff --git a/static/js/utils/uiHelpers.js b/static/js/utils/uiHelpers.js index e038b2e8..4d2a55aa 100644 --- a/static/js/utils/uiHelpers.js +++ b/static/js/utils/uiHelpers.js @@ -177,7 +177,7 @@ export function toggleFolderTags() { // Add this to your existing initialization code export function initFolderTagsVisibility() { - const isCollapsed = getStorageItem('folderTagsCollapsed') === 'true'; + const isCollapsed = getStorageItem('folderTagsCollapsed'); if (isCollapsed) { const folderTags = document.querySelector('.folder-tags'); const toggleBtn = document.querySelector('.toggle-folders-btn i');