2020 SOFTWARE.
2121 */
2222import {
23- Component , ElementRef , EventEmitter , Input , Renderer2 , ViewChild , Output , AfterViewInit ,
24- OnDestroy
23+ Component , ElementRef , EventEmitter , Input , Renderer2 , ViewChild , Output ,
24+ OnDestroy , OnInit , ChangeDetectorRef
2525} from '@angular/core' ;
26- import { ChatStatus } from './interfaces/chat-status.interface' ;
27- import { ChatContact } from './interfaces/chat-contact.interface' ;
28- import { ChatMessage } from './interfaces/chat-message.interface' ;
29- import { Status } from './enums/status.enum' ;
30- import { Subscription } from 'rxjs' ;
31- import { ChatService } from './services/chat.service' ;
32- import { I18nService } from '../i18n/i18n.service' ;
26+ import { ChatStatus } from './interfaces/chat-status.interface' ;
27+ import { ChatContact } from './interfaces/chat-contact.interface' ;
28+ import { ChatMessage } from './interfaces/chat-message.interface' ;
29+ import { Status } from './enums/status.enum' ;
30+ import { Subscription } from 'rxjs' ;
31+ import { ChatService } from './services/chat.service' ;
32+ import { I18nService } from '../i18n/i18n.service' ;
3333
3434let uniqueIdentifier = 0 ;
3535
36- @Component ( {
36+ @Component ( {
3737 selector : 'tl-chatlist' ,
3838 templateUrl : './chatlist.html' ,
39- styleUrls : [ './chatlist.scss' ] ,
40- } )
41- export class TlChatList implements AfterViewInit , OnDestroy {
39+ styleUrls : [ './chatlist.scss' ] ,
40+ } )
41+ export class TlChatList implements OnInit , OnDestroy {
4242
4343 @Input ( ) maxHeight = '450px' ;
4444
@@ -61,7 +61,7 @@ export class TlChatList implements AfterViewInit, OnDestroy {
6161 @Input ( ) user : ChatContact ;
6262
6363 @Input ( 'contacts' )
64- set contacts ( data : ChatContact [ ] ) {
64+ set contacts ( data : ChatContact [ ] ) {
6565 if ( data && data . length > 0 && this . user ) {
6666 if ( ! this . user . id ) {
6767 throw Error ( 'User id not found' ) ;
@@ -74,15 +74,15 @@ export class TlChatList implements AfterViewInit, OnDestroy {
7474 return this . _dataSource ;
7575 }
7676
77+ @Output ( ) readMessage : EventEmitter < ChatMessage [ ] > = new EventEmitter ( ) ;
78+
7779 @Output ( ) sendMessage : EventEmitter < ChatMessage > = new EventEmitter ( ) ;
7880
7981 @Output ( ) changeStatus : EventEmitter < any > = new EventEmitter ( ) ;
8082
81- @Output ( ) newMessages : EventEmitter < boolean > = new EventEmitter ( ) ;
82-
8383 @Output ( ) selectContact : EventEmitter < any > = new EventEmitter ( ) ;
8484
85- @ViewChild ( 'content' , { static : false } ) content : ElementRef ;
85+ @ViewChild ( 'content' , { static : false } ) content : ElementRef ;
8686
8787 public transform = '0' ;
8888
@@ -96,13 +96,17 @@ export class TlChatList implements AfterViewInit, OnDestroy {
9696
9797 public filterControl = '' ;
9898
99- public messages : ChatMessage [ ] = [ ] ;
100-
10199 private _dataSource ;
102100
101+ public messages = [ ] ;
102+
103103 private subscription = new Subscription ( ) ;
104104
105- constructor ( private renderer : Renderer2 , private chatService : ChatService , private i18nService : I18nService ) { }
105+ constructor ( private renderer : Renderer2 ,
106+ private change : ChangeDetectorRef ,
107+ private chatService : ChatService ,
108+ private i18nService : I18nService ) {
109+ }
106110
107111 get online ( ) {
108112 return Status . ONLINE ;
@@ -116,8 +120,25 @@ export class TlChatList implements AfterViewInit, OnDestroy {
116120 return Status . BUSY ;
117121 }
118122
119- ngAfterViewInit ( ) {
120- this . chatService . chat = this ;
123+ ngOnInit ( ) {
124+ this . listenChangeStatus ( ) ;
125+ this . listenChangeMessages ( ) ;
126+ this . messages = this . chatService . getAllMessages ( this . id ) ;
127+ }
128+
129+ listenChangeStatus ( ) {
130+ this . subscription . add ( this . chatService . changeStatus . subscribe ( ( value : { chatId : string , status : Status } ) => {
131+ if ( value . chatId === this . id ) {
132+ this . setStatus ( value . status ) ;
133+ }
134+ } ) ) ;
135+ }
136+
137+ listenChangeMessages ( ) {
138+ this . subscription . add ( this . chatService . allMessages . subscribe ( ( messages : ChatMessage [ ] ) => {
139+ this . messages = messages ;
140+ this . change . detectChanges ( ) ;
141+ } ) ) ;
121142 }
122143
123144 animationContentDone ( event : AnimationEvent ) {
@@ -127,89 +148,52 @@ export class TlChatList implements AfterViewInit, OnDestroy {
127148 }
128149
129150 getUnreadMessages ( item : ChatContact ) {
130- if ( ! item || ! item . id ) {
131- return [ ] ;
132- }
133151 if ( this . messages . length > 0 ) {
134152 return this . messages . filter ( ( message : ChatMessage ) => {
135- if ( message . from && message . to ) {
136- return ( ! message . viewed && message . from . id === item . id ) && ( message . to . id === this . user . id ) ;
153+ if ( message . from && message . to ) {
154+ return ( ! message . viewed && message . from . id === item . id ) && ( message . to . id === this . user . id ) ;
137155 }
138156 } ) ;
139157 }
140158 return [ ] ;
141159 }
142160
143- getFilter ( statusSelected ) {
161+ getFilter ( statusSelected ) {
144162 if ( statusSelected === this . offline ) {
145- return { filter : this . filterControl , status : [ this . offline ] } ;
163+ return { filter : this . filterControl , status : [ this . offline ] } ;
146164 }
147- return { filter : this . filterControl , status : [ this . online , this . busy ] } ;
165+ return { filter : this . filterControl , status : [ this . online , this . busy ] } ;
148166 }
149167
150168 trackByFn ( index ) {
151169 return index ;
152170 }
153171
154- hasMessages ( ) {
155- const messages = this . messages . filter ( ( value ) => {
156- if ( value . to && this . user ) {
157- return ! value . viewed && ( value . to . id === this . user . id ) ;
158- }
159- } ) ;
160- return messages . length > 0 ;
161- }
162-
163172 selectPartner ( item : ChatContact ) {
164173 this . updatePartner ( item ) ;
165- this . selectContact . emit ( { ...item , unreadMessages : this . getUnreadMessages ( item ) } ) ;
174+ this . selectContact . emit ( { ...item , unreadMessages : this . getUnreadMessages ( item ) } ) ;
166175 this . renderer . setStyle ( this . content . nativeElement , 'animation' , 'showOffContent 0.2s forwards' ) ;
167- this . readMessages ( this . getUnreadMessages ( item ) ) ;
168- this . newMessages . emit ( this . hasMessages ( ) ) ;
169- }
170-
171- readMessages ( messages : ChatMessage [ ] ) {
172- this . messages . forEach ( ( item ) => {
173- messages . forEach ( ( value ) => {
174- if ( JSON . stringify ( item ) === JSON . stringify ( value ) ) {
175- item . viewed = true ;
176- }
177- } ) ;
178- } ) ;
179- }
180-
181- readAllMessages ( ) {
182- this . messages . forEach ( ( item : ChatMessage , index , array ) => item . viewed = true ) ;
176+ this . chatService . readMessages ( this . getUnreadMessages ( item ) , this . user , this . id ) ;
183177 }
184178
185179 updatePartner ( item : ChatContact ) {
186180 this . partner = item ;
187181 }
188182
189- loadMessages ( messages : ChatMessage [ ] ) {
190- this . messages = messages ;
191- this . newMessages . emit ( this . hasMessages ( ) ) ;
192- }
193-
194- appendMessage ( message : ChatMessage ) {
195- this . messages = [ ...this . messages , message ] ;
196- this . newMessages . emit ( this . hasMessages ( ) ) ;
197- }
198-
199183 setStatus ( status : Status ) {
200- this . changeStatus . emit ( { user : this . user , status : status } ) ;
184+ this . changeStatus . emit ( { user : this . user , status : status } ) ;
201185 }
202186
203187 onMessage ( message : { value : string , time : Date } ) {
204188 const msm = {
189+ id : String ( new Date ( ) . getTime ( ) ) ,
205190 to : this . partner ,
206191 from : this . user ,
207192 message : message . value ,
208193 time : message . time ,
209194 viewed : false
210195 } ;
211196 this . sendMessage . emit ( msm ) ;
212- this . appendMessage ( msm ) ;
213197 }
214198
215199 selectStatus ( status ) {
@@ -224,7 +208,6 @@ export class TlChatList implements AfterViewInit, OnDestroy {
224208
225209 ngOnDestroy ( ) {
226210 this . subscription . unsubscribe ( ) ;
227- this . chatService . removeChat ( this . id ) ;
228211 }
229212
230213}
0 commit comments