import { API_ENDPOINT , HIDE_OLD_BUSES_KEY , OLD_BUS_LIMIT } from '../const' ; import { PreferencesManager } from '../managers/preferences' ; import { Injectable } from '@angular/core' ; import { Http } from '@angular/http' ; import { Bus } from '../models/bus' ; class @ Injectable ( ) export class SearchService { public constructor ( private http , private preferences ) { } private param { any [ ] } obj param { boolean } hideOldies return { Bus [ ] } private processBuses ( obj , hideOldies ) { let result = [ ] ; obj . forEach ( data => { let tmp = new Bus ( data . line , data . order , data . speed , data . direction , data . latitude , data . longitude , data . sense , data . timeStamp ) ; if ( hideOldies ) { let timeSinceUpdate = ( ( new Date ( ) ) . getTime ( ) - tmp . Timestamp . getTime ( ) ) / 60000 ; if ( timeSinceUpdate < OLD_BUS_LIMIT ) result . push ( tmp ) ; } else result . push ( tmp ) ; } ) ; return result ; } param { string } query return { Promise < Bus [ ] > } public getBuses ( query ) { return new Promise < Bus [ ] > ( ( resolve , reject ) => { this . preferences . getKey < boolean > ( HIDE_OLD_BUSES_KEY ) . then ( hideOldies => this . http . get ( `${ API_ENDPOINT }/v3/search/${ query }` ) . subscribe ( data => resolve ( this . processBuses ( data . json ( ) , ! ! hideOldies ) ) ) , error => reject ( error ) ) ; } ) ; } }