import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import { Message } from './message' ; import { Contact } from './contact' ; import awesomePhone from 'awesome-phonenumber' ; @ Injectable ( ) export class SmsStoreService { messages : Message [ ] ; contacts : Contact [ ] ; messageMap : Map < string , Message [ ] > ; countryCode : string ; messagesLoaded : boolean ; constructor ( ) { this . messagesLoaded = false ; this . countryCode = 'US' ; } areMessagesLoaded ( ) { return Promise . resolve ( this . messagesLoaded ) ; } changeCountry ( countryCode ) { return new Promise ( ( resolve , reject ) => { if ( this . countryCode != countryCode ) { this . countryCode = countryCode ; if ( this . messagesLoaded ) { this . loadAllMessages ( this . messages ) ; } } resolve ( ) ; } ) ; } getCountry ( ) { return new Promise ( ( resolve , reject ) => { resolve ( this . countryCode ) ; } ) ; } private _messagesLoadedSource = new BehaviorSubject < boolean > ( false ) ; messagesLoaded$ = this . _messagesLoadedSource . asObservable ( ) ; broadcastMessagesLoaded ( messagesLoaded ) { this . _messagesLoadedSource . next ( messagesLoaded ) ; } private _contactClickedSource = new BehaviorSubject < Contact > ( null ) ; contactClicked$ = this . _contactClickedSource . asObservable ( ) ; broadcastContactClicked ( contactClicked ) { this . _contactClickedSource . next ( contactClicked ) ; } loadAllMessages ( messages ) { this . messages = messages ; this . messageMap = new Map ( ) ; this . contacts = new Array < Contact > ( ) ; return new Promise ( ( resolve , reject ) => { this . messages = messages ; for ( let message of messages ) { let mapEntry ; let phone = new awesomePhone ( message . contactAddress , this . countryCode ) ; let contactAddress = phone . getNumber ( 'international' ) ; if ( ! contactAddress ) { contactAddress = message . contactAddress ; } if ( ! ( mapEntry = this . messageMap . get ( contactAddress ) ) ) { mapEntry = new Array < Message > ( ) ; mapEntry . push ( message ) ; this . messageMap . set ( contactAddress , mapEntry ) ; } else { mapEntry . push ( message ) ; } } this . messageMap . forEach ( ( value , key ) => { value = value . sort ( ( message1 , message2 ) => message1 . date . getTime ( ) - message2 . date . getTime ( ) ) ; this . messageMap . set ( key , value ) ; } ) ; this . messageMap . forEach ( ( value , key ) => { let contactName = value [ 0 ] . contactName ; this . contacts . push ( { name : ( contactName != '(Unknown)' ) ? contactName : null , address : key as string , messageCount : value . length } ) ; } ) ; resolve ( ) ; } ) ; } clearAllMessages ( ) { return new Promise ( ( resolve , reject ) => { this . messageMap = new Map ( ) ; this . messages = new Array < Message > ( ) ; this . messagesLoaded = false ; console . log ( 'Cleared in service: ' + JSON . stringify ( this . messages ) ) ; resolve ( ) ; } ) ; } getAllMessages ( ) { return new Promise ( ( resolve , reject ) => { resolve ( this . messageMap ) ; } ) ; } getAllContacts ( ) { return new Promise ( ( resolve , reject ) => { resolve ( this . contacts ) ; } ) ; } getMessages ( contactId ) { let returnMessages ; return new Promise ( ( resolve , reject ) => { returnMessages = this . messageMap . get ( contactId ) ; resolve ( returnMessages ) ; } ) ; } fillContactNames ( contactMap ) { return new Promise ( ( resolve , reject ) => { this . contacts . forEach ( function ( contact ) { let name ; console . log ( contact . address ) ; name = contactMap . get ( contact . address ) ; if ( name ) { contact . name = name ; console . log ( contact . name ) ; } else { console . log ( "not found" ) ; } } ) ; resolve ( ) ; } ) ; } }