import { Injectable } from '@angular/core' ; import { Storage } from '@ionic/storage' ; @ Injectable ( { providedIn : 'root' } ) export class UserData { favorites : string [ ] = [ ] ; HAS_LOGGED_IN = 'hasLoggedIn' ; HAS_SEEN_TUTORIAL = 'hasSeenTutorial' ; constructor ( public storage ) { } hasFavorite ( sessionName ) { return ( this . favorites . indexOf ( sessionName ) > - 1 ) ; } addFavorite ( sessionName ) { this . favorites . push ( sessionName ) ; } removeFavorite ( sessionName ) { const index = this . favorites . indexOf ( sessionName ) ; if ( index > - 1 ) { this . favorites . splice ( index , 1 ) ; } } login ( username ) { return this . storage . set ( this . HAS_LOGGED_IN , true ) . then ( ( ) => { this . setUsername ( username ) ; return window . dispatchEvent ( new CustomEvent ( 'user:login' ) ) ; } ) ; } signup ( username ) { return this . storage . set ( this . HAS_LOGGED_IN , true ) . then ( ( ) => { this . setUsername ( username ) ; return window . dispatchEvent ( new CustomEvent ( 'user:signup' ) ) ; } ) ; } logout ( ) { return this . storage . remove ( this . HAS_LOGGED_IN ) . then ( ( ) => { return this . storage . remove ( 'username' ) ; } ) . then ( ( ) => { window . dispatchEvent ( new CustomEvent ( 'user:logout' ) ) ; } ) ; } setUsername ( username ) { return this . storage . set ( 'username' , username ) ; } getUsername ( ) { return this . storage . get ( 'username' ) . then ( ( value ) => { return value ; } ) ; } isLoggedIn ( ) { return this . storage . get ( this . HAS_LOGGED_IN ) . then ( ( value ) => { return value === true ; } ) ; } checkHasSeenTutorial ( ) { return this . storage . get ( this . HAS_SEEN_TUTORIAL ) . then ( ( value ) => { return value ; } ) ; } }