Skip to content

Commit

Permalink
Merge pull request #12 from chronogolf/fix-parent-class
Browse files Browse the repository at this point in the history
Fix classes and iOS openStore
  • Loading branch information
SBats authored Oct 13, 2017
2 parents f6c59d2 + 56f3dc9 commit a73dd00
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 170 deletions.
104 changes: 0 additions & 104 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/interfaces/app-store-result.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface IAppleStoreResult {
version: string
minimumOsVersion: string
currentVersionReleaseDate: string
trackViewUrl: string
}

/*
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/store-update.config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface IStoreUpdateConfig {
revisionUpdateAlertType?: number
notifyNbDaysAfterRelease?: number
countryCode?: string
onConfirmed?: any
}
29 changes: 20 additions & 9 deletions src/store-update.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,30 @@ app.on(app.resumeEvent, () => {
StoreUpdate.checkForUpdate()
})

export class StoreUpdate extends StoreUpdateCommon {
private static _checkHasTimeouted = false
private static _timeoutChecker
private static _looperChecker
export class StoreUpdate {
private static _storeUpdateCommon
private static _instantiated = false

/*
* Public
*/
* Public
*/

static init(config: IStoreUpdateConfig) {
if (StoreUpdate._instantiated) throw new Error('NS Store Update already configured')
StoreUpdate._storeUpdateCommon = new StoreUpdateCommon({
...config,
onConfirmed: StoreUpdate._openStore.bind(StoreUpdate),
})
StoreUpdate._instantiated = true
}

static checkForUpdate() {
GooglePlayHelper.getAppInfos(StoreUpdate.getBundleId())
if (!StoreUpdate._instantiated) return
GooglePlayHelper.getAppInfos(StoreUpdate._storeUpdateCommon.getBundleId())
.then(StoreUpdate._extendResults)
.then(StoreUpdate._triggerAlertIfEligible.bind(StoreUpdate))
.then(
StoreUpdate._storeUpdateCommon.triggerAlertIfEligible.bind(StoreUpdate._storeUpdateCommon)
)
.catch(console.error)
}

