Skip to content

Commit

Permalink
The new logic is more universal and more stable.
Browse files Browse the repository at this point in the history
Now we get in the memory what we see on the display.

+ Fixes issues, when CSS class was set by default and the switch has to change it without making a memory.

+ We now only remember the  CSS class that was given to us by the function call.

+ Fixes memory conflicts, when the memory cell had wrong data, or older switch data.

+ Now we deep clean the switch memory cell on every CSS class switch change, so that no old or wrong data can exist in our switch memory.

This resolves issues, where the memory was manipulated, or had wrong data, or corrupt data, or with manual data input without permission. So, it's allays a fresh switch memory, when we make the new memory. With this we reduce memory errors on the switch side.


// For testing the variables and logic.

 console.log("New Class was : " + ClassName ) ;

 console.log("ClassWasActive was : " + ClassWasActive ) ;

 console.log("OldMemory was : " + OldMemory ) ;


// Have fun future traveler !
  • Loading branch information
Dorson authored Apr 18, 2020
1 parent 672ab79 commit dba7d73
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -299,40 +299,65 @@

function AfterDark() {

var hour = new Date().getHours() ;
let hour = new Date().getHours() ;

if ( !localStorage.getItem( "ColorMode" ) && ( hour >= 20 || hour < 7 ) ) {
let ColorWasSet = localStorage.getItem( "ColorMode" ) ;

document.querySelector("html").classList.add( "dark" ) ;
let DarkWasSet = document.querySelector("html").classList.contains("dark") ;

if ( ( hour >= 20 || hour < 7 ) && !( ColorWasSet || DarkWasSet ) )
{
document.querySelector("html").classList.add( "dark" ) ;
}
}



/* Toggle Switch ON,OFF CSS class + remember it in localStorage. */
/* CSS class Toggle Switch. ON,OFF CSS class + remember in localStorage memory. */


function ClassSwitch( TagName, ClassName, MemoryName ) {

const MemoryContent = localStorage.getItem( MemoryName ) ;

/* If class was already set, then switch it OFF and clean memory */
/* We use const variables to avoid logic errors, if their values change during process. */

const ClassWasActive = document.querySelector( TagName ).classList.contains( ClassName ) ;

const OldMemory = window.localStorage.getItem( MemoryName ) ;


/* If class is New and NOT-active. We remove possible OldMemory class.
Deep Clean memory cell. Activate and remember new CSS class in clean memory.
*/

if ( MemoryContent === ClassName ) {
localStorage.removeItem( MemoryName ) ;
document.querySelector( TagName ).classList.remove( ClassName ) ;
}
if ( !ClassWasActive && ( OldMemory !== ClassName ) ) {

if ( OldMemory ) {
document.querySelector( TagName ).classList.remove( OldMemory ) ;
window.localStorage.removeItem( MemoryName ) ;
}

/* Else we remove old class + clean memory + save, set OR toggle new CSS class */
document.querySelector( TagName ).classList.add( ClassName ) ;
window.localStorage.setItem( MemoryName , ClassName ) ;
}

/* Else CSS class was already active OR the switch memory was not clean.
We toggle given class. Remove possible OldMemory class. Deep Clean memory cell.
*/

else {
document.querySelector( TagName ).classList.remove( MemoryContent ) ;
window.localStorage.removeItem( MemoryName ) ;
else {

localStorage.setItem( MemoryName, ClassName ) ;
document.querySelector( TagName ).classList.toggle( ClassName ) ;
}

return false;
if ( OldMemory ) {
document.querySelector( TagName ).classList.remove( OldMemory ) ;
window.localStorage.removeItem( MemoryName ) ;
}

}

return false ;

}


Expand Down

0 comments on commit dba7d73

Please sign in to comment.