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

Fix inbox loading issues #197

Open
github-actions bot opened this issue Mar 9, 2021 · 0 comments
Open

Fix inbox loading issues #197

github-actions bot opened this issue Mar 9, 2021 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Mar 9, 2021

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()

// TODO: Fix inbox loading issues

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';

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()

7976d1041626774b6a73819f9023e9e682d19d42

@github-actions github-actions bot added the todo label Mar 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

0 participants