From ea716bfb69643739e36c7ca9ada9fd6a64c4db79 Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <> Date: Fri, 27 Dec 2024 00:09:19 -0700 Subject: [PATCH] Implement notification mechanism --- .../service/LibraryProcessingService.java | 1 + .../src/main/resources/application.yaml | 2 +- booklore-ui/src/app/app.component.ts | 7 +++- .../layout/app.topbar.component.html | 27 ++++++------ .../layout/app.topbar.component.scss | 39 +++++++++++++++++ .../component/layout/app.topbar.component.ts | 36 +++++++++++++--- .../notification/notification.component.html | 6 --- .../notification/notification.component.scss | 3 -- .../notification/notification.component.ts | 42 ------------------- .../src/app/book/service/event.service.ts | 36 ++++++++++++++++ 10 files changed, 129 insertions(+), 70 deletions(-) delete mode 100644 booklore-ui/src/app/book/component/notification/notification.component.html delete mode 100644 booklore-ui/src/app/book/component/notification/notification.component.scss delete mode 100644 booklore-ui/src/app/book/component/notification/notification.component.ts create mode 100644 booklore-ui/src/app/book/service/event.service.ts diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java index 7c73bbe21..d35b981fd 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/LibraryProcessingService.java @@ -69,6 +69,7 @@ public class LibraryProcessingService { if (bookDTO != null) { BookNotification notification = BookNotification.builder().action(BOOK_ADDED).addedBook(bookDTO).build(); notificationService.sendMessage("/topic/books", notification); + notificationService.sendMessage("/topic/logs", "Book added: " + bookDTO.getFileName()); log.info("Processed file: {}", libraryFile.getFilePath()); } } diff --git a/booklore-api/src/main/resources/application.yaml b/booklore-api/src/main/resources/application.yaml index 7ab7156f0..5cf7224fe 100644 --- a/booklore-api/src/main/resources/application.yaml +++ b/booklore-api/src/main/resources/application.yaml @@ -22,7 +22,7 @@ spring: hibernate: naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl - show-sql: true + show-sql: false properties: hibernate: jdbc: diff --git a/booklore-ui/src/app/app.component.ts b/booklore-ui/src/app/app.component.ts index f8cb002dd..e823cb725 100644 --- a/booklore-ui/src/app/app.component.ts +++ b/booklore-ui/src/app/app.component.ts @@ -3,6 +3,7 @@ import {RxStompService} from './book/service/rx-stomp.service'; import {Message} from '@stomp/stompjs'; import {BookService} from './book/service/book.service'; import {Action, parseBookNotification} from './book/model/book-notification.model'; +import {EventService} from './book/service/event.service'; @Component({ selector: 'app-root', @@ -12,7 +13,7 @@ import {Action, parseBookNotification} from './book/model/book-notification.mode }) export class AppComponent implements OnInit { - constructor(private bookService: BookService, private rxStompService: RxStompService) { + constructor(private bookService: BookService, private rxStompService: RxStompService, private eventService: EventService) { } ngOnInit(): void { @@ -24,6 +25,10 @@ export class AppComponent implements OnInit { this.bookService.handleRemovedBookIds(notification.removedBookIds!); } }); + + this.rxStompService.watch('/topic/logs').subscribe((message: Message) => { + this.eventService.handleIncomingEvent(message.body); + }); } sendMessage() { diff --git a/booklore-ui/src/app/book/component/layout/app.topbar.component.html b/booklore-ui/src/app/book/component/layout/app.topbar.component.html index 9205fb1e8..089eef5e8 100644 --- a/booklore-ui/src/app/book/component/layout/app.topbar.component.html +++ b/booklore-ui/src/app/book/component/layout/app.topbar.component.html @@ -1,8 +1,6 @@
- @@ -19,20 +17,25 @@
- + +
+ {{ latestEvent }} +
+ - -
diff --git a/booklore-ui/src/app/book/component/layout/app.topbar.component.scss b/booklore-ui/src/app/book/component/layout/app.topbar.component.scss index e69de29bb..0e2f765a9 100644 --- a/booklore-ui/src/app/book/component/layout/app.topbar.component.scss +++ b/booklore-ui/src/app/book/component/layout/app.topbar.component.scss @@ -0,0 +1,39 @@ +.live-event-container { + position: absolute; + top: 50px; + right: 0; + width: 500px; + background: var(--surface-border); + border: 1px solid var(--primary-800); + border-radius: 4px; + padding: 10px; + box-shadow: 0px 3px 5px rgba(0, 0, 0, .02), 0px 0px 2px rgba(0, 0, 0, .05), 0px 1px 4px rgba(0, 0, 0, .08); + color: var(--text-color); + overflow-x: hidden; + z-index: 1000; + word-wrap: break-word; + white-space: normal; +} + +.highlight { + color: var(--yellow-400) !important; +} + +@keyframes pulsate { + 0% { + transform: scale(1); + opacity: 1; + } + 50% { + transform: scale(1.25); + opacity: 0.8; + } + 100% { + transform: scale(1); + opacity: 1; + } +} + +.pulsating { + animation: pulsate 1.5s infinite ease-in-out; +} diff --git a/booklore-ui/src/app/book/component/layout/app.topbar.component.ts b/booklore-ui/src/app/book/component/layout/app.topbar.component.ts index 290782e66..0a398f825 100644 --- a/booklore-ui/src/app/book/component/layout/app.topbar.component.ts +++ b/booklore-ui/src/app/book/component/layout/app.topbar.component.ts @@ -9,31 +9,56 @@ import {TooltipModule} from 'primeng/tooltip'; import {FormsModule} from '@angular/forms'; import {InputTextModule} from 'primeng/inputtext'; import {SearchComponent} from '../search/search.component'; -import {NotificationComponent} from '../notification/notification.component'; import {FileUploadComponent} from '../file-upload/file-upload.component'; +import {NgClass, NgIf} from '@angular/common'; +import {EventService} from '../../service/event.service'; @Component({ selector: 'app-topbar', + templateUrl: './app.topbar.component.html', + styleUrls: ['./app.topbar.component.scss'], imports: [ RouterLink, TooltipModule, FormsModule, InputTextModule, SearchComponent, - NotificationComponent, - ThemeSwitcherComponent + ThemeSwitcherComponent, + NgIf ], - templateUrl: './app.topbar.component.html', }) export class AppTopBarComponent implements OnDestroy { items!: MenuItem[]; ref: DynamicDialogRef | undefined; + latestEvent: string = 'No recent events...'; + eventHighlight: boolean = false; + showEvents: boolean = false; + eventTimeout: any; + @ViewChild('menubutton') menuButton!: ElementRef; @ViewChild('topbarmenubutton') topbarMenuButton!: ElementRef; @ViewChild('topbarmenu') menu!: ElementRef; - constructor(public layoutService: LayoutService, public dialogService: PrimeDialogService) { + constructor(public layoutService: LayoutService, public dialogService: PrimeDialogService, private eventService: EventService) { + this.eventService.latestEvent$.subscribe(event => { + this.latestEvent = event; + }); + this.eventService.eventHighlight$.subscribe(highlight => { + this.eventHighlight = highlight; + }); + } + + onMouseEnter() { + this.showEvents = true; + } + + onMouseLeave() { + this.showEvents = false; + } + + toggleEventDisplay(): void { + this.showEvents = !this.showEvents; } openLibraryCreatorDialog(): void { @@ -60,5 +85,6 @@ export class AppTopBarComponent implements OnDestroy { if (this.ref) { this.ref.close(); } + clearTimeout(this.eventTimeout); } } diff --git a/booklore-ui/src/app/book/component/notification/notification.component.html b/booklore-ui/src/app/book/component/notification/notification.component.html deleted file mode 100644 index e4968b3c0..000000000 --- a/booklore-ui/src/app/book/component/notification/notification.component.html +++ /dev/null @@ -1,6 +0,0 @@ - diff --git a/booklore-ui/src/app/book/component/notification/notification.component.scss b/booklore-ui/src/app/book/component/notification/notification.component.scss deleted file mode 100644 index 019770115..000000000 --- a/booklore-ui/src/app/book/component/notification/notification.component.scss +++ /dev/null @@ -1,3 +0,0 @@ -.glowing-icon { - color: yellow; -} diff --git a/booklore-ui/src/app/book/component/notification/notification.component.ts b/booklore-ui/src/app/book/component/notification/notification.component.ts deleted file mode 100644 index 7a1da508f..000000000 --- a/booklore-ui/src/app/book/component/notification/notification.component.ts +++ /dev/null @@ -1,42 +0,0 @@ -import {Component, OnInit, OnDestroy} from '@angular/core'; -import {Subscription} from 'rxjs'; -import {NgClass} from '@angular/common'; -import {BookUpdateEvent} from '../../model/book.model'; - -@Component({ - selector: 'app-notification', - templateUrl: './notification.component.html', - imports: [ - NgClass - ], - styleUrls: ['./notification.component.scss'] -}) -export class NotificationComponent implements OnInit, OnDestroy { - glowing: boolean = false; - private eventSubscription!: Subscription; - - ngOnInit(): void { - /*this.eventSubscription = this.bookProgressService.connect(1).subscribe({ - next: (event: BookUpdateEvent) => { - this.handleNewNotification(event); - }, - error: (error) => { - console.error('Error receiving progress updates:', error); - } - });*/ - } - - handleNewNotification(event: BookUpdateEvent): void { - this.glowing = true; - setTimeout(() => { - this.glowing = false; - console.log("reset") - }, 200); - } - - ngOnDestroy(): void { - if (this.eventSubscription) { - this.eventSubscription.unsubscribe(); - } - } -} diff --git a/booklore-ui/src/app/book/service/event.service.ts b/booklore-ui/src/app/book/service/event.service.ts new file mode 100644 index 000000000..7263e087a --- /dev/null +++ b/booklore-ui/src/app/book/service/event.service.ts @@ -0,0 +1,36 @@ +import { Injectable } from '@angular/core'; +import { BehaviorSubject } from 'rxjs'; + +@Injectable({ + providedIn: 'root', +}) +export class EventService { + private latestEventSubject = new BehaviorSubject('No recent events...'); + latestEvent$ = this.latestEventSubject.asObservable(); + + private eventHighlightSubject = new BehaviorSubject(false); + eventHighlight$ = this.eventHighlightSubject.asObservable(); + + private eventTimeout: any; + + constructor() {} + + handleIncomingEvent(event: string): void { + this.latestEventSubject.next(event); + this.eventHighlightSubject.next(true); + + if (this.eventTimeout) { + clearTimeout(this.eventTimeout); + } + + this.eventTimeout = setTimeout(() => { + this.eventHighlightSubject.next(false); + }, 1000); + + setTimeout(() => { + if (!this.eventHighlightSubject.value) { + this.latestEventSubject.next('No recent events...'); + } + }, 2500); + } +}