Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.
This repository was archived by the owner on Jul 11, 2022. It is now read-only.

Fix unreadItems loading issues #199

@github-actions

Description

@github-actions

Fix unreadItems loading issues

Values should be repushed after unread Items Count was requested.

Current workaround setTimeout()

// TODO: Fix unreadItems loading issues

import { LocalNotifications } from '@ionic-native/local-notifications/ngx';

import { map, startWith, switchMap, takeUntil } from 'rxjs/operators';
import { interval, forkJoin, Subject, BehaviorSubject, Observable, Subscription } from 'rxjs';

import { AuthService } from '../auth/auth.service';
import { IResponse } from 'src/app/interfaces/response';
import { IRequestOptions } from 'src/app/interfaces/request-options';
import { environment } from 'src/environments/environment';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class NotificationService implements OnDestroy {
  public static readonly updateIntervall = environment.api.updateNotificationsIntervall * 60 * 1000;

  private readonly url = environment.api.url + environment.api.version;
  private readonly pageSize: number = 30;

  private destroy: Subject<any>;
  private _inbox: BehaviorSubject<any>;
  private _achievements: BehaviorSubject<any>;
  private _unreadItemsCount: BehaviorSubject<number>;

  private notificationSubscription: Subscription;

  constructor(
    private http: HttpClient,
    private authService: AuthService,
    private localNotifications: LocalNotifications,
    private router: Router,
  ) {
    this._inbox = new BehaviorSubject<any>([]);
    this._achievements = new BehaviorSubject<any>([]);
    this._unreadItemsCount = new BehaviorSubject<number>(0);
    this.destroy = new Subject<any>();
    this.initNotificationSubscription();
  }

  public ngOnDestroy(): void {
    this.destroy.next();
    this.destroy.complete();
    this.notificationSubscription.unsubscribe();
  }

  private initNotificationSubscription(): void {
    // On notification click navigate to notifications tab
    if(window.hasOwnProperty('cordova'))
    this.localNotifications
      .on('click')
      .pipe(takeUntil(this.destroy))
      .subscribe(() => this.router.navigateByUrl('/menu/pages/tabs/notification'));

    // Rethreave new notifications every {{NotificationService.updateIntervall}} seconds
    interval(NotificationService.updateIntervall).pipe(
      startWith(() => forkJoin([
          this.getInbox(),
          this.getAchievements(),
        ])
      ),
      switchMap(() => forkJoin([
          this.getInbox(),
          this.getAchievements(),
        ])
      ),
      takeUntil(this.destroy),
    ).subscribe(([inbox, achievements]) => {
      this._inbox.next(inbox.items);
      this._achievements.next(achievements.items);

      const unreadItemsCount = this.countUnread(inbox.items) + this.countUnread(achievements.items);
      this.sendUnreadNotification(unreadItemsCount);
      this._unreadItemsCount.next(unreadItemsCount);
    });
  }

  public inbox(): Observable<any> {
    // TODO: Fix inbox loading issues
    // Currently the notification component displays nothing when called after the app
    // was opened within NotificationService.updateIntervall.
    // Values should be repushed after inbox was requested.
    // > Current workaround `setTimeout()`
    setTimeout(() => this._inbox.next(this._inbox.getValue()), 200);
    return this._inbox.asObservable();
  }

  public achievements(): Observable<any> {
    // TODO: Fix achievements loading issues
    // Currently the notification component displays nothing when called after the app
    // was opened within NotificationService.updateIntervall.
    // Values should be repushed after achievements was requested.
    // > Current workaround `setTimeout()`
    setTimeout(() => this._achievements.next(this._achievements.getValue()), 200);
    return this._achievements.asObservable();
  }

  public unreadItemsCount(): Observable<any> {
    // TODO: Fix unreadItems loading issues
    // Values should be repushed after unread Items Count was requested.
    // > Current workaround `setTimeout()`
    setTimeout(() => this._unreadItemsCount.next(this._unreadItemsCount.getValue()), 200);
    return this._unreadItemsCount.asObservable();
  }

  public getInbox(options?: IRequestOptions): Observable<IResponse> {
    const headers = new HttpHeaders()

00799a81dd661ffc28aaa949af9f8b25699b8a3c

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions