mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-08 14:32:50 +00:00
refactor: create InappRatingProvider
This commit is contained in:
@@ -33,6 +33,7 @@ import {sampleAuthConfiguration} from './_helpers/data/sample-configuration';
|
|||||||
import {StorageProvider} from './modules/storage/storage.provider';
|
import {StorageProvider} from './modules/storage/storage.provider';
|
||||||
import {SimpleBrowser} from './util/browser.factory';
|
import {SimpleBrowser} from './util/browser.factory';
|
||||||
import {provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
|
import {provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
|
||||||
|
import {InappRatingProvider} from './modules/settings/inapp-rating/inapp-rating.provider';
|
||||||
|
|
||||||
describe('AppComponent', () => {
|
describe('AppComponent', () => {
|
||||||
let platformReadySpy: any;
|
let platformReadySpy: any;
|
||||||
@@ -40,6 +41,7 @@ describe('AppComponent', () => {
|
|||||||
let translateServiceSpy: jasmine.SpyObj<TranslateService>;
|
let translateServiceSpy: jasmine.SpyObj<TranslateService>;
|
||||||
let thingTranslateServiceSpy: jasmine.SpyObj<ThingTranslateService>;
|
let thingTranslateServiceSpy: jasmine.SpyObj<ThingTranslateService>;
|
||||||
let settingsProvider: jasmine.SpyObj<SettingsProvider>;
|
let settingsProvider: jasmine.SpyObj<SettingsProvider>;
|
||||||
|
let inappRatingsProvider: jasmine.SpyObj<InappRatingProvider>;
|
||||||
let configProvider: jasmine.SpyObj<ConfigProvider>;
|
let configProvider: jasmine.SpyObj<ConfigProvider>;
|
||||||
let ngxLogger: jasmine.SpyObj<NGXLogger>;
|
let ngxLogger: jasmine.SpyObj<NGXLogger>;
|
||||||
let scheduleSyncServiceSpy: jasmine.SpyObj<ScheduleSyncService>;
|
let scheduleSyncServiceSpy: jasmine.SpyObj<ScheduleSyncService>;
|
||||||
@@ -85,6 +87,7 @@ describe('AppComponent', () => {
|
|||||||
{provide: ThingTranslateService, useValue: thingTranslateServiceSpy},
|
{provide: ThingTranslateService, useValue: thingTranslateServiceSpy},
|
||||||
{provide: ScheduleSyncService, useValue: scheduleSyncServiceSpy},
|
{provide: ScheduleSyncService, useValue: scheduleSyncServiceSpy},
|
||||||
{provide: SettingsProvider, useValue: settingsProvider},
|
{provide: SettingsProvider, useValue: settingsProvider},
|
||||||
|
{provide: InappRatingProvider, useValue: inappRatingsProvider},
|
||||||
{provide: ConfigProvider, useValue: configProvider},
|
{provide: ConfigProvider, useValue: configProvider},
|
||||||
{provide: NGXLogger, useValue: ngxLogger},
|
{provide: NGXLogger, useValue: ngxLogger},
|
||||||
{provide: StorageProvider, useValue: storageProvider},
|
{provide: StorageProvider, useValue: storageProvider},
|
||||||
|
|||||||
@@ -17,10 +17,7 @@ import {Router} from '@angular/router';
|
|||||||
import {App, URLOpenListenerEvent} from '@capacitor/app';
|
import {App, URLOpenListenerEvent} from '@capacitor/app';
|
||||||
import {Platform, ToastController} from '@ionic/angular/standalone';
|
import {Platform, ToastController} from '@ionic/angular/standalone';
|
||||||
import {SettingsProvider} from './modules/settings/settings.provider';
|
import {SettingsProvider} from './modules/settings/settings.provider';
|
||||||
import {
|
import {InappRatingProvider} from './modules/settings/inapp-rating/inapp-rating.provider';
|
||||||
increaseSessionCount,
|
|
||||||
startInappRatingIfFeasible,
|
|
||||||
} from './modules/settings/inapp-rating/inapp-rating.provider.js';
|
|
||||||
import {AuthHelperService} from './modules/auth/auth-helper.service';
|
import {AuthHelperService} from './modules/auth/auth-helper.service';
|
||||||
import {environment} from '../environments/environment';
|
import {environment} from '../environments/environment';
|
||||||
import {Capacitor} from '@capacitor/core';
|
import {Capacitor} from '@capacitor/core';
|
||||||
@@ -28,7 +25,6 @@ import {ScheduleSyncService} from './modules/background/schedule/schedule-sync.s
|
|||||||
import {Keyboard, KeyboardResize} from '@capacitor/keyboard';
|
import {Keyboard, KeyboardResize} from '@capacitor/keyboard';
|
||||||
import {AppVersionService} from './modules/about/app-version.service';
|
import {AppVersionService} from './modules/about/app-version.service';
|
||||||
import {SplashScreen} from '@capacitor/splash-screen';
|
import {SplashScreen} from '@capacitor/splash-screen';
|
||||||
import {StorageProvider} from './modules/storage/storage.provider.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO
|
* TODO
|
||||||
@@ -60,7 +56,7 @@ export class AppComponent implements AfterContentInit {
|
|||||||
constructor(
|
constructor(
|
||||||
private readonly platform: Platform,
|
private readonly platform: Platform,
|
||||||
private readonly settingsProvider: SettingsProvider,
|
private readonly settingsProvider: SettingsProvider,
|
||||||
private readonly storageProvider: StorageProvider,
|
private readonly inappRatingProvider: InappRatingProvider,
|
||||||
private readonly router: Router,
|
private readonly router: Router,
|
||||||
private readonly zone: NgZone,
|
private readonly zone: NgZone,
|
||||||
private readonly authHelper: AuthHelperService,
|
private readonly authHelper: AuthHelperService,
|
||||||
@@ -92,8 +88,8 @@ export class AppComponent implements AfterContentInit {
|
|||||||
async hideSplash() {
|
async hideSplash() {
|
||||||
if (Capacitor.isNativePlatform()) {
|
if (Capacitor.isNativePlatform()) {
|
||||||
void SplashScreen.hide();
|
void SplashScreen.hide();
|
||||||
void increaseSessionCount(this.storageProvider);
|
void this.inappRatingProvider.increaseSessionCount();
|
||||||
void startInappRatingIfFeasible(this.storageProvider);
|
void this.inappRatingProvider.startInappRatingIfFeasible();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import {Injectable} from '@angular/core';
|
||||||
import {InAppReview} from '@capacitor-community/in-app-review';
|
import {InAppReview} from '@capacitor-community/in-app-review';
|
||||||
import {StorageProvider} from '../../storage/storage.provider';
|
import {StorageProvider} from '../../storage/storage.provider';
|
||||||
|
|
||||||
@@ -10,84 +11,93 @@ export const INAPP_RATING_LAST_RATING_KEY = 'last-rating';
|
|||||||
export type INAPP_RATING_KEYS = typeof INAPP_RATING_SESSIONS_KEY | typeof INAPP_RATING_LAST_RATING_KEY;
|
export type INAPP_RATING_KEYS = typeof INAPP_RATING_SESSIONS_KEY | typeof INAPP_RATING_LAST_RATING_KEY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Provider for app settings
|
||||||
*/
|
*/
|
||||||
function inappRatingSettingStorageKey(key: string): string {
|
@Injectable()
|
||||||
return `${INAPP_RATING_SETTINGS_KEY}.${key}`;
|
export class InappRatingProvider {
|
||||||
}
|
/**
|
||||||
|
* @param storageProvider TODO
|
||||||
|
*/
|
||||||
|
constructor(private readonly storageProvider: StorageProvider) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async function getInappRatingSetting<T>(
|
private inappRatingSettingStorageKey(key: string): string {
|
||||||
storageProvider: StorageProvider,
|
return `${INAPP_RATING_SETTINGS_KEY}.${key}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private async getInappRatingSetting<T>(
|
||||||
key: INAPP_RATING_KEYS,
|
key: INAPP_RATING_KEYS,
|
||||||
defaultValue: T,
|
defaultValue: T,
|
||||||
): Promise<typeof defaultValue> {
|
): Promise<typeof defaultValue> {
|
||||||
try {
|
try {
|
||||||
return await storageProvider.get<typeof defaultValue>(inappRatingSettingStorageKey(key));
|
return await this.storageProvider.get<typeof defaultValue>(this.inappRatingSettingStorageKey(key));
|
||||||
} catch {
|
} catch {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async function putInappRatingSetting<T>(storageProvider: StorageProvider, key: INAPP_RATING_KEYS, value: T) {
|
private async setInappRatingSetting<T>(key: INAPP_RATING_KEYS, value: T) {
|
||||||
return storageProvider.put<typeof value>(inappRatingSettingStorageKey(key), value);
|
return this.storageProvider.put<typeof value>(this.inappRatingSettingStorageKey(key), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async function putInappRatingSessions(storageProvider: StorageProvider, value: number) {
|
private async setInappRatingSessions(value: number) {
|
||||||
return putInappRatingSetting(storageProvider, INAPP_RATING_SESSIONS_KEY, value);
|
return this.setInappRatingSetting(INAPP_RATING_SESSIONS_KEY, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async function getInappRatingSessions(storageProvider: StorageProvider): Promise<number> {
|
private async getInappRatingSessions(): Promise<number> {
|
||||||
return getInappRatingSetting(storageProvider, INAPP_RATING_SESSIONS_KEY, 0);
|
return this.getInappRatingSetting(INAPP_RATING_SESSIONS_KEY, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async function putInappRatingLastRating(storageProvider: StorageProvider, value: Date) {
|
private async setInappRatingLastRating(value: Date) {
|
||||||
return putInappRatingSetting(storageProvider, INAPP_RATING_SESSIONS_KEY, value.getTime());
|
return this.setInappRatingSetting(INAPP_RATING_SESSIONS_KEY, value.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async function getInappRatingLastRating(storageProvider: StorageProvider): Promise<Date> {
|
private async getInappRatingLastRating(): Promise<Date> {
|
||||||
return getInappRatingSetting(storageProvider, INAPP_RATING_SESSIONS_KEY, 0).then(timestamp => {
|
return this.getInappRatingSetting(INAPP_RATING_SESSIONS_KEY, 0).then(timestamp => {
|
||||||
return new Date(timestamp);
|
return new Date(timestamp);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export async function increaseSessionCount(storageProvider: StorageProvider, increment = 1): Promise<number> {
|
public async increaseSessionCount(increment = 1): Promise<number> {
|
||||||
try {
|
try {
|
||||||
const currentSessions = await getInappRatingSessions(storageProvider);
|
const currentSessions = await this.getInappRatingSessions();
|
||||||
await putInappRatingSessions(storageProvider, currentSessions + increment);
|
await this.setInappRatingSessions(currentSessions + increment);
|
||||||
return currentSessions + increment;
|
return currentSessions + increment;
|
||||||
} catch {
|
} catch {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export async function startInappRatingIfFeasible(storageProvider: StorageProvider): Promise<boolean> {
|
public async startInappRatingIfFeasible(): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const currentSessions = await getInappRatingSessions(storageProvider);
|
const currentSessions = await this.getInappRatingSessions();
|
||||||
const lastRating = await getInappRatingLastRating(storageProvider);
|
const lastRating = await this.getInappRatingLastRating();
|
||||||
const dateDiffMillis = Math.abs(lastRating.getTime() - Date.now());
|
const dateDiffMillis = Math.abs(lastRating.getTime() - Date.now());
|
||||||
const diffDays = Math.ceil(dateDiffMillis / (1000 * 3600 * 24));
|
const diffDays = Math.ceil(dateDiffMillis / (1000 * 3600 * 24));
|
||||||
|
|
||||||
@@ -97,10 +107,11 @@ export async function startInappRatingIfFeasible(storageProvider: StorageProvide
|
|||||||
|
|
||||||
await InAppReview.requestReview();
|
await InAppReview.requestReview();
|
||||||
|
|
||||||
await putInappRatingLastRating(storageProvider, new Date());
|
await this.setInappRatingLastRating(new Date());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ import {
|
|||||||
IonToolbar,
|
IonToolbar,
|
||||||
} from '@ionic/angular/standalone';
|
} from '@ionic/angular/standalone';
|
||||||
import {IonIconDirective} from 'src/app/util/ion-icon/ion-icon.directive';
|
import {IonIconDirective} from 'src/app/util/ion-icon/ion-icon.directive';
|
||||||
|
import {InappRatingProvider} from './inapp-rating/inapp-rating.provider';
|
||||||
|
|
||||||
const settingsRoutes: Routes = [{path: 'settings', component: SettingsPageComponent}];
|
const settingsRoutes: Routes = [{path: 'settings', component: SettingsPageComponent}];
|
||||||
|
|
||||||
@@ -101,6 +102,13 @@ const settingsRoutes: Routes = [{path: 'settings', component: SettingsPageCompon
|
|||||||
IonInput,
|
IonInput,
|
||||||
IonNote,
|
IonNote,
|
||||||
],
|
],
|
||||||
providers: [ScheduleSyncService, SettingsProvider, CalendarService, ScheduleProvider, ThingTranslatePipe],
|
providers: [
|
||||||
|
ScheduleSyncService,
|
||||||
|
SettingsProvider,
|
||||||
|
InappRatingProvider,
|
||||||
|
CalendarService,
|
||||||
|
ScheduleProvider,
|
||||||
|
ThingTranslatePipe,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class SettingsModule {}
|
export class SettingsModule {}
|
||||||
|
|||||||
@@ -106,7 +106,6 @@ describe('StorageProvider', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should put multiple values into the storage', async () => {
|
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());
|
spyOn(storageProvider, 'put').and.callFake(() => Promise.resolve());
|
||||||
await storageProvider.putMultiple(sampleEntries);
|
await storageProvider.putMultiple(sampleEntries);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user