mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
refactor: create InAppReviewProvider
This commit is contained in:
@@ -33,6 +33,7 @@ import {sampleAuthConfiguration} from './_helpers/data/sample-configuration';
|
||||
import {StorageProvider} from './modules/storage/storage.provider';
|
||||
import {SimpleBrowser} from './util/browser.factory';
|
||||
import {provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
|
||||
import {InAppReviewProvider} from './modules/settings/in-app-review/in-app-review.provider';
|
||||
|
||||
describe('AppComponent', () => {
|
||||
let platformReadySpy: any;
|
||||
@@ -40,6 +41,7 @@ describe('AppComponent', () => {
|
||||
let translateServiceSpy: jasmine.SpyObj<TranslateService>;
|
||||
let thingTranslateServiceSpy: jasmine.SpyObj<ThingTranslateService>;
|
||||
let settingsProvider: jasmine.SpyObj<SettingsProvider>;
|
||||
let inAppReviewProvider: jasmine.SpyObj<InAppReviewProvider>;
|
||||
let configProvider: jasmine.SpyObj<ConfigProvider>;
|
||||
let ngxLogger: jasmine.SpyObj<NGXLogger>;
|
||||
let scheduleSyncServiceSpy: jasmine.SpyObj<ScheduleSyncService>;
|
||||
@@ -85,6 +87,7 @@ describe('AppComponent', () => {
|
||||
{provide: ThingTranslateService, useValue: thingTranslateServiceSpy},
|
||||
{provide: ScheduleSyncService, useValue: scheduleSyncServiceSpy},
|
||||
{provide: SettingsProvider, useValue: settingsProvider},
|
||||
{provide: InAppReviewProvider, useValue: inAppReviewProvider},
|
||||
{provide: ConfigProvider, useValue: configProvider},
|
||||
{provide: NGXLogger, useValue: ngxLogger},
|
||||
{provide: StorageProvider, useValue: storageProvider},
|
||||
|
||||
@@ -17,6 +17,7 @@ import {Router} from '@angular/router';
|
||||
import {App, URLOpenListenerEvent} from '@capacitor/app';
|
||||
import {Platform, ToastController} from '@ionic/angular/standalone';
|
||||
import {SettingsProvider} from './modules/settings/settings.provider';
|
||||
import {InAppReviewProvider} from './modules/settings/in-app-review/in-app-review.provider';
|
||||
import {AuthHelperService} from './modules/auth/auth-helper.service';
|
||||
import {environment} from '../environments/environment';
|
||||
import {Capacitor} from '@capacitor/core';
|
||||
@@ -55,6 +56,7 @@ export class AppComponent implements AfterContentInit {
|
||||
constructor(
|
||||
private readonly platform: Platform,
|
||||
private readonly settingsProvider: SettingsProvider,
|
||||
private readonly inAppReviewProvider: InAppReviewProvider,
|
||||
private readonly router: Router,
|
||||
private readonly zone: NgZone,
|
||||
private readonly authHelper: AuthHelperService,
|
||||
@@ -86,6 +88,8 @@ export class AppComponent implements AfterContentInit {
|
||||
async hideSplash() {
|
||||
if (Capacitor.isNativePlatform()) {
|
||||
void SplashScreen.hide();
|
||||
void this.inAppReviewProvider.increaseSessionCount();
|
||||
void this.inAppReviewProvider.startInAppReviewIfFeasible();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,6 @@
|
||||
[size]="24"
|
||||
[fill]="(isFavorite$ | async) || false"
|
||||
[class.selected]="isFavorite$ | async"
|
||||
name="grade"
|
||||
name="star"
|
||||
></ion-icon>
|
||||
</ion-button>
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
slot="icon-only"
|
||||
[size]="32"
|
||||
color="medium"
|
||||
name="grade"
|
||||
name="star"
|
||||
></ion-icon>
|
||||
}
|
||||
<label class="thank-you">{{ 'ratings.thank_you' | translate }}</label>
|
||||
|
||||
@@ -25,7 +25,7 @@ import {FilterContext, SortContext} from './context-type';
|
||||
import {provideIonicAngular, ModalController} from '@ionic/angular/standalone';
|
||||
import {BehaviorSubject, of} from 'rxjs';
|
||||
import {addIcons} from 'ionicons';
|
||||
import {swapVertical, trash} from 'ionicons/icons';
|
||||
import {swapVertical, trash, menu} from 'ionicons/icons';
|
||||
|
||||
describe('ContextMenuModalComponent', () => {
|
||||
let fixture: ComponentFixture<ContextMenuModalComponent>;
|
||||
@@ -37,6 +37,7 @@ describe('ContextMenuModalComponent', () => {
|
||||
addIcons({
|
||||
delete: trash,
|
||||
sort: swapVertical,
|
||||
menu: menu,
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (C) 2022 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/* eslint-disable unicorn/no-useless-undefined, @typescript-eslint/no-non-null-assertion */
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {StorageProvider} from '../../storage/storage.provider';
|
||||
import {
|
||||
InAppReviewProvider,
|
||||
IN_APP_REVIEW_COOLDOWN_DAYS,
|
||||
IN_APP_REVIEW_NECESSARY_SESSIONS,
|
||||
IN_APP_REVIEW_SETTINGS_KEY,
|
||||
IN_APP_REVIEW_SESSIONS_KEY,
|
||||
IN_APP_REVIEW_LAST_RATING_KEY,
|
||||
} from './in-app-review.provider';
|
||||
|
||||
describe('InappRatingProvider', () => {
|
||||
let storageProviderSpy: jasmine.SpyObj<StorageProvider>;
|
||||
let inappRatingProvider: InAppReviewProvider;
|
||||
const ONE_DAY_IN_MILLIS = 1000 * 3600 * 24;
|
||||
|
||||
beforeEach(async () => {
|
||||
storageProviderSpy = jasmine.createSpyObj('StorageProvider', ['init', 'get', 'has', 'put']);
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
imports: [],
|
||||
providers: [
|
||||
InAppReviewProvider,
|
||||
{
|
||||
provide: StorageProvider,
|
||||
useValue: storageProviderSpy,
|
||||
},
|
||||
],
|
||||
});
|
||||
// set settings returned from config
|
||||
inappRatingProvider = TestBed.inject(InAppReviewProvider);
|
||||
storageProviderSpy.has.and.returnValue(Promise.resolve(false));
|
||||
});
|
||||
|
||||
it('should set user sessions count starting with one', async () => {
|
||||
storageProviderSpy.get.and.returnValue(Promise.resolve(0));
|
||||
const sessionCount = await inappRatingProvider.increaseSessionCount();
|
||||
expect(sessionCount).toEqual(1);
|
||||
});
|
||||
|
||||
it('should start in app rating/review flow if contitions are met', async () => {
|
||||
const nowMinusCooldownDays = Date.now() - (IN_APP_REVIEW_COOLDOWN_DAYS + 1) * ONE_DAY_IN_MILLIS;
|
||||
spyOn(inappRatingProvider, 'requestReview').and.returnValue(Promise.resolve());
|
||||
|
||||
storageProviderSpy.get
|
||||
.withArgs(`${IN_APP_REVIEW_SETTINGS_KEY}.${IN_APP_REVIEW_SESSIONS_KEY}`)
|
||||
.and.returnValue(Promise.resolve(IN_APP_REVIEW_NECESSARY_SESSIONS));
|
||||
storageProviderSpy.get
|
||||
.withArgs(`${IN_APP_REVIEW_SETTINGS_KEY}.${IN_APP_REVIEW_LAST_RATING_KEY}`)
|
||||
.and.returnValue(Promise.resolve(nowMinusCooldownDays));
|
||||
await inappRatingProvider.startInAppReviewIfFeasible();
|
||||
expect(inappRatingProvider.requestReview).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should not start in app rating/review flow if contitions are not met', async () => {
|
||||
const nowMinusCooldownDays = Date.now() - IN_APP_REVIEW_COOLDOWN_DAYS * ONE_DAY_IN_MILLIS;
|
||||
spyOn(inappRatingProvider, 'requestReview').and.returnValue(Promise.resolve());
|
||||
|
||||
storageProviderSpy.get
|
||||
.withArgs(`${IN_APP_REVIEW_SETTINGS_KEY}.${IN_APP_REVIEW_SESSIONS_KEY}`)
|
||||
.and.returnValue(Promise.resolve(IN_APP_REVIEW_NECESSARY_SESSIONS - 1));
|
||||
storageProviderSpy.get
|
||||
.withArgs(`${IN_APP_REVIEW_SETTINGS_KEY}.${IN_APP_REVIEW_LAST_RATING_KEY}`)
|
||||
.and.returnValue(Promise.resolve(nowMinusCooldownDays));
|
||||
|
||||
await inappRatingProvider.startInAppReviewIfFeasible();
|
||||
|
||||
storageProviderSpy.get
|
||||
.withArgs(`${IN_APP_REVIEW_SETTINGS_KEY}.${IN_APP_REVIEW_SESSIONS_KEY}`)
|
||||
.and.returnValue(Promise.resolve(IN_APP_REVIEW_NECESSARY_SESSIONS));
|
||||
storageProviderSpy.get
|
||||
.withArgs(`${IN_APP_REVIEW_SETTINGS_KEY}.${IN_APP_REVIEW_LAST_RATING_KEY}`)
|
||||
.and.returnValue(Promise.resolve(nowMinusCooldownDays + ONE_DAY_IN_MILLIS));
|
||||
|
||||
await inappRatingProvider.startInAppReviewIfFeasible();
|
||||
|
||||
expect(inappRatingProvider.requestReview).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,120 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {InAppReview} from '@capacitor-community/in-app-review';
|
||||
import {StorageProvider} from '../../storage/storage.provider';
|
||||
|
||||
export const IN_APP_REVIEW_COOLDOWN_DAYS = 365;
|
||||
export const IN_APP_REVIEW_NECESSARY_SESSIONS = 3;
|
||||
|
||||
export const IN_APP_REVIEW_SETTINGS_KEY = 'inapp-rating';
|
||||
export const IN_APP_REVIEW_SESSIONS_KEY = 'sessions';
|
||||
export const IN_APP_REVIEW_LAST_RATING_KEY = 'last-rating';
|
||||
export type IN_APP_REVIEW_KEYS = typeof IN_APP_REVIEW_SESSIONS_KEY | typeof IN_APP_REVIEW_LAST_RATING_KEY;
|
||||
|
||||
/**
|
||||
* Provider for In-App Review
|
||||
*/
|
||||
@Injectable()
|
||||
export class InAppReviewProvider {
|
||||
requestReview: () => Promise<void>;
|
||||
|
||||
/**
|
||||
* @param storageProvider TODO
|
||||
*/
|
||||
constructor(private readonly storageProvider: StorageProvider) {
|
||||
this.requestReview = InAppReview.requestReview;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private inappRatingSettingStorageKey(key: string): string {
|
||||
return `${IN_APP_REVIEW_SETTINGS_KEY}.${key}`;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private async getInAppReviewSetting<T>(
|
||||
key: IN_APP_REVIEW_KEYS,
|
||||
defaultValue: T,
|
||||
): Promise<typeof defaultValue> {
|
||||
try {
|
||||
return await this.storageProvider.get<typeof defaultValue>(this.inappRatingSettingStorageKey(key));
|
||||
} catch {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private async setInAppReviewSetting<T>(key: IN_APP_REVIEW_KEYS, value: T) {
|
||||
return this.storageProvider.put<typeof value>(this.inappRatingSettingStorageKey(key), value);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private async setInAppReviewSessions(value: number) {
|
||||
return this.setInAppReviewSetting(IN_APP_REVIEW_SESSIONS_KEY, value);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private async getInAppReviewSessions(): Promise<number> {
|
||||
return this.getInAppReviewSetting(IN_APP_REVIEW_SESSIONS_KEY, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private async setInAppReviewLastRating(value: Date) {
|
||||
return this.setInAppReviewSetting(IN_APP_REVIEW_LAST_RATING_KEY, value.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private async getInAppReviewLastRating(): Promise<Date> {
|
||||
return this.getInAppReviewSetting(IN_APP_REVIEW_LAST_RATING_KEY, 0).then(timestamp => {
|
||||
return new Date(timestamp);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Increases session count to keep local info how often the app was used.
|
||||
*/
|
||||
public async increaseSessionCount(increment = 1): Promise<number> {
|
||||
try {
|
||||
const currentSessions = await this.getInAppReviewSessions();
|
||||
await this.setInAppReviewSessions(currentSessions + increment);
|
||||
return currentSessions + increment;
|
||||
} catch {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes In App Review Flow/Views depending on the OS iff conditions are met.
|
||||
*/
|
||||
public async startInAppReviewIfFeasible(): Promise<boolean> {
|
||||
try {
|
||||
const currentSessions = await this.getInAppReviewSessions();
|
||||
const lastRating = await this.getInAppReviewLastRating();
|
||||
const dateDiffMillis = Math.abs(lastRating.getTime() - Date.now());
|
||||
const dateDiffDays = Math.floor(dateDiffMillis / (1000 * 3600 * 24));
|
||||
|
||||
if (currentSessions < IN_APP_REVIEW_NECESSARY_SESSIONS || dateDiffDays < IN_APP_REVIEW_COOLDOWN_DAYS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await this.requestReview();
|
||||
await this.setInAppReviewLastRating(new Date());
|
||||
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,7 @@ import {
|
||||
IonToolbar,
|
||||
} from '@ionic/angular/standalone';
|
||||
import {IonIconDirective} from 'src/app/util/ion-icon/ion-icon.directive';
|
||||
import {InAppReviewProvider} from './in-app-review/in-app-review.provider';
|
||||
|
||||
const settingsRoutes: Routes = [{path: 'settings', component: SettingsPageComponent}];
|
||||
|
||||
@@ -101,6 +102,13 @@ const settingsRoutes: Routes = [{path: 'settings', component: SettingsPageCompon
|
||||
IonInput,
|
||||
IonNote,
|
||||
],
|
||||
providers: [ScheduleSyncService, SettingsProvider, CalendarService, ScheduleProvider, ThingTranslatePipe],
|
||||
providers: [
|
||||
ScheduleSyncService,
|
||||
SettingsProvider,
|
||||
InAppReviewProvider,
|
||||
CalendarService,
|
||||
ScheduleProvider,
|
||||
ThingTranslatePipe,
|
||||
],
|
||||
})
|
||||
export class SettingsModule {}
|
||||
|
||||
@@ -106,7 +106,6 @@ describe('StorageProvider', () => {
|
||||
});
|
||||
|
||||
it('should put multiple values into the storage', async () => {
|
||||
// @ts-expect-error no need to return anything for this case
|
||||
spyOn(storageProvider, 'put').and.callFake(() => Promise.resolve());
|
||||
await storageProvider.putMultiple(sampleEntries);
|
||||
|
||||
|
||||
@@ -120,8 +120,9 @@ export class StorageProvider {
|
||||
* Puts a value of type T into the storage using provided key
|
||||
* @param key Unique identifier
|
||||
* @param value Resource to store under the key
|
||||
* @returns Returns a promise that resolves when the key and value are set
|
||||
*/
|
||||
async put<T>(key: string, value: T): Promise<T> {
|
||||
async put<T>(key: string, value: T) {
|
||||
return this.storage.set(key, value);
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
Reference in New Issue
Block a user