import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import { Contact } from './contact' ; var vCard = require ( 'vcf' ) ; import awesomePhone from 'awesome-phonenumber' ; @ Injectable ( ) export class VcfStoreService { contacts : string = null ; contactMap : Map < string , string > ; countryCode : string ; contactsLoaded : boolean ; text : string ; constructor ( ) { this . contactsLoaded = false ; this . countryCode = 'US' ; } areContactsLoaded ( ) { return Promise . resolve ( this . contactsLoaded ) ; } changeCountry ( countryCode ) { return new Promise ( ( resolve , reject ) => { if ( this . countryCode != countryCode ) { this . countryCode = countryCode ; if ( this . contacts ) { this . loadAllContacts ( this . contacts ) ; } } resolve ( ) ; } ) ; } getCountry ( ) { return new Promise ( ( resolve , reject ) => { resolve ( this . countryCode ) ; } ) ; } private _contactsLoadedSource = new BehaviorSubject < boolean > ( false ) ; contactsLoaded$ = this . _contactsLoadedSource . asObservable ( ) ; broadcastMessagesLoaded ( contactsLoaded ) { this . _contactsLoadedSource . next ( contactsLoaded ) ; } addContacttoMap ( tel , contactName ) { if ( ( tel != '' ) && tel . hasOwnProperty ( "_data" ) ) { let phone = new awesomePhone ( tel [ "_data" ] , this . countryCode ) ; let contactNum = phone . getNumber ( 'international' ) ; let name = contactName [ "_data" ] ; if ( name . search ( '=' ) >= 0 ) { name = name . replace ( /={1}/g , '%' ) ; name = decodeURI ( name ) ; } if ( ! contactNum ) { contactNum = tel [ "_data" ] ; } console . log ( contactNum + ":" + name ) ; this . contactMap . set ( contactNum , name ) ; } } loadAllContacts ( contacts ) { this . contacts = contacts ; this . contactMap = new Map ( ) ; return new Promise ( ( resolve , reject ) => { var arcontacts = vCard . parse ( contacts ) ; var content = '' ; for ( let contact of arcontacts ) { var tel ; var tels ; var i ; tels = contact . get ( 'tel' ) ; if ( tels ) { if ( tels . length ) { for ( i = 0 ; i < tels . length ; i ++ ) { this . addContacttoMap ( tels [ i ] , contact . get ( 'fn' ) ) ; } } else { this . addContacttoMap ( tels , contact . get ( 'fn' ) ) ; } } } this . contactsLoaded = true ; resolve ( ) ; } ) ; } clearAllContacts ( ) { return new Promise ( ( resolve , reject ) => { this . contactMap = new Map ( ) ; this . contactsLoaded = false ; console . log ( 'Cleared in service: ' ) ; resolve ( ) ; } ) ; } getAllContacts ( ) { return new Promise ( ( resolve , reject ) => { resolve ( this . contactMap ) ; } ) ; } getContact ( numberId ) { let contact ; return new Promise ( ( resolve , reject ) => { contact = this . contactMap . get ( numberId ) ; resolve ( contact ) ; } ) ; } }