Skip to content

Commit

Permalink
darkmode now functional + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leoratte committed Aug 16, 2020
1 parent df5dbf3 commit 17e8899
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# led_control_app
android app for https://github.com/leoratte/led_control
web and android app for https://github.com/leoratte/led_control
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "led_control",
"version": "0.1.0",
"version": "0.2.0",
"author": "Leonard Anderweit",
"homepage": "https://leoratte.de/",
"scripts": {
Expand Down
9 changes: 8 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ import { ManageLedComponent } from './components/manage-led/manage-led.component
import { FormsModule } from '@angular/forms';
import { MenuComponent } from './components/menu/menu.component';
import { AnimationCreationComponent } from './components/animation-creation/animation-creation.component';
import { DarkmodeToggleComponent } from './components/darkmode-toggle/darkmode-toggle.component';


@NgModule({
declarations: [AppComponent, ManageLedComponent, MenuComponent, AnimationCreationComponent],
declarations: [
AppComponent,
ManageLedComponent,
MenuComponent,
AnimationCreationComponent,
DarkmodeToggleComponent
],
entryComponents: [],
imports: [
BrowserModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ion-toggle (ionChange)="this.update($event.detail.checked)" [(checked)]="this.darkmode" slot="end"></ion-toggle>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { IonicModule } from '@ionic/angular';

import { DarkmodeToggleComponent } from './darkmode-toggle.component';

describe('DarkmodeToggleComponent', () => {
let component: DarkmodeToggleComponent;
let fixture: ComponentFixture<DarkmodeToggleComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DarkmodeToggleComponent ],
imports: [IonicModule.forRoot()]
}).compileComponents();

fixture = TestBed.createComponent(DarkmodeToggleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));

it('should create', () => {
expect(component).toBeTruthy();
});
});
51 changes: 51 additions & 0 deletions src/app/components/darkmode-toggle/darkmode-toggle.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Component, OnInit } from '@angular/core';
import { StorageService } from 'src/app/services/storage.service';

const DARKMODE_KEY = 'darkmode';
@Component({
selector: 'app-darkmode-toggle',
templateUrl: './darkmode-toggle.component.html',
styleUrls: ['./darkmode-toggle.component.scss'],
})
export class DarkmodeToggleComponent implements OnInit {
public darkmode: boolean;
constructor(
private storageService: StorageService
) {
this.load();
}

ngOnInit() {}

load(): void {
this.storageService
.load(DARKMODE_KEY)
.then((darkmode: boolean) =>{
if(darkmode != null) {
this.darkmode = darkmode;
} else {
this.darkmode = window.matchMedia('(prefers-color-scheme: dark)').matches;
}
this.apply();
});
}

storeDarkmode(): void {
this.storageService.store(DARKMODE_KEY, this.darkmode);
}

update(darkmode: boolean): void {
this.darkmode = darkmode;
this.storeDarkmode();
this.apply();
}

apply(): void {
if(this.darkmode){
document.body.setAttribute('data-theme', 'dark');
}
else{
document.body.setAttribute('data-theme', 'light');
}
}
}
2 changes: 1 addition & 1 deletion src/app/components/menu/menu.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ion-item>
<ion-icon name="moon" slot="start"></ion-icon>
<ion-label>darkmode</ion-label>
<ion-toggle (ionChange)="onClick($event)" slot="end"></ion-toggle>
<app-darkmode-toggle></app-darkmode-toggle>
</ion-item>
<ion-item button (click)="manageLed()">
<ion-icon name="settings-outline" slot="start"></ion-icon>
Expand Down
10 changes: 4 additions & 6 deletions src/app/components/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export class MenuComponent implements OnInit {

constructor(
private websocketService: WebsocketService,
private selectLedService: SelectLedService) { }
private selectLedService: SelectLedService
) {

}

ngOnInit() {}

Expand All @@ -24,11 +27,6 @@ export class MenuComponent implements OnInit {
this.websocketService.connect();
}

onClick(event: any) {
const systemDark = window.matchMedia('(prefers-color-scheme: dark)');
// console.log(systemDark);
}

manageLed(): void {
this.selectLedService.presentManageLed();
}
Expand Down
7 changes: 5 additions & 2 deletions src/app/services/colors.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ export class ColorsService {

load(): void {
this.storageService.load(COLOR_KEY).then((colors: string[]) => {
for (const iterator of colors) {
this.addColor(iterator);
if(colors != null){
for (const iterator of colors) {
this.addColor(iterator);
}
}

});
}

Expand Down
7 changes: 5 additions & 2 deletions src/app/services/select-led.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ export class SelectLedService {

load(): void {
this.storageService.load(LEDMAP_KEY).then((ledmap: Led[]) => {
if (ledmap.length === 5) {
this.ledmap = ledmap;
if (ledmap != null) {
if (ledmap.length === 5) {
this.ledmap = ledmap;
}
}

});
}

Expand Down
9 changes: 4 additions & 5 deletions src/theme/variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@
--ion-color-tint: var(--ion-color-blue-tint);
}

@media (prefers-color-scheme: dark) {
/*
* Dark Colors
* -------------------------------------------
*/

body {
body[data-theme="dark"] {
--ion-color-primary: #428cff;
--ion-color-primary-rgb: 66,140,255;
--ion-color-primary-contrast: #ffffff;
Expand Down Expand Up @@ -197,7 +196,7 @@
* -------------------------------------------
*/

.ios body {
.ios body[data-theme="dark"] {
--ion-background-color: #000000;
--ion-background-color-rgb: 0,0,0;

Expand Down Expand Up @@ -236,7 +235,7 @@
* -------------------------------------------
*/

.md body {
.md body[data-theme="dark"] {
--ion-background-color: #121212;
--ion-background-color-rgb: 18,18,18;

Expand Down Expand Up @@ -271,4 +270,4 @@
ion-title.title-large {
--color: white;
}
}

0 comments on commit 17e8899

Please sign in to comment.