fabiobiondi

Angular / Raspberry / AngularFire demo code

Dec 6th, 2021 (edited)
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component } from '@angular/core';
  2. import { AngularFireAuth } from '@angular/fire/compat/auth';
  3. import { AngularFireDatabase } from '@angular/fire/compat/database';
  4. import { Observable, of, iif, switchMap, timer } from 'rxjs';
  5.  
  6. const URL_IMAGE = 'IMAGE PATH;
  7.  
  8. interface MyData {
  9.  ble: {
  10.    temp: number;
  11.    pressure: number;
  12.    altitude: number;
  13.  }
  14. }
  15.  
  16. @Component({
  17.  selector: 'fb-demo-firebase',
  18.  template: `
  19.      <ng-template #showLogin>
  20.        <h1 class="title-xl">Firebase Demo</h1>
  21.        <button (click)="login()" class="btn btn-outline-secondary">
  22.          <i class="fa fa-sign-in me-2"></i>Login with Google
  23.        </button>
  24.      </ng-template>
  25.      
  26.      <div *ngIf="auth.user | async as user; else showLogin">
  27.        <h1 class="title-xl mt-5">
  28.          <i class="fa fa-user"></i>
  29.          Hello {{ user.email | json }}!
  30.        </h1>
  31.        
  32.        <div class="row">
  33.          <div *ngIf="data$ | async as data" class="col">
  34.            <h1 class="title-xl mt-5">
  35.              <i class="fa fa-thermometer-2"></i>
  36.              {{data.ble.temp | number: '1.2-2'}} °
  37.            </h1>
  38.        
  39.            <h1 class="title-xl mt-5">
  40.              <i class="fa fa-arrows-v"></i>
  41.              {{data.ble.altitude | number: '1.2-2'}} cm
  42.            </h1>
  43.            
  44.          </div>
  45.          
  46.          <div class="col">
  47.            <img [src]="url" alt="WebCam" width="100%" style="border-radius: 20px">
  48.          </div>
  49.        </div>
  50.          
  51.        <button (click)="logout()" class="btn btn-outline-secondary">
  52.          <i class="fa fa-sign-out"></i>
  53.          Logout
  54.        </button>
  55.      </div>
  56.  `,
  57. })
  58. export class DemoFirebaseComponent {
  59.  data$: Observable<MyData | null> = this.auth.user
  60.    .pipe(
  61.      switchMap(res => iif(
  62.          () => !!res,
  63.          this.db.object<MyData>(res.uid).valueChanges(),
  64.          of(null)
  65.      ))
  66.    )
  67.  url = URL_IMAGE;
  68.  
  69.  constructor(
  70.    public auth: AngularFireAuth,
  71.    private db: AngularFireDatabase,
  72.  ) {
  73.    timer(0, 1000)
  74.      .subscribe(() => this.url = URL_IMAGE + Math.random())
  75.  }
  76.  
  77.  login() {
  78.    this.auth.signInWithEmailAndPassword('biondi@biondi.com', 'biondi')
  79.  }
  80.  logout() {
  81.    this.auth.signOut();
  82.  }
  83. }
  84.  
Add Comment
Please, Sign In to add comment