import { alert , File , isAndroid , isIOS , knownFolders , Observable , prompt } from "@nativescript/core" ; import { firebase } from "@nativescript/firebase" ; import { admob as firebaseAdmob } from "@nativescript/firebase/admob" ; import { RewardedVideoAdReward } from "@nativescript/firebase/admob/admob" ; import { analytics as firebaseAnalytics } from "@nativescript/firebase/analytics" ; import { crashlytics as firebaseCrashlytics } from "@nativescript/firebase/crashlytics" ; import { firebaseFunctions } from "@nativescript/firebase/functions" ; import { performance as firebasePerformance } from "@nativescript/firebase/performance" ; import { FirebaseTrace } from "@nativescript/firebase/performance/performance" ; import { storage as firebaseStorage } from "@nativescript/firebase/storage" ; import { UploadMetadata } from "@nativescript/firebase/storage/storage" ; import { MessagingViewModel } from "./messaging-view-model" ; const firebaseWebApi = require ( "@nativescript/firebase/app" ) ; export class HelloWorldModel extends Observable { public messaging = new MessagingViewModel ( ) ; public userEmailOrPhone : string ; private userListenerWrapper : firebase . AddEventListenerResult ; private companiesListenerWrapper : firebase . AddEventListenerResult ; private onAuthStateChangedHandlerSet = false ; private firebaseTrace : FirebaseTrace ; /***********************************************
     * Web API usage examples
     ***********************************************/ private ensureWebOnAuthChangedHandler ( ) { if ( ! this . onAuthStateChangedHandlerSet ) { this . onAuthStateChangedHandlerSet = true ; firebaseWebApi . auth ( ) . onAuthStateChanged ( ( user ? ) => { console . log ( ">> auth state changed: " + user ) ; if ( user ) { this . set ( "userEmailOrPhone" , user . email ? user . email : ( user . phoneNumber ? user . phoneNumber : "N/A" ) ) ; alert ( { title : "User signed in" , message : JSON . stringify ( user ) , okButtonText : "Nice!" } ) ; } else { alert ( { title : "User signed out" , okButtonText : "Bye!" } ) ; } } ) ; } } public doWebInit ( ) { firebaseWebApi . initializeApp ( ) ; } public doWebLoginAnonymously ( ) { this . ensureWebOnAuthChangedHandler ( ) ; firebaseWebApi . auth ( ) . signInAnonymously ( ) . then ( ( ) => console . log ( "User logged in" ) ) . catch ( err => { alert ( { title : "Login error" , message : JSON . stringify ( err ) , okButtonText : "OK, pity" } ) ; } ) ; } public doWebLoginByPassword ( ) { this . ensureWebOnAuthChangedHandler ( ) ; firebaseWebApi . auth ( ) . signInWithEmailAndPassword ( 'eddyverbruggen+firebase@gmail.com' , 'pwd123LOL' ) . then ( ( ) => { console . log ( "User logged in" ) ; firebaseWebApi . auth ( ) . currentUser . getIdToken ( false ) . then ( ( token ) => console . log ( "Auth token retrieved: " + token ) ) . catch ( errorMessage => console . log ( "Auth token retrieval error: " + errorMessage ) ) ; } ) . catch ( err => { alert ( { title : "Login error" , message : JSON . stringify ( err ) , okButtonText : "OK, pity" } ) ; } ) ; } public doWebFetchSignInMethodsForEmail ( ) { const user = firebaseWebApi . auth ( ) . currentUser ; if ( ! user || ! user . email ) { alert ( { title : "Can't fetch providers" , message : "No user with an emailaddress logged in." , okButtonText : "OK, makes sense.." } ) ; return ; } firebaseWebApi . auth ( ) . fetchSignInMethodsForEmail ( user . email ) . then ( result => { alert ( { title : `Sign-in methods for ${ user . email }` , message : JSON . stringify ( result ) , okButtonText : "Thanks!" } ) ; } , errorMessage => { alert ( { title : "Sign-in methods for Email error" , message : errorMessage , okButtonText : "OK, pity.." } ) ; } ) ; } public doWebLogout ( ) { firebaseWebApi . auth ( ) . signOut ( ) . then ( ( ) => { this . set ( "userEmailOrPhone" , null ) ; alert ( { title : "Logout OK" , okButtonText : "OK, bye!" } ) ; } ) . catch ( error => { alert ( { title : "Logout error" , message : JSON . stringify ( error ) , okButtonText : "Hmmkay" } ) ; } ) ; } public doWebCreateUser ( ) { firebaseWebApi . auth ( ) . createUserWithEmailAndPassword ( 'eddyverbruggen+firebasewebapi@gmail.com' , 'firebase' ) . then ( ( user ) => { console . log ( "User created: " + JSON . stringify ( user ) ) ; this . set ( "userEmailOrPhone" , user . email ) ; alert ( { title : "User created" , message : JSON . stringify ( user ) , okButtonText : "Nice!" } ) ; } ) . catch ( error => { console . log ( "Error creating user: " + error ) ; alert ( { title : "No user created" , message : JSON . stringify ( error ) , okButtonText : "OK, got it" } ) ; } ) ; } public doWebGetCurrentUser ( ) { const user = firebaseWebApi . auth ( ) . currentUser ; if ( user ) { console . log ( "Current user: " + JSON . stringify ( user ) ) ; this . set ( "userEmailOrPhone" , user . email ) ; alert ( { title : "Current user" , message : JSON . stringify ( user ) , okButtonText : "Nice!" } ) ; } else { console . log ( "No current user" ) ; alert ( { title : "No current user" , okButtonText : "OK, thanks" } ) ; } } public doWebCallableFunction ( ) { const fn = firebaseFunctions . httpsCallable ( "helloName" ) ; fn ( "Nativescript-Plugin-Firebase!" ) . then ( ( dataCue ) => { alert ( { title : "Callable Function Result" , message : dataCue . message , okButtonText : "Nice!" } ) ; } ) . catch ( ( errorMessage ) => { alert ( { title : "An Error Occurred" , message : errorMessage , okButtonText : "OK, thanks" } ) ; } ) ; } public doWebAddValueEventListenerForCompanies ( ) { const path = "/companies" ; const onValueEvent = result => { if ( result . error ) { alert ( { title : "Listener error" , message : result . error , okButtonText : "Darn!" } ) ; } else { console . log ( "key exists? " + result . exists ( ) ) ; this . set ( "path" , path ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . val ( ) ) ) ; } } ; firebaseWebApi . database ( ) . ref ( path ) . on ( "value" , onValueEvent ) ; } public doWebRemoveValueEventListenersForCompanies ( ) { const path = "/companies" ; firebaseWebApi . database ( ) . ref ( path ) . off ( "value" ) ; } public doWebGetServerInfo ( ) { const path = ".info" ; firebaseWebApi . database ( ) . ref ( path ) . once ( "value" ) . then ( result => { console . log ( "key exists? " + result . exists ( ) ) ; this . set ( "path" , path ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . val ( ) ) ) ; } ) . catch ( error => console . log ( "doWebGetServerTime error: " + error ) ) ; } public doWebGetValueForCompanies ( ) { const path = "/companies" ; firebaseWebApi . database ( ) . ref ( path ) . once ( "value" ) . then ( result => { console . log ( "key exists? " + result . exists ( ) ) ; this . set ( "path" , path ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . val ( ) ) ) ; } ) . catch ( error => console . log ( "doWebGetValueForCompanies error: " + error ) ) ; } public doWebStoreCompaniesBySetValue ( ) { firebaseWebApi . database ( ) . ref ( "/companies" ) . set ( [ { name : 'Telerik (web)' , country : 'Bulgaria' , since : 2000 , updateTs : firebase . ServerValue . TIMESTAMP } , { name : 'Google (web)' , country : 'USA' , since : 1900 , updateTs : firebase . ServerValue . TIMESTAMP } ] ) . then ( ( ) => console . log ( "firebase.setValue done" ) ) . catch ( error => console . log ( "firebase.setValue error: " + error ) ) ; } public doWebRemoveCompanies ( ) { firebaseWebApi . database ( ) . ref ( "/companies" ) . remove ( ) . then ( ( ) => console . log ( "firebase.remove done" ) ) . catch ( ( err ) => console . log ( "firebase.remove error: " + err ) ) ; } public doWebQueryBulgarianCompanies ( ) { const path = "/companies" ; const child = "name" ; firebaseWebApi . database ( ) . ref ( path ) . orderByChild ( child ) . once ( "value" ) . then ( result => { console . log ( "key exists? " + result . exists ( ) ) ; this . set ( "path" , path ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . val ( ) ) ) ; } ) . catch ( error => console . log ( "doWebQueryBulgarianCompanies error: " + error ) ) ; } public doWebStoreCompanyByFirstCreatingKey ( ) { const path = "companies" , companyRef = firebaseWebApi . database ( ) . ref ( path ) , newCompanyKey = companyRef . push ( ) . key , storeAtPath = `/${ path }/${ newCompanyKey }` , value = { name : `Company with key ${ newCompanyKey }` , updateTs : firebase . ServerValue . TIMESTAMP } ; firebaseWebApi . database ( ) . ref ( storeAtPath ) . set ( value ) . then ( ( ) => { this . set ( "path" , storeAtPath ) ; this . set ( "key" , newCompanyKey ) ; this . set ( "value" , JSON . stringify ( value ) ) ; } ) ; } public doWebUploadFile ( ) { const appPath = knownFolders . currentApp ( ) . path ; const logoPath = appPath + "/images/telerik-logo.png" ; const storageRef = firebaseWebApi . storage ( ) . ref ( ) ; const childRef = storageRef . child ( "uploads/images/telerik-logo-uploaded.png" ) ; const metadata = { contentType : "demo/test" , contentLanguage : "fr" , customMetadata : { "foo" : "bar" , "foo2" : "bar2" } } ; childRef . put ( File . fromPath ( logoPath ) , metadata ) . then ( uploadedFile => { console . log ( "Uploaded! " + JSON . stringify ( uploadedFile ) ) ; this . set ( "storageFeedback" , "Uploaded!" ) ; } , error => { console . log ( "firebase.doWebUploadFile error: " + error ) ; this . set ( "storageFeedback" , "Error: " + error ) ; } ) ; } public doWebDownloadFile ( ) { const storageRef = firebaseWebApi . storage ( ) . ref ( ) ; const childRef = storageRef . child ( "uploads/images/telerik-logo-uploaded.png" ) ; const documents = knownFolders . documents ( ) ; const logoPath = documents . path + "/telerik-logo-downloaded.png" ; childRef . download ( logoPath ) . then ( ( ) => { console . log ( "The file has been downloaded" ) ; this . set ( "storageFeedback" , "The file has been downloaded" ) ; } ) . catch ( error => { console . log ( "Download error: " + error ) ; this . set ( "storageFeedback" , "Error: " + error ) ; } ) ; } public doWebGetDownloadUrl ( ) { const storageRef = firebaseWebApi . storage ( ) . ref ( ) ; const childRef = storageRef . child ( "uploads/images/telerik-logo-uploaded.png" ) ; childRef . getDownloadURL ( ) . then ( theUrl => { console . log ( "Download url: " + theUrl ) ; this . set ( "storageFeedback" , "Download URL logged to the console" ) ; } ) . catch ( error => { console . log ( "Download error: " + error ) ; this . set ( "storageFeedback" , "Error: " + error ) ; } ) ; } public doWebDeleteFile ( ) { firebaseWebApi . storage ( ) . ref ( ) . child ( "uploads/images/telerik-logo-uploaded.png" ) . delete ( ) . then ( ( ) => { console . log ( "Deleted file" ) ; this . set ( "storageFeedback" , "File deleted" ) ; } ) . catch ( error => { console . log ( "Error deleting file: " + error ) ; this . set ( "storageFeedback" , "Error deleting file: " + error ) ; } ) ; } public doWebListAll ( ) { firebaseWebApi . storage ( ) . ref ( ) . child ( "uploads/images" ) . listAll ( ) . then ( result => { console . log ( JSON . stringify ( result ) ) ; result . items . forEach ( item => { item . listAll ( ) . then ( result2 => console . log ( `Inner result for ITEM ${ item . name }: ${ JSON . stringify ( result2 ) }` ) ) . catch ( err => console . log ( err ) ) ; } ) ; result . prefixes . forEach ( prefix => { prefix . listAll ( ) . then ( result2 => console . log ( `Inner result for PREFIX ${ prefix . name }: ${ JSON . stringify ( result2 ) }` ) ) . catch ( err => console . log ( err ) ) ; } ) ; } ) . catch ( err => console . log ( err ) ) ; } /***********************************************
     * Native API usage examples
     ***********************************************/ public doInit ( ) { firebase . init ( { crashlyticsCollectionEnabled : true , persist : true , analyticsCollectionEnabled : false , onAuthStateChanged : data => { console . log ( ( data . loggedIn ? "Logged in to firebase" : "Logged out from firebase" ) + " (init's onAuthStateChanged callback)" ) ; console . log ( JSON . stringify ( data ) ) ; if ( data . loggedIn ) { this . set ( "userEmailOrPhone" , data . user . email ? data . user . email : ( data . user . phoneNumber ? data . user . phoneNumber : "N/A" ) ) ; } } , onDynamicLinkCallback : result => { console . log ( "dynamic link callback invoked with: " + result ) ; setTimeout ( ( ) => { alert ( { title : "Dynamic Link!" , message : JSON . stringify ( result ) , okButtonText : "Awesome!" } ) ; } , 500 ) ; } } ) . then ( ( ) => { console . log ( "Firebase is ready" ) ; } , error => { console . log ( "firebase.init error: " + error ) ; } ) ; } public doEnableAnalytics ( ) { firebaseAnalytics . setAnalyticsCollectionEnabled ( true ) ; alert ( { title : "Analytics collection" , message : "ENABLED" , okButtonText : "OK" } ) ; } public doDisableAnalytics ( ) { firebaseAnalytics . setAnalyticsCollectionEnabled ( false ) ; alert ( { title : "Analytics collection" , message : "DISABLED" , okButtonText : "OK" } ) ; } public doLogAnalyticsEvents ( ) { firebaseAnalytics . logEvent ( { key : "add_to_cart" , parameters : [ { key : "item_id" , value : "p7655" } , { key : "item_name" , value : "abcd" } ] } ) . then ( ( ) => { console . log ( "Analytics event logged" ) ; alert ( { title : "Analytics event pushed" , okButtonText : "Awesome :)" } ) ; } , errorMessage => { alert ( { title : "Analytics error" , message : errorMessage , okButtonText : "Ehh, OK" } ) ; } ) ; /**
         * Same thing as logEvent but can add an array or specific types not just string (LogComplexEventTypeParameter.BOOLEAN, LogComplexEventTypeParameter.STRING,
         * LogComplexEventTypeParameter.DOUBLE, LogComplexEventTypeParameter.FLOAT, LogComplexEventTypeParameter.INT, LogComplexEventTypeParameter.ARRAY)
         */ firebaseAnalytics . logComplexEvent ( { key : "view_item_list" , parameters : [ { key : "item1" , type : "array" , value : [ { parameters : [ { key : "item_id" , value : "id of item" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_name" , value : "name of item" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_category" , value : "category" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_variant" , value : "variant" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_brand" , value : "name of item brand" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "price" , value : 1 , type : firebase . LogComplexEventTypeParameter . DOUBLE } , { key : "item_list" , value : "name of list" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "index" , value : 1 , type : firebase . LogComplexEventTypeParameter . INT } ] } , { parameters : [ { key : "item_id" , value : "id of item" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_name" , value : "name of item" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_category" , value : "category" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_variant" , value : "variant" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "item_brand" , value : "name of item brand" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "price" , value : 1 , type : firebase . LogComplexEventTypeParameter . DOUBLE } , { key : "item_list" , value : "name of list" , type : firebase . LogComplexEventTypeParameter . STRING } , { key : "index" , value : 2 , type : firebase . LogComplexEventTypeParameter . INT } ] } ] } ] } ) ; } public doSetAnalyticsUserProperty ( ) { firebaseAnalytics . setUserProperty ( { key : "origin" , value : "demoapp" } ) . then ( ( ) => { alert ( { title : "Analytics user property set" , okButtonText : "Great :P" } ) ; } , errorMessage => { alert ( { title : "Analytics error" , message : errorMessage , okButtonText : "Ehhmmm, OK" } ) ; } ) ; } public doSetScreenNameA ( ) { this . setScreenName ( "Screen A" ) ; } public doSetScreenNameB ( ) { this . setScreenName ( "Screen B" ) ; } private setScreenName ( screenName ) { firebaseAnalytics . setScreenName ( { screenName } ) . then ( ( ) => { alert ( { title : "Analytics screen name set to: " + name , okButtonText : "Great!" } ) ; } , errorMessage => { alert ( { title : "Analytics error" , message : errorMessage , okButtonText : "Ehhmmm, OK" } ) ; } ) ; } public doShowAdMobBanner ( ) { firebaseAdmob . showBanner ( { size : firebaseAdmob . AD_SIZE . SMART_BANNER , margins : { bottom : isIOS ? 50 : 0 } , androidBannerId : "ca-app-pub-9517346003011652/7749101329" , iosBannerId : "ca-app-pub-9517346003011652/3985369721" , testing : true , iosTestDeviceIds : [ "fee4cf319a242eab4701543e4c16db89c722731f" , "a4cbb499e279054b55c206528f8510ff7fbf20c8" , ] , keywords : [ "keyword1" , "keyword2" ] , onClicked : ( ) => console . log ( "Ad clicked" ) , onLeftApplication : ( ) => console . log ( "Ad left application (opened a browser, likely)" ) , onOpened : ( ) => console . log ( "Ad opened" ) , onClosed : ( ) => console . log ( "Ad closed" ) } ) . then ( ( ) => { alert ( { title : "Ad loaded successfully" , message : "Should now be showing.." , okButtonText : "Hmmkay" } ) ; } , errorMessage => { alert ( { title : "AdMob error" , message : errorMessage , okButtonText : "Hmmkay" } ) ; } ) ; } public doShowAdMobInterstitial ( ) { firebaseAdmob . showInterstitial ( { iosInterstitialId : "ca-app-pub-9517346003011652/6938836122" , androidInterstitialId : "ca-app-pub-9517346003011652/9225834529" , testing : true , iosTestDeviceIds : [ "45d77bf513dfabc2949ba053da83c0c7b7e87715" , "fee4cf319a242eab4701543e4c16db89c722731f" ] } ) . then ( ( ) => { console . log ( "AdMob interstitial showing" ) ; } , errorMessage => { alert ( { title : "AdMob error" , message : errorMessage , okButtonText : "Hmmkay" } ) ; } ) ; } public doPreloadAdMobInterstitial ( ) { firebaseAdmob . preloadInterstitial ( { iosInterstitialId : "ca-app-pub-9517346003011652/6938836122" , androidInterstitialId : "ca-app-pub-9517346003011652/9225834529" , testing : true , iosTestDeviceIds : [ "45d77bf513dfabc2949ba053da83c0c7b7e87715" , "fee4cf319a242eab4701543e4c16db89c722731f" ] , onClosed : ( ) => console . log ( "Interstitial closed" ) , onClicked : ( ) => console . log ( "Interstitial clicked" ) , onLeftApplication : ( ) => console . log ( "Interstitial left application (opened a browser, likely)" ) , onOpened : ( ) => console . log ( "Interstitial opened" ) } ) . then ( ( ) => console . log ( "AdMob interstitial preloaded" ) , errorMessage => { alert ( { title : "AdMob error" , message : errorMessage , okButtonText : "Hmmkay" } ) ; } ) ; } public doShowPreloadedAdMobInterstitial ( ) { firebaseAdmob . showInterstitial ( ) . then ( ( ) => console . log ( "AdMob interstitial showing" ) , errorMessage => { alert ( { title : "AdMob error" , message : errorMessage , okButtonText : "Hmmkay" } ) ; } ) ; } public doPreloadRewardedVideoAd ( ) { firebaseAdmob . preloadRewardedVideoAd ( { iosAdPlacementId : "ca-app-pub-9517346003011652/8586553377" , androidAdPlacementId : "ca-app-pub-9517346003011652/2819097664" , testing : true , iosTestDeviceIds : [ "45d77bf513dfabc2949ba053da83c0c7b7e87715" , "fee4cf319a242eab4701543e4c16db89c722731f" ] , keywords : [ "foo" , "bar" ] } ) . then ( ( ) => console . log ( "AdMob rewarded video ad preloaded" ) , errorMessage => { alert ( { title : "AdMob error" , message : errorMessage , okButtonText : "Hmmkay" } ) ; } ) ; } public doShowPreloadedRewardedVideoAd ( ) { let reward ; firebaseAdmob . showRewardedVideoAd ( { onRewarded : receivedReward => { reward = receivedReward ; console . log ( "Rewarded video ad: rewarded. Details: " + JSON . stringify ( reward ) ) ; } , onLoaded : ( ) => console . log ( "Rewarded video ad: loaded" ) , onFailedToLoad : ( ) => console . log ( "Rewarded video ad: failed to load" ) , onOpened : ( ) => console . log ( "Rewarded video ad: opened" ) , onStarted : ( ) => console . log ( "Rewarded video ad: started" ) , onCompleted : ( ) => console . log ( "Rewarded video ad: completed" ) , onClosed : ( ) => { console . log ( "Rewarded video ad: closed" ) ; if ( reward ) { setTimeout ( ( ) => { alert ( { title : "You were rewarded!" , message : `${ reward . amount } ${ reward . type }` , okButtonText : "Thanks!" } ) ; } , 500 ) ; } } , onLeftApplication : ( ) => console . log ( "Rewarded video ad: left application" ) } ) . then ( ( ) => console . log ( "AdMob rewarded video ad showing" ) , errorMessage => { alert ( { title : "AdMob error" , message : errorMessage , okButtonText : "Hmmkay" } ) ; } ) ; } /**
     * Note that an interstitial is supposed to be hidden by clicking the close button,
     * so there's no function to do it programmatically.
     */ public doHideAdMobBanner ( ) { firebaseAdmob . hideBanner ( ) . then ( ( ) => { console . log ( "AdMob banner hidden" ) ; } , errorMessage => { alert ( { title : "AdMob error" , message : errorMessage , okButtonText : "Hmmkay" } ) ; } ) ; } public doGetRemoteConfig ( ) { console . log ( ">>> doGetRemoteConfig" ) ; firebase . getRemoteConfig ( { developerMode : true , cacheExpirationSeconds : 600 , properties : [ { "key" : "holiday_promo_enabled" , "default" : false } , { "key" : "default_only_prop" , "default" : 77 } , { "key" : "coupons_left" , "default" : 100 } , { "key" : "origin" , "default" : "client" } , { "key" : "double_test" , "default" : 9.99 } , { "key" : "int_test" , "default" : 11 } ] } ) . then ( ( result ) => { console . log ( "remote config fetched: " + JSON . stringify ( result . properties ) ) ; alert ( { title : `Fetched at ${ result . lastFetch } ${ result . throttled ? '(throttled)' : '' }` , message : JSON . stringify ( result . properties ) , okButtonText : "Nice!" } ) ; } , errorMessage => { alert ( { title : "Remote Config error" , message : errorMessage , okButtonText : "OK, thanks" } ) ; } ) ; } public doGetCurrentUser ( ) { firebase . getCurrentUser ( ) . then ( user => { this . set ( "userEmailOrPhone" , user . email ) ; alert ( { title : "Current user" , message : JSON . stringify ( user ) , okButtonText : "Nice!" } ) ; } , errorMessage => { alert ( { title : "No current user" , message : errorMessage , okButtonText : "OK, thanks" } ) ; } ) ; } public doUpdateProfile ( ) { firebase . updateProfile ( { displayName : `Name UpdateTS ${ new Date ( ) . getTime ( ) }` , photoURL : 'https://avatars2.githubusercontent.com/u/1426370?v=3&u=9661f01efde3c412e19650c9b632297970cbe6ed&s=400' } ) . then ( ( ) => { alert ( { title : "Profile updated" , okButtonText : "Nice!" } ) ; } , errorMessage => { alert ( { title : "Profile update error" , message : errorMessage , okButtonText : "OK.." } ) ; } ) ; } public doLoginAnonymously ( ) { firebase . login ( { type : firebase . LoginType . ANONYMOUS } ) . then ( result => { console . log ( "Anonymous login OK: " + JSON . stringify ( result ) ) ; alert ( { title : "Login OK" , message : JSON . stringify ( result ) , okButtonText : "Nice!" } ) ; } , errorMessage => { alert ( { title : "Login error" , message : errorMessage , okButtonText : "OK, pity" } ) ; } ) ; } public doFetchSignInMethodsForEmail ( ) { firebase . getCurrentUser ( ) . then ( user => { if ( ! user || ! user . email ) { alert ( { title : "Can't fetch providers" , message : "No user with emailaddress logged in." , okButtonText : "OK, makes sense.." } ) ; return ; } firebase . fetchSignInMethodsForEmail ( user . email ) . then ( result => { alert ( { title : `Sign-in methods for ${ user . email }` , message : JSON . stringify ( result ) , okButtonText : "Thanks!" } ) ; } , errorMessage => { alert ( { title : "Fetch Sign-in methods for Email error" , message : errorMessage , okButtonText : "OK, pity.." } ) ; } ) ; } ) ; } public doCreateUser ( ) { firebase . createUser ( { email : 'eddyverbruggen+firebasetest@gmail.com' , password : 'firebase' } ) . then ( ( user ) => { console . log ( "User created: " + JSON . stringify ( user ) ) ; this . set ( "userEmailOrPhone" , user . email ) ; alert ( { title : "User created" , message : JSON . stringify ( user ) , okButtonText : "Nice!" } ) ; } , error => { console . log ( "Error creating user: " + error ) ; alert ( { title : "No user created" , message : error , okButtonText : "OK, got it" } ) ; } ) ; } public doDeleteUser ( ) { firebase . deleteUser ( ) . then ( ( ) => { alert ( { title : "User deleted" , okButtonText : "Nice!" } ) ; } , errorMessage => { alert ( { title : "User not deleted" , message : errorMessage , okButtonText : "OK, got it" } ) ; } ) ; } public doLoginByPassword ( ) { firebase . login ( { type : firebase . LoginType . PASSWORD , passwordOptions : { email : 'eddyverbruggen+firebase@gmail.com' , password : 'pwd123LOL' } } ) . then ( result => { console . log ( "Password login OK: " + JSON . stringify ( result ) ) ; alert ( { title : "Login OK" , message : JSON . stringify ( result ) , okButtonText : "Nice!" } ) ; firebase . getAuthToken ( { forceRefresh : false } ) . then ( ( result ) => console . log ( "Auth token retrieved: " + JSON . stringify ( result ) ) ) . catch ( errorMessage => console . log ( "Auth token retrieval error: " + errorMessage ) ) ; } , errorMessage => { console . log ( "Login error: " + errorMessage ) ; this . set ( "userEmailOrPhone" , errorMessage ) ; alert ( { title : "Login error" , message : errorMessage , okButtonText : "OK, pity" } ) ; } ) ; } public doLoginByPhone ( ) { prompt ( "Your phone number" , "+31612345678" ) . then ( promptResult => { if ( ! promptResult . result ) { return ; } console . log ( ">> using promptResult.text: " + promptResult . text ) ; firebase . login ( { type : firebase . LoginType . PHONE , phoneOptions : { phoneNumber : promptResult . text , verificationPrompt : "The received verification code" } } ) . then ( result => { console . log ( "Phone login OK: " + JSON . stringify ( result ) ) ; alert ( { title : "Phone login OK" , message : JSON . stringify ( result ) , okButtonText : "Cool" } ) ; } , errorMessage => { alert ( { title : "Phone login error" , message : errorMessage , okButtonText : "OK, damn shame" } ) ; } ) ; } ) ; } public doLoginByEmailLink ( ) { prompt ( { title : "The email address to send the link to" , defaultText : "" , inputType : "email" , capitalizationType : "none" , okButtonText : "OK" , cancelButtonText : "Cancel" } ) . then ( promptResult => { if ( ! promptResult . result ) { return ; } console . log ( ">> using promptResult.text: " + promptResult . text ) ; firebase . login ( { type : firebase . LoginType . EMAIL_LINK , emailLinkOptions : { email : promptResult . text . trim ( ) , url : "https://combidesk.com?foo=bar" } } ) . then ( result => { console . log ( "Email link sent" ) ; alert ( { title : "Email link sent" , message : "Check your email :)" , okButtonText : "Cool" } ) ; } , errorMessage => { alert ( { title : "Email link login error" , message : errorMessage , okButtonText : "OK, damn shame" } ) ; } ) ; } ) ; } public doLoginByFacebook ( ) { firebase . login ( { type : firebase . LoginType . FACEBOOK } ) . then ( result => { console . log ( "Facebook login OK: " + JSON . stringify ( result ) ) ; alert ( { title : "Login OK" , message : JSON . stringify ( result ) , okButtonText : "Nice!" } ) ; } , errorMessage => { alert ( { title : "Login error" , message : errorMessage , okButtonText : "OK, pity" } ) ; } ) ; } public doSignInWithApple ( ) { firebase . login ( { type : firebase . LoginType . APPLE , appleOptions : { locale : "nl" , scopes : [ "email" ] } } ) . then ( result => console . log ( "Apple login OK: " + JSON . stringify ( result ) ) , errorMessage => { alert ( { title : "Login error" , message : errorMessage , okButtonText : "OK, pity" } ) ; } ) ; } public doLoginByGoogle ( ) { firebase . login ( { type : firebase . LoginType . GOOGLE , googleOptions : { scopes : [ "https://www.googleapis.com/auth/contacts.readonly" , "https://www.googleapis.com/auth/user.birthday.read" ] } } ) . then ( result => { console . log ( "Google login OK: " + JSON . stringify ( result . additionalUserInfo ) ) ; console . log ( "Google login OK, photoURL: " + result . photoURL ) ; alert ( { title : "Login OK" , message : JSON . stringify ( result ) , okButtonText : "Nice!" } ) ; } , errorMessage => { alert ( { title : "Login error" , message : errorMessage , okButtonText : "OK, pity" } ) ; } ) ; } public doReloadUser ( ) { firebase . reloadUser ( ) . then ( ( ) => { alert ( { title : "User reloaded" , message : "You can use 'get current user' to inspect the reloaded data" , okButtonText : "OK" } ) ; } , errorMessage => { alert ( { title : "Reload error" , message : errorMessage , okButtonText : "OK, shame" } ) ; } ) ; } public doResetPassword ( ) { firebase . sendPasswordResetEmail ( "eddyverbruggen+firebase@gmail.com" ) . then ( ( ) => { console . log ( "Password reset. Check your email." ) ; this . set ( "userEmailOrPhone" , "Password reset mail sent to eddyverbruggen@gmail.com." ) ; alert ( { title : "Password reset. Check your email." , okButtonText : "OK, nice!" } ) ; } , error => { console . log ( "Password reset error: " + error ) ; alert ( { title : "Password reset error" , message : error , okButtonText : "Hmmkay :(" } ) ; } ) ; } public doWebResetPassword ( ) { firebaseWebApi . auth ( ) . sendPasswordResetEmail ( "eddyverbruggen+firebase@gmail.com" ) . then ( ( ) => { console . log ( "Password reset. Check your email." ) ; this . set ( "userEmailOrPhone" , "Password reset mail sent to eddyverbruggen@gmail.com." ) ; alert ( { title : "Password reset. Check your email." , okButtonText : "OK, nice!" } ) ; } , error => { console . log ( "Password reset error: " + error ) ; alert ( { title : "Password reset error" , message : error , okButtonText : "Hmmkay :(" } ) ; } ) ; } public doUpdateEmail ( ) { firebase . updateEmail ( "eddyverbruggen+firebase@gmail.com" ) . then ( ( ) => { console . log ( "Email updated." ) ; this . set ( "userEmailOrPhone" , "Email updated to eddyverbruggen+firebase@gmail.com" ) ; alert ( { title : "Email updated." , okButtonText : "OK, nice!" } ) ; } , error => { console . log ( "Email update error: " + error ) ; alert ( { title : "Email update error" , message : error , okButtonText : "Hmmkay :(" } ) ; } ) ; } public doWebUpdateEmail ( ) { firebaseWebApi . auth ( ) . updateEmail ( "eddyverbruggen+firebase@gmail.com" ) . then ( ( ) => { console . log ( "Email updated." ) ; this . set ( "userEmailOrPhone" , "Email updated to eddyverbruggen+firebase@gmail.com" ) ; alert ( { title : "Email updated." , okButtonText : "OK, nice!" } ) ; } , error => { console . log ( "Email update error: " + error ) ; alert ( { title : "Email update error" , message : error , okButtonText : "Hmmkay :(" } ) ; } ) ; } public doUpdatePassword ( ) { firebase . updatePassword ( "pwd123LOL" ) . then ( ( ) => { console . log ( "Password updated." ) ; this . set ( "userEmailOrPhone" , "Password updated to pwd123LOL" ) ; alert ( { title : "Password updated." , okButtonText : "OK, nice!" } ) ; } , error => { console . log ( "Password update error: " + error ) ; alert ( { title : "Password update error" , message : error , okButtonText : "Hmmkay :(" } ) ; } ) ; } public doWebUpdatePassword ( ) { firebaseWebApi . auth ( ) . updatePassword ( "pwd123LOL" ) . then ( ( ) => { console . log ( "Password updated." ) ; this . set ( "userEmailOrPhone" , "Password updated to pwd123LOL" ) ; alert ( { title : "Password updated." , okButtonText : "OK, nice!" } ) ; } , error => { console . log ( "Password update error: " + error ) ; alert ( { title : "Password update error" , message : error , okButtonText : "Hmmkay :(" } ) ; } ) ; } public doSendEmailVerification ( ) { firebase . sendEmailVerification ( ) . then ( ( ) => { alert ( { title : "Email sent!" , okButtonText : "Okay" } ) ; } , error => { alert ( { title : "Error sending email verification" , message : error , okButtonText : "Hmmmkay" } ) ; } ) ; } public doLogout ( ) { firebase . logout ( ) . then ( ( ) => { this . set ( "userEmailOrPhone" , null ) ; console . log ( "Logout OK" ) ; alert ( { title : "Logout OK" , okButtonText : "OK, bye!" } ) ; } , error => { console . log ( "Logout error: " + error ) ; alert ( { title : "Logout error" , message : error , okButtonText : "Hmmkay" } ) ; } ) ; } public doKeepUsersInSyncOn ( ) { firebase . keepInSync ( "/users" , true ) . then ( ( ) => { console . log ( "firebase.keepInSync ON" ) ; } , error => { console . log ( "firebase.keepInSync error: " + error ) ; } ) ; } public doKeepUsersInSyncOff ( ) { firebase . keepInSync ( "/users" , false ) . then ( ( ) => { console . log ( "firebase.keepInSync OFF" ) ; } , error => { console . log ( "firebase.keepInSync error: " + error ) ; } ) ; } public doAddChildEventListenerForUsers ( ) { const onChildEvent = result => { this . set ( "path" , '/users' ) ; this . set ( "type" , result . type ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . value ) ) ; } ; firebase . addChildEventListener ( onChildEvent , "/users" ) . then ( result => { this . userListenerWrapper = result ; console . log ( "firebase.addChildEventListener added" ) ; } , error => { console . log ( "firebase.addChildEventListener error: " + error ) ; } ) ; } public doRemoveChildEventListenerForUsers ( ) { if ( ! this . userListenerWrapper ) { return ; } firebase . removeEventListeners ( this . userListenerWrapper . listeners , this . userListenerWrapper . path ) . then ( ( ) => { console . log ( "firebase.doRemoveChildEventListenerForUsers success" ) ; alert ( { title : "Listener removed" , okButtonText : "OK" } ) ; } , error => { console . log ( "firebase.removeEventListeners error: " + error ) ; } ) ; } public doAddValueEventListenerForCompanies ( ) { const path = "/companies" ; const onValueEvent = result => { if ( result . error ) { alert ( { title : "Listener error" , message : result . error , okButtonText : "Darn!" } ) ; } else { this . set ( "path" , path ) ; this . set ( "type" , result . type ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . value ) ) ; } } ; firebase . addValueEventListener ( onValueEvent , path ) . then ( result => { this . companiesListenerWrapper = result ; console . log ( "firebase.addValueEventListener added" ) ; } , error => { console . log ( "firebase.addValueEventListener error: " + error ) ; } ) ; } public doRemoveValueEventListenersForCompanies ( ) { if ( ! this . companiesListenerWrapper ) { return ; } firebase . removeEventListeners ( this . companiesListenerWrapper . listeners , this . companiesListenerWrapper . path ) . then ( ( ) => { console . log ( "firebase.doRemoveValueEventListenersForCompanies success" ) ; alert ( { title : "Listener removed" , okButtonText : "OK" } ) ; } , error => { console . log ( "firebase.removeEventListeners error." ) ; } ) ; } public doGetValueForCompanies ( ) { firebase . getValue ( '/companies' ) . then ( result => { alert ( { title : "Value retrieved" , message : JSON . stringify ( result ) , okButtonText : "OK" } ) ; } , error => { console . log ( "doGetValueForCompanies error: " + error ) ; } ) ; } public doUserStoreByPush ( ) { firebase . push ( '/users' , { 'first' : 'Eddy' , 'last' : 'Verbruggen' , 'birthYear' : 1977 , 'isMale' : true , 'address' : { 'street' : 'foostreet' , 'number' : 123 } } ) . then ( result => { console . log ( "firebase.push done, created key: " + result . key ) ; } , error => { console . log ( "firebase.push error: " + error ) ; } ) ; } public doStoreCompaniesBySetValue ( ) { firebase . setValue ( '/companies' , [ { name : 'Telerik' , country : 'Bulgaria' , since : 2000 , updateTs : firebase . ServerValue . TIMESTAMP } , { name : 'Google' , country : 'USA' , since : 1900 , updateTs : firebase . ServerValue . TIMESTAMP } ] ) . then ( ( ) => console . log ( "firebase.setValue done" ) , error => console . log ( "firebase.setValue error: " + error ) ) ; } public doRemoveUsers ( ) { firebase . remove ( "/users" ) . then ( ( ) => console . log ( "firebase.remove done" ) , error => console . log ( "firebase.remove error: " + error ) ) ; } public doRemoveCompanies ( ) { firebase . remove ( "/companies" ) . then ( ( ) => console . log ( "firebase.remove done" ) , error => console . log ( "firebase.remove error: " + error ) ) ; } public doQueryBulgarianCompanies ( ) { const path = "/companies" ; const onValueEvent = result => { console . log ( "Query result: " + JSON . stringify ( result ) ) ; if ( result . error ) { alert ( { title : "Listener error" , message : result . error , okButtonText : "Darn!" } ) ; } else { this . set ( "path" , path ) ; this . set ( "type" , result . type ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . value ) ) ; } } ; firebase . query ( onValueEvent , path , { orderBy : { type : firebase . QueryOrderByType . CHILD , value : 'since' } , ranges : [ { type : firebase . QueryRangeType . START_AT , value : 1999 } , { type : firebase . QueryRangeType . END_AT , value : 2000 } ] , limit : { type : firebase . QueryLimitType . LAST , value : 2 } , singleEvent : true } ) . then ( result => console . log ( "firebase.doQueryBulgarianCompanies done; added a listener, result: " + JSON . stringify ( result ) ) , errorMessage => { alert ( { title : "Query error" , message : errorMessage , okButtonText : "OK, pity" } ) ; } ) ; } public doQueryUsers ( ) { const path = "/users" ; const onValueEvent = result => { console . log ( "Query result: " + JSON . stringify ( result ) ) ; if ( result . error ) { alert ( { title : "Listener error" , message : result . error , okButtonText : "Darn!!" } ) ; } else { this . set ( "path" , path ) ; this . set ( "type" , result . type ) ; this . set ( "key" , result . key ) ; this . set ( "value" , JSON . stringify ( result . value ) ) ; } } ; firebase . query ( onValueEvent , path , { singleEvent : true , orderBy : { type : firebase . QueryOrderByType . CHILD , value : "first" } } ) . then ( result => { console . log ( "This 'result' should be available since singleEvent is true: " + JSON . stringify ( result ) ) ; } , errorMessage => { alert ( { title : "Query error" , message : errorMessage , okButtonText : "OK, pity!" } ) ; } ) ; } public doListAll ( ) { firebaseStorage . listAll ( { remoteFullPath : "uploads/images" } ) . then ( result => { console . log ( JSON . stringify ( result ) ) ; result . items . forEach ( item => { item . listAll ( ) . then ( result2 => console . log ( `Inner result for ITEM ${ item . name }: ${ JSON . stringify ( result2 ) }` ) ) . catch ( err => console . log ( err ) ) ; } ) ; result . prefixes . forEach ( prefix => { prefix . listAll ( ) . then ( result2 => console . log ( `Inner result for PREFIX ${ prefix . name }: ${ JSON . stringify ( result2 ) }` ) ) . catch ( err => console . log ( err ) ) ; } ) ; } ) . catch ( err => console . log ( err ) ) ; } public doUploadFile ( ) { const appPath = knownFolders . currentApp ( ) . path ; const logoPath = appPath + "/images/telerik-logo.png" ; const metadata = { contentType : "demo/test2" , contentLanguage : "de" , customMetadata : { "first" : "first!" , "second" : "second!" } } ; firebaseStorage . uploadFile ( { remoteFullPath : 'uploads/images/telerik-logo-uploaded.png' , localFile : File . fromPath ( logoPath ) , onProgress : status => { console . log ( "Uploaded fraction: " + status . fractionCompleted + " (" + status . percentageCompleted + "%)" ) ; } , metadata } ) . then ( uploadedFile => { alert ( { title : "File upload successful!" , message : JSON . stringify ( uploadedFile ) , okButtonText : "Cool!" } ) ; } , error => { console . log ( "firebase.doUploadFile error: " + error ) ; } ) ; } public doDownloadFile ( ) { const documents = knownFolders . documents ( ) ; const logoPath = documents . path + "/telerik-logo-downloaded.png" ; const localLogoFile = documents . getFile ( "telerik-logo-downloaded.png" ) ; firebaseStorage . downloadFile ( { remoteFullPath : 'uploads/images/telerik-logo-uploaded.png' , localFullPath : logoPath } ) . then ( ( ) => { alert ( { title : "File download successful!" , message : "The file has been downloaded to the requested location" , okButtonText : "OK" } ) ; } , error => { alert ( { title : "File download error" , message : error , okButtonText : "Mmkay!" } ) ; } ) ; } public doGetDownloadUrl ( ) { firebaseStorage . getDownloadUrl ( { remoteFullPath : 'uploads/images/telerik-logo-uploaded.png' } ) . then ( theUrl => { console . log ( "Download url: " + theUrl ) ; alert ( { title : "File download URL determined" , message : "You can download the file at: " + theUrl , okButtonText : "OK!" } ) ; } , error => { alert ( { title : "File download URL error" , message : error , okButtonText : "Mmkay" } ) ; } ) ; } public doDeleteFile ( ) { firebaseStorage . deleteFile ( { remoteFullPath : 'uploads/images/telerik-logo-uploaded.png' } ) . then ( theUrl => { alert ( { title : "File deleted" , message : "Enjoy your day!" , okButtonText : "Thanks ;)" } ) ; } , error => { alert ( { title : "File deletion error" , message : error , okButtonText : "OK" } ) ; } ) ; } public doReauthenticatePwdUser ( ) { firebase . reauthenticate ( { type : firebase . LoginType . PASSWORD , passwordOptions : { email : 'eddyverbruggen@gmail.com' , password : 'firebase' } } ) . then ( ( ) => { alert ( { title : "Re-authenticated password user" , okButtonText : "OK" } ) ; } , error => { alert ( { title : "Re-authenticate error" , message : error , okButtonText : "OK" } ) ; } ) ; } public doReauthenticateGoogleUser ( ) { firebase . reauthenticate ( { type : firebase . LoginType . GOOGLE } ) . then ( result => { console . log ( "Google login OK: " + JSON . stringify ( result ) ) ; alert ( { title : "Reauthenticated with Google" , message : JSON . stringify ( result ) , okButtonText : "Nice!" } ) ; } , error => { alert ( { title : "Re-authenticate error" , message : error , okButtonText : "OK" } ) ; } ) ; } public doReauthenticateFacebookUser ( ) { firebase . reauthenticate ( { type : firebase . LoginType . FACEBOOK } ) . then ( result => { console . log ( "Facebook login OK: " + JSON . stringify ( result ) ) ; alert ( { title : "Reauthenticated with Facebook" , message : JSON . stringify ( result ) , okButtonText : "Nice!" } ) ; } , error => { alert ( { title : "Re-authenticate error" , message : error , okButtonText : "OK" } ) ; } ) ; } public startPerformanceMonitoringTrace ( ) { this . firebaseTrace = firebasePerformance . startTrace ( "myTrace" ) ; console . log ( ">> trace started" ) ; } public stopPerformanceMonitoringTrace ( ) { if ( this . firebaseTrace ) { this . firebaseTrace . stop ( ) ; this . firebaseTrace = undefined ; console . log ( ">> trace stopped" ) ; } } public setValuePerformanceMonitoring ( ) { if ( this . firebaseTrace ) { this . firebaseTrace . setValue ( "foo" , "bar" ) ; console . log ( ">> value set" ) ; } } public getValuePerformanceMonitoring ( ) { if ( this . firebaseTrace ) { console . log ( ">> value: " + this . firebaseTrace . getValue ( "foo" ) ) ; } } public getAttributesPerformanceMonitoring ( ) { if ( this . firebaseTrace ) { console . log ( ">> attributes: " + JSON . stringify ( this . firebaseTrace . getAttributes ( ) ) ) ; } } public removeAttributePerformanceMonitoring ( ) { if ( this . firebaseTrace ) { this . firebaseTrace . removeAttribute ( "foo" ) ; console . log ( ">> attribute removed" ) ; } } public incrementMetricPerformanceMonitoring ( ) { if ( this . firebaseTrace ) { this . firebaseTrace . incrementMetric ( "foo_metric" , 1 ) ; console . log ( ">> metric incremented" ) ; } } public doCrash ( ) { console . log ( "Gonna crash on demand" ) ; firebaseCrashlytics . crash ( ) ; } public doLogMessageCrashlytics ( ) { firebaseCrashlytics . log ( "Tag message" , "TAG" , 1 ) ; if ( isAndroid ) { firebaseCrashlytics . sendCrashLog ( new java . lang . Exception ( "test Exception" ) ) ; } else if ( isIOS ) { firebaseCrashlytics . sendCrashLog ( new NSError ( { domain : 'ShiploopHttpResponseErrorDomain' , code : 42 , userInfo : null } ) ) ; } alert ( { title : "Message logged" , message : "Check the Firebase console" , okButtonText : "Okay" } ) ; } public doSetCrashlyticString ( ) { firebaseCrashlytics . setString ( "test_key" , "test_value" ) ; alert ( { title : "String created" , message : "New string key created, log a new message and check firebase console" , okButtonText : "Okay" } ) ; } public doSetCrashlyticBool ( ) { firebaseCrashlytics . setBool ( "test_key_bool" , true ) ; alert ( { title : "Bool created" , message : "New string key created, log a new message and check firebase console" , okButtonText : "Okay" } ) ; } public doSetCrashlyticInt ( ) { firebaseCrashlytics . setInt ( "test_key_int" , 2 ) ; alert ( { title : "Int created" , message : "New string key created, log a new message and check firebase console" , okButtonText : "Okay" } ) ; } public doSetCrashlyticDouble ( ) { firebaseCrashlytics . setDouble ( "test_key_double" , 56615.55548465 ) ; alert ( { title : "Double created" , message : "New string key created, log a new message and check firebase console" , okButtonText : "Okay" } ) ; } public doSetCrashlyticFloat ( ) { firebaseCrashlytics . setFloat ( "test_key" , 54646.45 ) ; alert ( { title : "Float created" , message : "New string key created, log a new message and check firebase console" , okButtonText : "Okay" } ) ; } public doSetUserId ( ) { firebaseCrashlytics . setUserId ( "user#42" ) ; alert ( { title : "User id changed" , message : "Log a new message and check firebase console" , okButtonText : "Okay" } ) ; } }