import { Injectable } from '@angular/core' ; import * as moment from 'moment' ; import { StorageService } from './storage' ; import { STORE_KEY } from '../utils/constants' ; @ Injectable ( ) export class HistoryService { constructor ( public storage ) { } async getTearDate ( ) { return await this . storage . get ( STORE_KEY . TORN_DATE ) ; } async setTearDay ( day ) { const lastTear = await this . getTearDate ( ) ; if ( ! lastTear ) { await this . storage . set ( STORE_KEY . FIRST_TEAR , 'torn' ) ; } else if ( moment ( lastTear ) . isAfter ( day ) ) { return ; } return await this . storage . set ( STORE_KEY . TORN_DATE , day . toDate ( ) ) ; } async tearNextDay ( currentDate , width , height ) { const lastTear = await this . getTearDate ( ) ; if ( ! lastTear ) { await this . storage . set ( STORE_KEY . FIRST_TEAR , 'torn' ) ; } return await this . storage . set ( STORE_KEY . TORN_DATE , currentDate . toDate ( ) ) ; } async isFirstOpen ( ) { return await this . storage . get ( STORE_KEY . FIRST_OPEN ) == null ; } async setFirstOpened ( ) { await this . storage . set ( STORE_KEY . FIRST_OPEN , 'opened' ) ; } async isFirstTear ( ) { return ! await this . storage . get ( STORE_KEY . FIRST_TEAR ) ; } async canPromptAd ( ) { if ( await this . storage . get ( STORE_KEY . DONT_ASK_AD_AGAIN ) === true ) { return false ; } return Math . random ( ) > 0.5 ; } async markDontPromptAd ( ) { await this . storage . set ( STORE_KEY . DONT_ASK_AD_AGAIN , true ) ; } }