116 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Observable, Subject } from 'rxjs';
2019-11-01 11:25:54 +09:00
import {
NativeService,
WindowState,
2019-11-09 17:29:02 +09:00
NotificationRequest,
WindowIdle,
2019-11-01 11:25:54 +09:00
} from '@ucap-webmessenger/native';
import { HttpClient } from '@angular/common/http';
import { map, share } from 'rxjs/operators';
2019-11-11 15:53:39 +09:00
import { Injectable } from '@angular/core';
2019-11-11 18:09:47 +09:00
import { TranslateLoader } from '@ngx-translate/core';
import { TranslateLoaderService } from '../translate/browser-loader';
import { NotificationService } from '../notification/notification.service';
2019-11-11 15:53:39 +09:00
@Injectable({
providedIn: 'root',
2019-11-11 15:53:39 +09:00
})
export class BrowserNativeService implements NativeService {
2019-11-11 18:09:47 +09:00
private notificationService: NotificationService;
private chatOpenRoomSubject: Subject<string> | null = null;
private chatOpenRoom$: Observable<string> | null = null;
2019-11-11 18:09:47 +09:00
postAppInit(): void {
this.notificationService.requestPermission();
}
notify(noti: NotificationRequest): void {
this.notificationService.notify(noti, () => {
window.focus();
this.chatOpenRoomSubject.next(noti.roomSeq);
});
2019-11-11 18:09:47 +09:00
}
2019-11-09 17:29:02 +09:00
closeAllNotify(): void {}
checkForUpdates(): Observable<boolean> {
return new Observable<boolean>(subscriber => {
try {
subscriber.next(false);
} catch (error) {
subscriber.error(error);
} finally {
subscriber.complete();
}
});
}
showImageViewer(): void {}
2019-11-07 11:37:33 +09:00
readFile(path: string): Observable<Buffer> {
return this.httpClient.get(path, { responseType: 'arraybuffer' }).pipe(
map(arrayBuffer => {
return Buffer.from(arrayBuffer);
})
);
}
2019-11-07 11:37:33 +09:00
saveFile(
buffer: Buffer,
fileName: string,
path?: string
): Observable<string> {
return this.httpClient.post<string>(path, buffer, {});
}
windowStateChanged(): Observable<WindowState> {
return new Observable<WindowState>(subscriber => {
try {
subscriber.next(WindowState.Normal);
} catch (error) {
subscriber.error(error);
} finally {
subscriber.complete();
}
});
}
windowClose(): void {}
windowMinimize(): void {}
windowMaximize(): void {}
idleStateChanged(): Observable<WindowIdle> {
return new Observable<WindowIdle>(subscriber => {
try {
subscriber.next(WindowIdle.Active);
} catch (error) {
subscriber.error(error);
} finally {
subscriber.complete();
}
});
}
chatOpenRoom(): Observable<string> {
if (!this.chatOpenRoomSubject) {
this.chatOpenRoomSubject = new Subject<WindowIdle>();
this.chatOpenRoom$ = this.chatOpenRoomSubject
.asObservable()
.pipe(share());
}
return this.chatOpenRoom$;
}
2019-11-11 18:09:47 +09:00
getTranslateLoader(prefix?: string, suffix?: string): TranslateLoader {
return new TranslateLoaderService(this, prefix, suffix);
}
constructor(private httpClient: HttpClient) {
this.notificationService = new NotificationService();
}
}