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.
This commit is contained in:
Will Miao
2025-03-22 08:46:36 +08:00
parent 19ef73a07f
commit 372d74ec71
6 changed files with 63 additions and 23 deletions

View File

@@ -63,7 +63,6 @@ export async function loadMoreLoras(resetPage = false, updateFolders = false) {
} }
const data = await response.json(); const data = await response.json();
console.log('Received data:', data);
if (data.items.length === 0 && pageState.currentPage === 1) { if (data.items.length === 0 && pageState.currentPage === 1) {
const grid = document.getElementById('loraGrid'); const grid = document.getElementById('loraGrid');

View File

@@ -2,7 +2,7 @@ import { modalManager } from './ModalManager.js';
import { showToast } from '../utils/uiHelpers.js'; import { showToast } from '../utils/uiHelpers.js';
import { state } from '../state/index.js'; import { state } from '../state/index.js';
import { resetAndReload } from '../api/loraApi.js'; import { resetAndReload } from '../api/loraApi.js';
import { setStorageItem } from '../utils/storageHelpers.js'; import { setStorageItem, getStorageItem } from '../utils/storageHelpers.js';
export class SettingsManager { export class SettingsManager {
constructor() { constructor() {
@@ -10,9 +10,24 @@ export class SettingsManager {
this.isOpen = false; this.isOpen = false;
// Add initialization to sync with modal state // Add initialization to sync with modal state
this.currentPage = document.body.dataset.page || 'loras';
// Ensure settings are loaded from localStorage
this.loadSettingsFromStorage();
this.initialize(); 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() { initialize() {
if (this.initialized) return; if (this.initialized) return;
@@ -101,8 +116,16 @@ export class SettingsManager {
// Apply frontend settings immediately // Apply frontend settings immediately
this.applyFrontendSettings(); this.applyFrontendSettings();
// Reload the loras without updating folders if (this.currentPage === 'loras') {
await resetAndReload(false); // 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) { } catch (error) {
showToast('Failed to save settings: ' + error.message, 'error'); showToast('Failed to save settings: ' + error.message, 'error');
} }

View File

@@ -8,15 +8,11 @@ export class UpdateService {
this.latestVersion = "v0.0.0"; // Initialize with default values this.latestVersion = "v0.0.0"; // Initialize with default values
this.updateInfo = null; this.updateInfo = null;
this.updateAvailable = false; this.updateAvailable = false;
this.updateNotificationsEnabled = getStorageItem('show_update_notifications') !== 'false'; this.updateNotificationsEnabled = getStorageItem('show_update_notifications');
this.lastCheckTime = parseInt(getStorageItem('last_update_check') || '0'); this.lastCheckTime = parseInt(getStorageItem('last_update_check') || '0');
} }
initialize() { 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 // Register event listener for update notification toggle
const updateCheckbox = document.getElementById('updateNotifications'); const updateCheckbox = document.getElementById('updateNotifications');
if (updateCheckbox) { if (updateCheckbox) {

View File

@@ -1,11 +1,16 @@
// Create the new hierarchical state structure // 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 = { export const state = {
// Global state // Global state
global: { global: {
settings: { settings: savedSettings,
blurMatureContent: true,
show_only_sfw: false
},
loadingManager: null, loadingManager: null,
observer: null, observer: null,
}, },

View File

@@ -74,6 +74,12 @@ export function removeStorageItem(key) {
* This should be called once during application initialization * This should be called once during application initialization
*/ */
export function migrateStorageItems() { 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 // List of known keys used in the application
const knownKeys = [ const knownKeys = [
'nsfwBlurLevel', 'nsfwBlurLevel',
@@ -93,18 +99,29 @@ export function migrateStorageItems() {
// Migrate each known key // Migrate each known key
knownKeys.forEach(key => { knownKeys.forEach(key => {
const value = localStorage.getItem(key); const prefixedKey = STORAGE_PREFIX + key;
if (value !== null) {
try { // Only migrate if the prefixed key doesn't already exist
// Try to parse as JSON first if (localStorage.getItem(prefixedKey) === null) {
const parsedValue = JSON.parse(value); const value = localStorage.getItem(key);
setStorageItem(key, parsedValue); if (value !== null) {
} catch (e) { try {
// If not JSON, store as is // Try to parse as JSON first
setStorageItem(key, value); 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'); console.log('Lora Manager: Storage migration completed');
} }

View File

@@ -177,7 +177,7 @@ export function toggleFolderTags() {
// Add this to your existing initialization code // Add this to your existing initialization code
export function initFolderTagsVisibility() { export function initFolderTagsVisibility() {
const isCollapsed = getStorageItem('folderTagsCollapsed') === 'true'; const isCollapsed = getStorageItem('folderTagsCollapsed');
if (isCollapsed) { if (isCollapsed) {
const folderTags = document.querySelector('.folder-tags'); const folderTags = document.querySelector('.folder-tags');
const toggleBtn = document.querySelector('.toggle-folders-btn i'); const toggleBtn = document.querySelector('.toggle-folders-btn i');