refactor: create InAppReviewProvider

This commit is contained in:
Rainer Killinger
2025-08-12 18:08:09 +02:00
parent 1c39483e9d
commit 5800238f8d
23 changed files with 313 additions and 72 deletions

View File

@@ -27,7 +27,7 @@ include ':capacitor-filesystem'
project(':capacitor-filesystem').projectDir = new File('../../../node_modules/.pnpm/@capacitor+filesystem@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/filesystem/android')
include ':capacitor-geolocation'
project(':capacitor-geolocation').projectDir = new File('../../../node_modules/.pnpm/@capacitor+geolocation@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation/android')
project(':capacitor-geolocation').projectDir = new File('../../../node_modules/.pnpm/@capacitor+geolocation@7.1.5_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation/android')
include ':capacitor-haptics'
project(':capacitor-haptics').projectDir = new File('../../../node_modules/.pnpm/@capacitor+haptics@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/haptics/android')

View File

@@ -299,7 +299,7 @@
"icon": "school",
"items": [
{
"icon": "grade",
"icon": "star",
"route": "/favorites",
"title": "favorites",
"translations": {

View File

@@ -20,7 +20,7 @@ const config = {
htmlGlob: 'src/**/*.html',
scriptGlob: 'src/**/*.ts',
additionalIcons: {
about: ['copyright', 'campaign', 'policy', 'description', 'text_snippet'],
about: ['copyright', 'campaign', 'policy', 'description', 'text_snippet', 'expand_more'],
navigation: [
'home',
'newspaper',
@@ -30,7 +30,7 @@ const config = {
'local_library',
'inventory_2',
'map',
'grade',
'star',
'account_circle',
'settings',
'info',

View File

@@ -19,7 +19,7 @@ def capacitor_pods
pod 'CapacitorDevice', :path => '../../../../node_modules/.pnpm/@capacitor+device@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/device'
pod 'CapacitorDialog', :path => '../../../../node_modules/.pnpm/@capacitor+dialog@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/dialog'
pod 'CapacitorFilesystem', :path => '../../../../node_modules/.pnpm/@capacitor+filesystem@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/filesystem'
pod 'CapacitorGeolocation', :path => '../../../../node_modules/.pnpm/@capacitor+geolocation@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation'
pod 'CapacitorGeolocation', :path => '../../../../node_modules/.pnpm/@capacitor+geolocation@7.1.5_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation'
pod 'CapacitorHaptics', :path => '../../../../node_modules/.pnpm/@capacitor+haptics@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/haptics'
pod 'CapacitorKeyboard', :path => '../../../../node_modules/.pnpm/@capacitor+keyboard@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/keyboard'
pod 'CapacitorLocalNotifications', :path => '../../../../node_modules/.pnpm/@capacitor+local-notifications@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/local-notifications'

View File

@@ -19,9 +19,9 @@ PODS:
- CapacitorFilesystem (7.1.4):
- Capacitor
- IONFilesystemLib (~> 1.0.1)
- CapacitorGeolocation (7.1.4):
- CapacitorGeolocation (7.1.5):
- Capacitor
- IONGeolocationLib (~> 1.0)
- IONGeolocationLib (= 1.0.1)
- CapacitorHaptics (7.0.2):
- Capacitor
- CapacitorKeyboard (7.0.2):
@@ -44,7 +44,7 @@ PODS:
- CordovaPlugins (7.4.2):
- CapacitorCordova
- IONFilesystemLib (1.0.1)
- IONGeolocationLib (1.0.0)
- IONGeolocationLib (1.0.1)
- SwiftKeychainWrapper (4.0.1)
- TransistorsoftCapacitorBackgroundFetch (7.1.0):
- Capacitor
@@ -60,7 +60,7 @@ DEPENDENCIES:
- "CapacitorDevice (from `../../../../node_modules/.pnpm/@capacitor+device@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/device`)"
- "CapacitorDialog (from `../../../../node_modules/.pnpm/@capacitor+dialog@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/dialog`)"
- "CapacitorFilesystem (from `../../../../node_modules/.pnpm/@capacitor+filesystem@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/filesystem`)"
- "CapacitorGeolocation (from `../../../../node_modules/.pnpm/@capacitor+geolocation@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation`)"
- "CapacitorGeolocation (from `../../../../node_modules/.pnpm/@capacitor+geolocation@7.1.5_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation`)"
- "CapacitorHaptics (from `../../../../node_modules/.pnpm/@capacitor+haptics@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/haptics`)"
- "CapacitorKeyboard (from `../../../../node_modules/.pnpm/@capacitor+keyboard@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/keyboard`)"
- "CapacitorLocalNotifications (from `../../../../node_modules/.pnpm/@capacitor+local-notifications@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/local-notifications`)"
@@ -101,7 +101,7 @@ EXTERNAL SOURCES:
CapacitorFilesystem:
:path: "../../../../node_modules/.pnpm/@capacitor+filesystem@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/filesystem"
CapacitorGeolocation:
:path: "../../../../node_modules/.pnpm/@capacitor+geolocation@7.1.4_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation"
:path: "../../../../node_modules/.pnpm/@capacitor+geolocation@7.1.5_@capacitor+core@7.4.2/node_modules/@capacitor/geolocation"
CapacitorHaptics:
:path: "../../../../node_modules/.pnpm/@capacitor+haptics@7.0.2_@capacitor+core@7.4.2/node_modules/@capacitor/haptics"
CapacitorKeyboard:
@@ -136,7 +136,7 @@ SPEC CHECKSUMS:
CapacitorDevice: 81ae78d5d1942707caad79276badd458bf6ec603
CapacitorDialog: 5bf72a94b747fb339df6f64ef60812e5e4630ad2
CapacitorFilesystem: f9bd850c41e048180e5dc0cbb90f2033ede9d2cc
CapacitorGeolocation: 32a1f849c38c72b5071de36a28ebc548ef3bb611
CapacitorGeolocation: 84f868bea4c2499aebc3bb3a88fd0a508f87ab87
CapacitorHaptics: b3fb2869e72c4466e18ce9ccbeb60a3d8723b3d4
CapacitorKeyboard: a86aa9e4741b6444a802df26440a92ae041b34a6
CapacitorLocalNotifications: 665188ae8accd40806129073896fb2b39322d858
@@ -148,10 +148,10 @@ SPEC CHECKSUMS:
CapacitorSplashScreen: 157947576b59d913792063a8d442a79e6d283ee5
CordovaPlugins: 7b9a4f380c92ca7f28630723befaca556461f4c3
IONFilesystemLib: 89258b8e3e85465da93127d25d7ce37f977e8a6f
IONGeolocationLib: 81f33f88d025846946de2cf63b0c7628e7c6bc9d
IONGeolocationLib: 20f9d0248a0b5264511fb57a37e25dd2badf797a
SwiftKeychainWrapper: 807ba1d63c33a7d0613288512399cd1eda1e470c
TransistorsoftCapacitorBackgroundFetch: 28e561636145a899f05025d31f627019c16791f5
PODFILE CHECKSUM: e0631161818ed4ac500327f0ee228d71857c0878
PODFILE CHECKSUM: 90bc09990a659848ea76c94331d39bad0b28024d
COCOAPODS: 1.16.2

View File

@@ -66,7 +66,7 @@
"@capacitor/device": "7.0.2",
"@capacitor/dialog": "7.0.2",
"@capacitor/filesystem": "7.1.4",
"@capacitor/geolocation": "7.1.4",
"@capacitor/geolocation": "7.1.5",
"@capacitor/haptics": "7.0.2",
"@capacitor/keyboard": "7.0.2",
"@capacitor/local-notifications": "7.0.2",

View File

@@ -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},

View File

@@ -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();
}
}

View File

@@ -19,6 +19,6 @@
[size]="24"
[fill]="(isFavorite$ | async) || false"
[class.selected]="isFavorite$ | async"
name="grade"
name="star"
></ion-icon>
</ion-button>

View File

@@ -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>

View File

@@ -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 () => {

View File

@@ -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);
});
});

View File

@@ -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;
}
}
}

View File

@@ -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 {}

View File

@@ -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);

View File

@@ -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