Skip to content

Commit

Permalink
feat: add synchronised multi-badge
Browse files Browse the repository at this point in the history
  • Loading branch information
jakerenzella committed May 22, 2024
1 parent 43de6ea commit f70ebdc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/app/common/unit-code/unit-code.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {trigger, state, style, animate, transition} from '@angular/animations';
import {Component, Input, OnDestroy, OnInit} from '@angular/core';
import {Subscription, interval} from 'rxjs';
import {Subscription} from 'rxjs';
import {UnitCodeService} from './unit-code.service';

@Component({
selector: 'f-unit-code',
Expand Down Expand Up @@ -29,6 +30,8 @@ export class UnitCodeComponent implements OnInit, OnDestroy {
showState = 'in'; // Animation state
subscription: Subscription;

constructor(private unitCodeService: UnitCodeService) {}

get isDualBadge() {
return this.unit_code?.includes('/');
}
Expand All @@ -42,18 +45,17 @@ export class UnitCodeComponent implements OnInit, OnDestroy {
this.width += 24;
}

this.startFlipping();
this.subscription = this.unitCodeService.getInterval().subscribe(() => {
this.flip();
});
}

startFlipping() {
const source = interval(3000);
this.subscription = source.subscribe(() => {
this.showState = 'out'; // Trigger animation out
setTimeout(() => {
this.currentIndex = (this.currentIndex + 1) % this.unitCodeParts.length;
this.showState = 'in'; // Trigger animation in after a delay
}, 200); // Delay to match the animation duration
});
flip() {
this.showState = 'out'; // Trigger animation out
setTimeout(() => {
this.currentIndex = (this.currentIndex + 1) % this.unitCodeParts.length;
this.showState = 'in'; // Trigger animation in after a delay
}, 200); // Delay to match the animation duration
}

ngOnDestroy() {
Expand Down
20 changes: 20 additions & 0 deletions src/app/common/unit-code/unit-code.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Injectable} from '@angular/core';
import {Observable, interval, shareReplay} from 'rxjs';

// we have a service so that we can share the same interval observable among multiple subscribers
// simply, all unitCodes will tick over at the same time
@Injectable({
providedIn: 'root',
})
export class UnitCodeService {
private readonly interval$: Observable<number>;

constructor() {
// we use shareReplay to share the same interval observable among multiple subscribers
this.interval$ = interval(3000).pipe(shareReplay({bufferSize: 1, refCount: true}));
}

getInterval(): Observable<number> {
return this.interval$;
}
}

0 comments on commit f70ebdc

Please sign in to comment.