import { map, tap, take, catchError } from 'rxjs/operators'; import { Component, OnInit, Inject, OnDestroy, ViewChild, ChangeDetectorRef } from '@angular/core'; import { Store, select } from '@ngrx/store'; import * as AppStore from '@app/store'; import * as ChatStore from '@app/store/messenger/chat'; import { Observable, Subscription, of } from 'rxjs'; import { UCAP_NATIVE_SERVICE, NativeService } from '@ucap-webmessenger/native'; import { UserInfo } from '@ucap-webmessenger/protocol-sync'; import { UserInfoSS, UserInfoF, UserInfoDN, QueryProtocolService } from '@ucap-webmessenger/protocol-query'; import { StatusProtocolService } from '@ucap-webmessenger/protocol-status'; import { StatusType, StatusCode } from '@ucap-webmessenger/core'; import { DialogService, ConfirmDialogComponent, ConfirmDialogResult, AlertDialogComponent, AlertDialogResult, AlertDialogData, ConfirmDialogData } from '@ucap-webmessenger/ui'; import { ProfileDialogComponent, ProfileDialogData, ProfileDialogResult } from '@app/layouts/messenger/dialogs/profile/profile.dialog.component'; import { MatDrawer } from '@angular/material'; import { NGXLogger } from 'ngx-logger'; import { LoginResponse } from '@ucap-webmessenger/protocol-authentication'; import { DaesangProtocolService } from '@ucap-webmessenger/daesang'; import { CallService } from '@ucap-webmessenger/api-prompt'; import { EnvironmentsInfo, KEY_ENVIRONMENTS_INFO } from '@app/types'; import { SessionStorageService } from '@ucap-webmessenger/web-storage'; import { MessageApiService, MessageType, DetailRequest } from '@ucap-webmessenger/api-message'; import { MessageStatusCode } from '@ucap-webmessenger/api'; import { MessageDetailDialogComponent, MessageDetailDialogResult, MessageDetailDialogData } from '@app/layouts/messenger/dialogs/message/message-detail.dialog.component'; @Component({ selector: 'app-page-messenger-main', templateUrl: './main.page.component.html', styleUrls: ['./main.page.component.scss'] }) export class MainPageComponent implements OnInit, OnDestroy { selectedChat$: Observable; selectedRightDrawer$: Observable; idleStateChangedSubscription: Subscription; chatOpenRoomSubscription: Subscription; msgOpenMessageSubscription: Subscription; defaultLeftSideComponentWidth = 380; leftSideComponentWidth = this.defaultLeftSideComponentWidth; loginRes: LoginResponse; loginResSubscription: Subscription; environmentsInfo: EnvironmentsInfo; @ViewChild('rightDrawer', { static: true }) rightDrawer: MatDrawer; constructor( @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService, private store: Store, private statusProtocolService: StatusProtocolService, private messageApiService: MessageApiService, private daesangProtocolService: DaesangProtocolService, private callService: CallService, private sessionStorageService: SessionStorageService, private dialogService: DialogService, private logger: NGXLogger, private changeDetectorRef: ChangeDetectorRef ) { this.environmentsInfo = this.sessionStorageService.get( KEY_ENVIRONMENTS_INFO ); } ngOnInit(): void { this.selectedChat$ = this.store.pipe( select(AppStore.MessengerSelector.ChatSelector.selectedRoom), tap(selectedRoom => { if (!selectedRoom) { this.rightDrawer.close(); } return selectedRoom; }) ); this.selectedRightDrawer$ = this.store.pipe( select(AppStore.MessengerSelector.ChatSelector.selectedRightDrawer), tap(selectedRightDrawer => { if (!!selectedRightDrawer) { this.rightDrawer.open(); } else { this.rightDrawer.close(); } }) ); this.idleStateChangedSubscription = this.nativeService .idleStateChanged() .subscribe(action => { this.logger.debug(action); let statusType: StatusCode; if (action === 'IDLE') { // away statusType = StatusCode.Away; } else { // online statusType = StatusCode.OnLine; } this.statusProtocolService.status({ statusDivisionType: StatusType.Messenger, statusType, statusMessage: '' }); }); this.chatOpenRoomSubscription = this.nativeService .chatOpenRoom() .subscribe(roomSeq => { this.store.dispatch(ChatStore.selectedRoom({ roomSeq })); }); this.msgOpenMessageSubscription = this.nativeService .msgOpenMessage() .subscribe(messageSeq => { console.log(messageSeq); // this.messageApiService // .detailMessage({ // userSeq: this.loginRes.userSeq, // deviceType: this.environmentsInfo.deviceType, // tokenKey: this.loginRes.tokenString, // type: MessageType.Receive, // msgId: Number(messageSeq) // } as DetailRequest) // .pipe( // take(1), // map(res => { // if (res.responseCode === MessageStatusCode.Success) { // // detail view.. // this.dialogService.open< // MessageDetailDialogComponent, // MessageDetailDialogData, // MessageDetailDialogResult // >(MessageDetailDialogComponent, { // width: '600px', // data: { // detail: res, // loginRes: this.loginRes, // environmentsInfo: this.environmentsInfo // } // }); // // this.changeDetectorRef.detectChanges(); // } else { // } // }), // catchError(error => of(this.logger.error(error))) // ) // .subscribe(); }); this.loginResSubscription = this.store .pipe( select(AppStore.AccountSelector.AuthenticationSelector.loginRes), tap(loginRes => { this.loginRes = loginRes; }) ) .subscribe(); } ngOnDestroy(): void { if (!!this.idleStateChangedSubscription) { this.idleStateChangedSubscription.unsubscribe(); } if (!!this.chatOpenRoomSubscription) { this.chatOpenRoomSubscription.unsubscribe(); } if (!!this.loginResSubscription) { this.loginResSubscription.unsubscribe(); } this.logger.debug('-----------------------MainPageComponent ngOnDestroy'); } onOpenedChange(event: boolean) { if (!event) { this.store.dispatch( ChatStore.selectedRightDrawer({ req: null }) ); } } onGutterDragEnd(e: { gutterNum: number; sizes: Array }) { this.leftSideComponentWidth = e.sizes[0]; } onGutterDblClick(e: { gutterNum: number; sizes: Array }) { this.leftSideComponentWidth = this.defaultLeftSideComponentWidth; } onClickOpenProfile(userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN) { // [GROUP] // this.queryProtocolService // .dataUser({ // divCd: 'OPENPROF', // seq: userInfo.seq, // senderCompanyCode: this.loginRes.userInfo.companyCode, // senderEmployeeType: this.loginRes.userInfo.employeeType // }) // .pipe( // take(1), // map(res => { // if (!!res && !!res.userInfo) { // this.dialogService.open< // ProfileDialogComponent, // ProfileDialogData, // ProfileDialogResult // >(ProfileDialogComponent, { // data: { // userInfo: res.userInfo // } // }); // } // }) // ) // .subscribe(); // [Daesang] this.daesangProtocolService .dataUserDaesang({ divCd: 'OPENPROF', seq: userInfo.seq, senderCompanyCode: this.loginRes.userInfo.companyCode, senderEmployeeType: this.loginRes.userInfo.employeeType }) .pipe( take(1), map(res => { if (!!res && !!res.userInfo) { this.dialogService.open< ProfileDialogComponent, ProfileDialogData, ProfileDialogResult >(ProfileDialogComponent, { data: { userInfo: res.userInfo } }); } }) ) .subscribe(); } async onClickSendClickToCall(calleeNumber: string) { const madn = this.loginRes.madn; if (!madn || madn.trim().length === 0) { this.dialogService.open< AlertDialogComponent, AlertDialogData, AlertDialogResult >(AlertDialogComponent, { data: { title: '', html: `전화를 걸 수 없는 사용자 입니다.` } }); return false; } calleeNumber = calleeNumber.replace(/\D/g, ''); if (!!calleeNumber && calleeNumber.length > 0) { const result = await this.dialogService.open< ConfirmDialogComponent, ConfirmDialogData, ConfirmDialogResult >(ConfirmDialogComponent, { width: '360px', data: { title: '전화 걸기', html: `${calleeNumber}로 전화를 거시겠습니까?` } }); if (!!result && !!result.choice && result.choice) { this.callService .sendCall({ userSeq: this.loginRes.userSeq, deviceType: this.environmentsInfo.deviceType, tokenKey: this.loginRes.tokenString, calleeNumber }) .pipe( take(1), map(res => { console.log(res); }), catchError(error => of(this.logger.debug(error))) ) .subscribe(); } } else { this.dialogService.open< AlertDialogComponent, AlertDialogData, AlertDialogResult >(AlertDialogComponent, { data: { title: '', html: `상대방 번호가 없어 전화를 걸 수 없습니다.` } }); } } onCloseRightDrawer() { this.rightDrawer.close(); } }