Expand All @@ -34,7 +45,7 @@ export class StoreUpdate extends StoreUpdateCommon {
*/

protected static _openStore() {
const storeUrl = `market://details?id=${StoreUpdate.getBundleId()}`
const storeUrl = `market://details?id=${StoreUpdate._storeUpdateCommon.getBundleId()}`
utils.openUrl(storeUrl)
}

Expand Down
104 changes: 53 additions & 51 deletions src/store-update.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,80 +22,66 @@ const defaultConfig: IStoreUpdateConfig = {
notifyNbDaysAfterRelease: 1,
patchUpdateAlertType: AlertTypesConstants.NONE,
revisionUpdateAlertType: AlertTypesConstants.NONE,
onConfirmed: () => console.log('User confirmed!'),
}

export class StoreUpdateCommon {
static instatiated = false
countryCode

protected static _countryCode
private _majorUpdateAlertType
private _minorUpdateAlertType
private _patchUpdateAlertType
private _revisionUpdateAlertType
private _notifyNbDaysAfterRelease
private _onConfirmed

private static _majorUpdateAlertType
private static _minorUpdateAlertType
private static _patchUpdateAlertType
private static _revisionUpdateAlertType
private static _notifyNbDaysAfterRelease
constructor(config?: IStoreUpdateConfig) {
if (config) this._init(config)
}

/*
* Public
*/

static init(config: IStoreUpdateConfig) {
if (this.instatiated) return
const conf = {
...defaultConfig,
...config,
}
this.instatiated = true
this._majorUpdateAlertType = conf.majorUpdateAlertType
this._minorUpdateAlertType = conf.minorUpdateAlertType
this._patchUpdateAlertType = conf.patchUpdateAlertType
this._revisionUpdateAlertType = conf.revisionUpdateAlertType
this._notifyNbDaysAfterRelease = conf.notifyNbDaysAfterRelease
this._countryCode = conf.countryCode
LocalesHelper.changeLang(device.language)
}

static getBundleId(): string {
getBundleId(): string {
return getAppIdSync()
}

static getLocalVersionNumber(): string {
getLocalVersionNumber(): string {
return `${getVersionNameSync()}.${getVersionCodeSync()}`
}

/*
* Protected
*/

protected static _isEligibleForUpdate({
isEligibleForUpdate({
version,
currentVersionReleaseDate,
minimumOsVersion,
systemVersion,
}): boolean {
if (!this._isUpdateCompatibleWithDeviceOS(systemVersion, minimumOsVersion)) return false
if (!this._isUpdateCompatibleWithDeviceOS(systemVersion, minimumOsVersion)) {
return false
}
if (!this._hasBeenReleasedLongerThanDelay(currentVersionReleaseDate)) return false
if (this._isCurrentVersionSkipped(version)) return false
if (!this._isAppStoreVersionNewer(version)) return false
return true
}

protected static _openStore() {
openStore() {
// Overriden by platforms
}

protected static _setVersionAsSkipped(version: string) {
setVersionAsSkipped(version: string) {
appSettings.setString(LAST_VERSION_SKIPPED_KEY, version)
}

protected static _triggerAlertForUpdate(version: string) {
return this._showAlertForUpdate(version).then((confirmed: boolean) => {
if (confirmed) this._openStore()
else this._setVersionAsSkipped(version)
triggerAlertForUpdate(version: string) {
return this.showAlertForUpdate(version).then((confirmed: boolean) => {
if (confirmed) this._onConfirmed()
else this.setVersionAsSkipped(version)
})
}

protected static _getAlertTypeForVersion(currentAppStoreVersion: string): number {
getAlertTypeForVersion(currentAppStoreVersion: string): number {
const updateType = this._getUpdateTypeForVersion(currentAppStoreVersion)

switch (updateType) {
Expand All @@ -116,7 +102,7 @@ export class StoreUpdateCommon {
}
}

protected static _buildDialogOptions({ skippable = true } = {}): ConfirmOptions {
buildDialogOptions({ skippable = true } = {}): ConfirmOptions {
let options = {
message: LocalesHelper.translate('ALERT_MESSAGE'),
neutralButtonText: null,
Expand All @@ -133,43 +119,59 @@ export class StoreUpdateCommon {
return options
}

protected static _showAlertForUpdate(version: string): Promise<boolean> {
const alertType = this._getAlertTypeForVersion(version)
showAlertForUpdate(version: string): Promise<boolean> {
const alertType = this.getAlertTypeForVersion(version)
switch (alertType) {
case AlertTypesConstants.FORCE: {
const options: ConfirmOptions = this._buildDialogOptions({ skippable: false })
const options: ConfirmOptions = this.buildDialogOptions({ skippable: false })
return confirm(options)
}
case AlertTypesConstants.OPTION: {
const options: ConfirmOptions = this._buildDialogOptions()
const options: ConfirmOptions = this.buildDialogOptions()
return confirm(options)
}
default:
return Promise.reject(null)
}
}

protected static _triggerAlertIfEligible(result) {
if (this._isEligibleForUpdate(result)) {
this._triggerAlertForUpdate(result.version)
triggerAlertIfEligible(result) {
if (this.isEligibleForUpdate(result)) {
this.triggerAlertForUpdate(result.version)
}
}

/*
* Private
*/

private static _isAppStoreVersionNewer(storeVersion: string): boolean {
private _init(config: IStoreUpdateConfig) {
const conf = {
...defaultConfig,
...config,
}

this._majorUpdateAlertType = conf.majorUpdateAlertType
this._minorUpdateAlertType = conf.minorUpdateAlertType
this._patchUpdateAlertType = conf.patchUpdateAlertType
this._revisionUpdateAlertType = conf.revisionUpdateAlertType
this._notifyNbDaysAfterRelease = conf.notifyNbDaysAfterRelease
this.countryCode = conf.countryCode
this._onConfirmed = conf.onConfirmed
LocalesHelper.changeLang(device.language)
}

private _isAppStoreVersionNewer(storeVersion: string): boolean {
if (storeVersion === null) return false
return VersionHelper.isHigher(storeVersion, this.getLocalVersionNumber())
}

private static _isCurrentVersionSkipped(currentAppStoreVersion: string): boolean {
private _isCurrentVersionSkipped(currentAppStoreVersion: string): boolean {
const lastVersionSkipped = appSettings.getString(LAST_VERSION_SKIPPED_KEY)
return currentAppStoreVersion === lastVersionSkipped
}

private static _hasBeenReleasedLongerThanDelay(releaseDate: string): boolean {
private _hasBeenReleasedLongerThanDelay(releaseDate: string): boolean {
if (releaseDate === null) return false

const daysSinceRelease = moment().diff(moment(new Date(releaseDate)), 'days')
Expand All @@ -184,7 +186,7 @@ export class StoreUpdateCommon {
return daysSinceRelease >= this._notifyNbDaysAfterRelease
}

private static _isUpdateCompatibleWithDeviceOS(
private _isUpdateCompatibleWithDeviceOS(
deviceVersion: string,
minimumRequiredOSVersion: string
): boolean {
Expand All @@ -195,7 +197,7 @@ export class StoreUpdateCommon {
return isCompatible
}

private static _getUpdateTypeForVersion(currentAppStoreVersion: string): number {
private _getUpdateTypeForVersion(currentAppStoreVersion: string): number {
const localVersion = this.getLocalVersionNumber()
if (VersionHelper.isMajorUpdate(currentAppStoreVersion, localVersion)) {
return UpdateTypesConstants.MAJOR
Expand Down
Loading

0 comments on commit a73dd00

Please sign in to comment.