From 0f542c3e3b1c12ea61ac37369c25808eb60a6987 Mon Sep 17 00:00:00 2001 From: piotrpekala7 <31202938+piotrpekala7@users.noreply.github.com> Date: Mon, 24 Feb 2020 11:55:23 +0100 Subject: [PATCH] Dynamic AdButler component --- src/app/app.module.ts | 3 ++- .../components/adbutler/adbutler.component.ts | 7 +++--- .../notification-box.component.html | 2 +- .../notification-box.component.ts | 23 +++++++++++++++++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 55735f9c..0a8843dc 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -580,7 +580,8 @@ if (environment.production) { NavigationDialogComponent, ScreenshotDialogComponent, ConfirmationBottomSheetComponent, - ConfigDialogComponent + ConfigDialogComponent, + AdbutlerComponent ], bootstrap: [AppComponent] }) diff --git a/src/app/components/adbutler/adbutler.component.ts b/src/app/components/adbutler/adbutler.component.ts index f0ee27a7..acee2902 100644 --- a/src/app/components/adbutler/adbutler.component.ts +++ b/src/app/components/adbutler/adbutler.component.ts @@ -11,13 +11,12 @@ import { ToasterService } from '../../services/toaster.service'; }) export class AdbutlerComponent implements OnInit { @ViewChild('ad', {static: false}) ad: ElementRef; - @Input() theme: string; - @Output() onLoad = new EventEmitter(); + theme: string; + onLoad = new EventEmitter(); htmlCode: string; constructor( - private httpClient: HttpClient, - private toasterService: ToasterService + private httpClient: HttpClient ) {} ngOnInit() { diff --git a/src/app/components/notification-box/notification-box.component.html b/src/app/components/notification-box/notification-box.component.html index c5bcafef..350fe7ce 100644 --- a/src/app/components/notification-box/notification-box.component.html +++ b/src/app/components/notification-box/notification-box.component.html @@ -2,7 +2,7 @@
- + close
diff --git a/src/app/components/notification-box/notification-box.component.ts b/src/app/components/notification-box/notification-box.component.ts index a07bd770..416981a7 100644 --- a/src/app/components/notification-box/notification-box.component.ts +++ b/src/app/components/notification-box/notification-box.component.ts @@ -1,6 +1,7 @@ -import { Component, OnInit, OnDestroy } from '@angular/core'; +import { Component, OnInit, OnDestroy, ComponentFactoryResolver, ViewContainerRef, ViewChild } from '@angular/core'; import { timer, Observable, Subscription } from 'rxjs'; import { ThemeService } from '../../services/theme.service'; +import { AdbutlerComponent } from '../adbutler/adbutler.component'; @Component({ selector: 'app-notification-box', @@ -8,6 +9,8 @@ import { ThemeService } from '../../services/theme.service'; styleUrls: ['./notification-box.component.scss'] }) export class NotificationBoxComponent implements OnInit, OnDestroy { + @ViewChild('dynamicComponentContainer', {read: ViewContainerRef, static: false}) dynamicComponentContainer; + timer: Observable; viewTimer: Observable; timerSubscription: Subscription; @@ -26,7 +29,9 @@ export class NotificationBoxComponent implements OnInit, OnDestroy { isLightThemeEnabled: boolean = false; constructor( - private themeService: ThemeService + private themeService: ThemeService, + private componentFactoryResolver: ComponentFactoryResolver, + private viewContainerRef: ViewContainerRef ){} ngOnInit() { @@ -34,6 +39,20 @@ export class NotificationBoxComponent implements OnInit, OnDestroy { this.themeService.getActualTheme() === 'light' ? this.isLightThemeEnabled = true : this.isLightThemeEnabled = false; } + ngAfterViewInit() { + this.createDynamicAdComponent(); + } + + createDynamicAdComponent() : void { + const factory = this.componentFactoryResolver.resolveComponentFactory(AdbutlerComponent); + const componentRef = this.dynamicComponentContainer.createComponent(factory); + componentRef.instance.theme = this.themeService.getActualTheme() === 'light'; + componentRef.instance.onLoad.subscribe(event => { + this.onLoadingAdbutler(event); + }) + componentRef.changeDetectorRef.detectChanges(); + } + startTimer() { this.timer = timer(this.delayTime, 1000);