From f1c1b0c6c61b0dd244ac3d2b37036c5d7900ae35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique?= Date: Mon, 28 Jul 2025 16:37:45 -0300 Subject: [PATCH] fixing multiple wallpapers --- App.tsx | 30 ++++++++++++++++-------------- components/ConfigurationModal.tsx | 23 ++++++++++++++++++++--- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/App.tsx b/App.tsx index 6c76a7e..0ab3781 100755 --- a/App.tsx +++ b/App.tsx @@ -94,7 +94,8 @@ const App: React.FC = () => { const updateWallpaper = () => { const availableWallpapers = allWallpapers.filter(w => config.backgroundUrls.includes(w.url || w.base64)); if (availableWallpapers.length > 0) { - const currentIndex = availableWallpapers.findIndex(w => (w.url || w.base64) === wallpaperState.current); + const currentWallpaperFromState = allWallpapers.find(w => w.name === wallpaperState.current); + const currentIndex = currentWallpaperFromState ? availableWallpapers.findIndex(w => w.name === currentWallpaperFromState.name) : -1; const nextIndex = (currentIndex + 1) % availableWallpapers.length; const newWallpaper = availableWallpapers[nextIndex]; const newWallpaperUrl = newWallpaper.url || newWallpaper.base64; @@ -102,24 +103,20 @@ const App: React.FC = () => { localStorage.setItem('wallpaperState', JSON.stringify({ current: newWallpaper.name, lastChanged: new Date().toISOString() })); } else { setCurrentWallpaper(''); + localStorage.removeItem('wallpaperState'); } }; - if (Date.now() - lastChanged > frequency) { + const currentWallpaperDetails = allWallpapers.find(w => w.name === wallpaperState.current); + const isCurrentWallpaperValid = currentWallpaperDetails && config.backgroundUrls.includes(currentWallpaperDetails.url || currentWallpaperDetails.base64 || ''); + + if (!isCurrentWallpaperValid || Date.now() - lastChanged > frequency) { updateWallpaper(); + } else if (currentWallpaperDetails) { + setCurrentWallpaper(currentWallpaperDetails.url || currentWallpaperDetails.base64 || ''); } else { - const currentWallpaperName = wallpaperState.current; - const wallpaper = allWallpapers.find(w => w.name === currentWallpaperName); - if (wallpaper) { - setCurrentWallpaper(wallpaper.url || wallpaper.base64 || ''); - } else { - const firstWallpaperUrl = config.backgroundUrls[0] || ''; - const firstWallpaper = allWallpapers.find(w => (w.url || w.base64) === firstWallpaperUrl); - setCurrentWallpaper(firstWallpaperUrl); - if (firstWallpaper) { - localStorage.setItem('wallpaperState', JSON.stringify({ current: firstWallpaper.name, lastChanged: new Date().toISOString() })); - } - } + // Fallback for when there's no valid wallpaper state + updateWallpaper(); } }, [config.backgroundUrls, config.wallpaperFrequency, allWallpapers]); @@ -133,6 +130,10 @@ const App: React.FC = () => { setIsConfigModalOpen(false); }; + const handleWallpaperChange = (newConfig: Partial) => { + setConfig(prev => ({ ...prev, ...newConfig })); + }; + const handleSaveWebsite = (website: Partial) => { if (editingWebsite) { const newCategories = categories.map(category => ({ @@ -341,6 +342,7 @@ const App: React.FC = () => { currentConfig={config} onClose={() => setIsConfigModalOpen(false)} onSave={handleSaveConfig} + onWallpaperChange={handleWallpaperChange} /> )} diff --git a/components/ConfigurationModal.tsx b/components/ConfigurationModal.tsx index 9622fdf..f4866d0 100644 --- a/components/ConfigurationModal.tsx +++ b/components/ConfigurationModal.tsx @@ -10,9 +10,10 @@ interface ConfigurationModalProps { onClose: () => void; onSave: (config: any) => void; currentConfig: any; + onWallpaperChange: (newConfig: Partial) => void; } -const ConfigurationModal: React.FC = ({ onClose, onSave, currentConfig }) => { +const ConfigurationModal: React.FC = ({ onClose, onSave, currentConfig, onWallpaperChange }) => { const [config, setConfig] = useState({ ...currentConfig, titleSize: currentConfig.titleSize || 'medium', @@ -47,6 +48,7 @@ const ConfigurationModal: React.FC = ({ onClose, onSave const [userWallpapers, setUserWallpapers] = useState([]); const menuRef = useRef(null); const fileInputRef = useRef(null); + const isSaving = useRef(false); const [isVisible, setIsVisible] = useState(false); useEffect(() => { @@ -64,6 +66,14 @@ const ConfigurationModal: React.FC = ({ onClose, onSave return () => clearTimeout(timer); }, []); + useEffect(() => { + return () => { + if (!isSaving.current) { + onWallpaperChange({ backgroundUrls: currentConfig.backgroundUrls }); + } + }; + }, []); + const handleClose = () => { setIsVisible(false); setTimeout(() => { @@ -90,6 +100,10 @@ const ConfigurationModal: React.FC = ({ onClose, onSave } }; + useEffect(() => { + onWallpaperChange({ backgroundUrls: config.backgroundUrls }); + }, [config.backgroundUrls]); + const handleClockToggleChange = (checked: boolean) => { setConfig({ ...config, clock: { ...config.clock, enabled: checked } }); }; @@ -201,7 +215,10 @@ const ConfigurationModal: React.FC = ({ onClose, onSave localStorage.setItem('userWallpapers', JSON.stringify(updatedUserWallpapers)); const newBackgroundUrls = config.backgroundUrls.filter((url: string) => url !== wallpaperIdentifier); - setConfig({ ...config, backgroundUrls: newBackgroundUrls }); + + const newConfig = { ...config, backgroundUrls: newBackgroundUrls }; + setConfig(newConfig); + onWallpaperChange({ backgroundUrls: newBackgroundUrls }); }; const allWallpapers = [...baseWallpapers, ...userWallpapers]; @@ -629,7 +646,7 @@ const ConfigurationModal: React.FC = ({ onClose, onSave
-