Add theme change listener and first startup handling

- Implement `setupThemeChangeListener` in `main.js` to detect theme changes across tabs, save font configurations, and recreate the chart with appropriate styles for mobile and desktop.
- Introduce new functions in `theme.js` to manage theme preferences on first startup, including setting the DeepSea theme as default and checking for previous app launches.
This commit is contained in:
DJObleezy 2025-04-23 13:55:19 -07:00
parent f617342c23
commit a71a6ce03a
2 changed files with 97 additions and 6 deletions

View File

@ -2134,21 +2134,81 @@ $(document).ready(function () {
// Add this function to the document ready section // Add this function to the document ready section
function setupThemeChangeListener() { function setupThemeChangeListener() {
// Listen for storage events to detect theme changes from other tabs/windows
window.addEventListener('storage', function (event) { window.addEventListener('storage', function (event) {
if (event.key === 'useDeepSeaTheme') { if (event.key === 'useDeepSeaTheme') {
// Update chart with new theme colors
if (trendChart) { if (trendChart) {
// Trigger chart update with new colors // Save all font configurations
const fontConfig = {
xTicks: { ...trendChart.options.scales.x.ticks.font },
yTicks: { ...trendChart.options.scales.y.ticks.font },
yTitle: { ...trendChart.options.scales.y.title.font },
tooltip: {
title: { ...trendChart.options.plugins.tooltip.titleFont },
body: { ...trendChart.options.plugins.tooltip.bodyFont }
}
};
// Check if we're on mobile (viewport width < 768px)
const isMobile = window.innerWidth < 768;
// Store the original sizes before destroying chart
const xTicksFontSize = fontConfig.xTicks.size || 14;
const yTicksFontSize = fontConfig.yTicks.size || 14;
const yTitleFontSize = fontConfig.yTitle.size || 16;
// Recreate the chart with new theme colors
trendChart.destroy(); trendChart.destroy();
trendChart = initializeChart(); trendChart = initializeChart();
// Ensure font sizes are explicitly set to original values
// This is especially important for mobile
if (isMobile) {
// On mobile, set explicit font sizes (based on the originals)
trendChart.options.scales.x.ticks.font = {
...fontConfig.xTicks,
size: xTicksFontSize
};
trendChart.options.scales.y.ticks.font = {
...fontConfig.yTicks,
size: yTicksFontSize
};
trendChart.options.scales.y.title.font = {
...fontConfig.yTitle,
size: yTitleFontSize
};
// Also set tooltip font sizes explicitly
trendChart.options.plugins.tooltip.titleFont = {
...fontConfig.tooltip.title,
size: fontConfig.tooltip.title.size || 16
};
trendChart.options.plugins.tooltip.bodyFont = {
...fontConfig.tooltip.body,
size: fontConfig.tooltip.body.size || 14
};
console.log('Mobile device detected: Setting explicit font sizes for chart labels');
} else {
// On desktop, use the full font config objects as before
trendChart.options.scales.x.ticks.font = fontConfig.xTicks;
trendChart.options.scales.y.ticks.font = fontConfig.yTicks;
trendChart.options.scales.y.title.font = fontConfig.yTitle;
trendChart.options.plugins.tooltip.titleFont = fontConfig.tooltip.title;
trendChart.options.plugins.tooltip.bodyFont = fontConfig.tooltip.body;
}
// Update with data and force an immediate chart update
updateChartWithNormalizedData(trendChart, latestMetrics); updateChartWithNormalizedData(trendChart, latestMetrics);
trendChart.update('none');
} }
// Update other UI elements that depend on theme colors // Update refresh button color
updateRefreshButtonColor(); updateRefreshButtonColor();
// Trigger a custom event that other components can listen to // Trigger custom event
$(document).trigger('themeChanged'); $(document).trigger('themeChanged');
} }
}); });

View File

@ -406,11 +406,42 @@ function saveThemePreference(useDeepSea) {
} }
} }
// Check if this is the first startup by checking for the "firstStartup" flag
function isFirstStartup() {
return localStorage.getItem('hasStartedBefore') !== 'true';
}
// Mark that the app has started before
function markAppStarted() {
try {
localStorage.setItem('hasStartedBefore', 'true');
} catch (e) {
console.error("Error marking app as started:", e);
}
}
// Initialize DeepSea as default on first startup
function initializeDefaultTheme() {
if (isFirstStartup()) {
console.log("First startup detected, setting DeepSea as default theme");
saveThemePreference(true); // Set DeepSea theme as default (true)
markAppStarted();
return true;
}
return false;
}
// Check for theme preference in localStorage // Check for theme preference in localStorage
function loadThemePreference() { function loadThemePreference() {
try { try {
// Check if it's first startup - if so, set DeepSea as default
const isFirstTime = initializeDefaultTheme();
// Get theme preference from localStorage
const themePreference = localStorage.getItem('useDeepSeaTheme'); const themePreference = localStorage.getItem('useDeepSeaTheme');
if (themePreference === 'true') {
// Apply theme based on preference
if (themePreference === 'true' || isFirstTime) {
applyDeepSeaTheme(); applyDeepSeaTheme();
} else { } else {
// Make sure the toggle button is styled correctly for Bitcoin theme // Make sure the toggle button is styled correctly for Bitcoin theme