diff --git a/projects/ucap-webmessenger-app/src/app/app-provider.module.ts b/projects/ucap-webmessenger-app/src/app/app-provider.module.ts index 42e71a9d..5dfda4ed 100644 --- a/projects/ucap-webmessenger-app/src/app/app-provider.module.ts +++ b/projects/ucap-webmessenger-app/src/app/app-provider.module.ts @@ -27,7 +27,7 @@ export function initializeApp(appService: AppService) { { provide: APP_INITIALIZER, useFactory: initializeApp, - deps: [AppService], + deps: [AppService, UCAP_NATIVE_SERVICE], multi: true } ] diff --git a/projects/ucap-webmessenger-app/src/app/app-translate.module.ts b/projects/ucap-webmessenger-app/src/app/app-translate.module.ts index 5faaf634..77a3fd70 100644 --- a/projects/ucap-webmessenger-app/src/app/app-translate.module.ts +++ b/projects/ucap-webmessenger-app/src/app/app-translate.module.ts @@ -6,16 +6,13 @@ import { UCAP_NATIVE_SERVICE, NativeService } from '@ucap-webmessenger/native'; export async function createTranslateLoader(nativeService: NativeService) { // tslint:disable-next-line: variable-name - let _TranslateLoader; - _TranslateLoader = await import('@ucap-webmessenger/native-browser').then( - m => m.TranslateBrowserLoader - ); - _TranslateLoader = await import('@ucap-webmessenger/native-electron').then( - m => m.TranslateElectronLoader + const translateLoader = nativeService.getTranslateLoader( + './assets/i18n/', + '.json' ); // return new TranslateBrowserLoader(nativeService, './assets/i18n/', '.json'); - return new _TranslateLoader(nativeService, './assets/i18n/', '.json'); + return translateLoader; } @NgModule({ diff --git a/projects/ucap-webmessenger-app/src/app/services/app.service.ts b/projects/ucap-webmessenger-app/src/app/services/app.service.ts index 77c8b0a0..2a2c16d5 100644 --- a/projects/ucap-webmessenger-app/src/app/services/app.service.ts +++ b/projects/ucap-webmessenger-app/src/app/services/app.service.ts @@ -1,16 +1,18 @@ -import { Injectable } from '@angular/core'; +import { Injectable, Inject } from '@angular/core'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { EnviromentUtilService } from '@ucap-webmessenger/util'; import { DeviceType } from '@ucap-webmessenger/core'; import { EnvironmentsInfo, KEY_ENVIRONMENTS_INFO } from '@app/types'; import { AppNotificationService } from './notification.service'; +import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native'; @Injectable() export class AppService { constructor( private enviromentUtilService: EnviromentUtilService, private sessionStorageService: SessionStorageService, - private appNotificationService: AppNotificationService + private appNotificationService: AppNotificationService, + @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService ) {} public postInit(): Promise { @@ -35,6 +37,7 @@ export class AppService { ); this.appNotificationService.subscribe(); + this.nativeService.postAppInit(); resolve(); } catch (error) { reject(); diff --git a/projects/ucap-webmessenger-app/src/environments/environment.type.ts b/projects/ucap-webmessenger-app/src/environments/environment.type.ts index 8a968260..68f23dd2 100644 --- a/projects/ucap-webmessenger-app/src/environments/environment.type.ts +++ b/projects/ucap-webmessenger-app/src/environments/environment.type.ts @@ -1,4 +1,4 @@ -import { Type } from '@angular/core'; +import { Type, ModuleWithProviders } from '@angular/core'; import { NativeService } from '@ucap-webmessenger/native'; export abstract class UrlConfig { diff --git a/projects/ucap-webmessenger-native-browser/src/lib/notification/notification.service.ts b/projects/ucap-webmessenger-native-browser/src/lib/notification/notification.service.ts new file mode 100644 index 00000000..0c40a4a2 --- /dev/null +++ b/projects/ucap-webmessenger-native-browser/src/lib/notification/notification.service.ts @@ -0,0 +1,35 @@ +import { NotificationRequest } from '@ucap-webmessenger/native'; + +export class NotificationService { + notificationPermission: NotificationPermission; + + constructor() { + this.notificationPermission = this.isSupported() ? 'default' : 'denied'; + } + + public isSupported(): boolean { + return 'Notification' in window; + } + + requestPermission(): void { + const self = this; + if ('Notification' in window) { + Notification.requestPermission().then(result => { + self.notificationPermission = result; + }); + } + } + + notify(noti: NotificationRequest) { + const notification = new Notification(noti.title, { body: noti.contents }); + notification.onclick = e => { + console.log('notification.onclick'); + }; + notification.onclose = e => { + console.log('notification.onclose'); + }; + notification.onerror = e => { + console.log('notification.onerror'); + }; + } +} diff --git a/projects/ucap-webmessenger-native-browser/src/lib/services/browser-native.service.ts b/projects/ucap-webmessenger-native-browser/src/lib/services/browser-native.service.ts index a3f1f900..5df23317 100644 --- a/projects/ucap-webmessenger-native-browser/src/lib/services/browser-native.service.ts +++ b/projects/ucap-webmessenger-native-browser/src/lib/services/browser-native.service.ts @@ -9,12 +9,24 @@ import { import { HttpClient } from '@angular/common/http'; import { map } from 'rxjs/operators'; import { Injectable } from '@angular/core'; +import { TranslateLoader } from '@ngx-translate/core'; +import { TranslateLoaderService } from '../translate/browser-loader'; +import { NotificationService } from '../notification/notification.service'; @Injectable({ providedIn: 'root' }) export class BrowserNativeService implements NativeService { - notify(noti: NotificationRequest): void {} + private notificationService: NotificationService; + + postAppInit(): void { + this.notificationService.requestPermission(); + } + + notify(noti: NotificationRequest): void { + console.log('ddd'); + this.notificationService.notify(noti); + } closeAllNotify(): void {} checkForUpdates(): Observable { @@ -77,5 +89,11 @@ export class BrowserNativeService implements NativeService { }); } - constructor(private httpClient: HttpClient) {} + getTranslateLoader(prefix?: string, suffix?: string): TranslateLoader { + return new TranslateLoaderService(this, prefix, suffix); + } + + constructor(private httpClient: HttpClient) { + this.notificationService = new NotificationService(); + } } diff --git a/projects/ucap-webmessenger-native-browser/src/lib/translate/browser-loader.ts b/projects/ucap-webmessenger-native-browser/src/lib/translate/browser-loader.ts index d2050273..3e5382f8 100644 --- a/projects/ucap-webmessenger-native-browser/src/lib/translate/browser-loader.ts +++ b/projects/ucap-webmessenger-native-browser/src/lib/translate/browser-loader.ts @@ -1,16 +1,11 @@ import { TranslateLoader } from '@ngx-translate/core'; import { Observable } from 'rxjs'; -import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native'; +import { NativeService } from '@ucap-webmessenger/native'; import { take, map } from 'rxjs/operators'; -import { FileUtil } from '@ucap-webmessenger/core'; -import { Injectable, Inject } from '@angular/core'; -@Injectable({ - providedIn: 'root' -}) -export class TranslateBrowserLoader implements TranslateLoader { +export class TranslateLoaderService implements TranslateLoader { constructor( - @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService, + private nativeService: NativeService, private prefix: string = '/assets/i18n/', private suffix: string = '.json' ) {} diff --git a/projects/ucap-webmessenger-native-electron/src/lib/services/electron-native.service.ts b/projects/ucap-webmessenger-native-electron/src/lib/services/electron-native.service.ts index a296bea9..c4e58732 100644 --- a/projects/ucap-webmessenger-native-electron/src/lib/services/electron-native.service.ts +++ b/projects/ucap-webmessenger-native-electron/src/lib/services/electron-native.service.ts @@ -17,6 +17,8 @@ import { IdleStateChannel } from '../types/channel.type'; import { Injectable } from '@angular/core'; +import { TranslateLoaderService } from '../translate/electron-loader'; +import { TranslateLoader } from '@ngx-translate/core'; @Injectable({ providedIn: 'root' @@ -28,6 +30,8 @@ export class ElectronNativeService implements NativeService { private idleStateChangedSubject: Subject | null = null; private idleStateChanged$: Observable | null = null; + postAppInit(): void {} + notify(noti: NotificationRequest): void { ipcRenderer.send(NotificationChannel.Notify, noti); } @@ -149,5 +153,9 @@ export class ElectronNativeService implements NativeService { return this.idleStateChanged$; } + getTranslateLoader(prefix?: string, suffix?: string): TranslateLoader { + return new TranslateLoaderService(this, prefix, suffix); + } + constructor() {} } diff --git a/projects/ucap-webmessenger-native-electron/src/lib/translate/electron-loader.ts b/projects/ucap-webmessenger-native-electron/src/lib/translate/electron-loader.ts index 35a1fb5c..92a89571 100644 --- a/projects/ucap-webmessenger-native-electron/src/lib/translate/electron-loader.ts +++ b/projects/ucap-webmessenger-native-electron/src/lib/translate/electron-loader.ts @@ -1,12 +1,11 @@ import { TranslateLoader } from '@ngx-translate/core'; import { Observable } from 'rxjs'; -import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native'; +import { NativeService } from '@ucap-webmessenger/native'; import { take, map } from 'rxjs/operators'; -import { Inject } from '@angular/core'; -export class TranslateElectronLoader implements TranslateLoader { +export class TranslateLoaderService implements TranslateLoader { constructor( - @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService, + private nativeService: NativeService, private prefix: string = '/assets/i18n/', private suffix: string = '.json' ) {} diff --git a/projects/ucap-webmessenger-native/src/lib/services/native.service.ts b/projects/ucap-webmessenger-native/src/lib/services/native.service.ts index 94ed535f..1a8c5d5f 100644 --- a/projects/ucap-webmessenger-native/src/lib/services/native.service.ts +++ b/projects/ucap-webmessenger-native/src/lib/services/native.service.ts @@ -3,8 +3,11 @@ import { Observable } from 'rxjs'; import { WindowState } from '../types/window-state.type'; import { WindowIdle } from '../types/window-idle.type'; import { NotificationRequest } from '../models/notification'; +import { TranslateLoader } from '@ngx-translate/core'; export interface NativeService { + postAppInit(): void; + notify(noti: NotificationRequest): void; closeAllNotify(): void; @@ -21,4 +24,6 @@ export interface NativeService { windowMaximize(): void; idleStateChanged(): Observable; + + getTranslateLoader(prefix?: string, suffix?: string): TranslateLoader; }