-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PeerChat): IsTyping indicator (#1575)
Co-authored-by: Jonathan Lim-Breitbart <[email protected]>
- Loading branch information
1 parent
ba8c7a4
commit 64ae2cf
Showing
11 changed files
with
242 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
src/assets/wise5/components/peerChat/peer-chat-chat-box/peer-chat-chat-box.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
.mat-mdc-card { | ||
padding: 12px; | ||
padding: 16px 16px 20px; | ||
} | ||
|
||
peer-chat-messages, peer-chat-members { | ||
margin-bottom: 12px; | ||
display: block; | ||
} | ||
|
||
peer-chat-member-typing-indicator { | ||
padding: 0 8px; | ||
position: absolute; | ||
bottom: 2px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/assets/wise5/components/peerChat/peer-chat-chat-box/peer-chat-chat-box.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...erChat/peer-chat-member-typing-indicator/peer-chat-member-typing-indicator.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ message }} |
36 changes: 36 additions & 0 deletions
36
...hat/peer-chat-member-typing-indicator/peer-chat-member-typing-indicator.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { PeerChatMemberTypingIndicatorComponent } from './peer-chat-member-typing-indicator.component'; | ||
import { ConfigService } from '../../../services/configService'; | ||
import { StompService } from '../../../services/stompService'; | ||
import { PeerGroup } from '../PeerGroup'; | ||
import { StudentDataService } from '../../../services/studentDataService'; | ||
import { Subject } from 'rxjs'; | ||
|
||
class MockConfigService {} | ||
class MockStudentDataService { | ||
private studentWorkReceivedSource: Subject<any> = new Subject<any>(); | ||
public studentWorkReceived$ = this.studentWorkReceivedSource.asObservable(); | ||
} | ||
describe('PeerChatMemberTypingIndicatorComponent', () => { | ||
let component: PeerChatMemberTypingIndicatorComponent; | ||
let fixture: ComponentFixture<PeerChatMemberTypingIndicatorComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [PeerChatMemberTypingIndicatorComponent], | ||
providers: [ | ||
{ provide: ConfigService, useClass: MockConfigService }, | ||
StompService, | ||
{ provide: StudentDataService, useClass: MockStudentDataService } | ||
] | ||
}); | ||
fixture = TestBed.createComponent(PeerChatMemberTypingIndicatorComponent); | ||
component = fixture.componentInstance; | ||
component.peerGroup = new PeerGroup(1, [], null); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
109 changes: 109 additions & 0 deletions
109
...peerChat/peer-chat-member-typing-indicator/peer-chat-member-typing-indicator.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { StompService } from '../../../services/stompService'; | ||
import { PeerChatComponent } from '../PeerChatComponent'; | ||
import { PeerGroup } from '../PeerGroup'; | ||
import { ConfigService } from '../../../services/configService'; | ||
import { Subscription } from 'rxjs'; | ||
import { StudentDataService } from '../../../services/studentDataService'; | ||
|
||
@Component({ | ||
selector: 'peer-chat-member-typing-indicator', | ||
templateUrl: './peer-chat-member-typing-indicator.component.html' | ||
}) | ||
export class PeerChatMemberTypingIndicatorComponent { | ||
@Input() component: PeerChatComponent; | ||
private intervalId: NodeJS.Timeout; | ||
protected message: string; | ||
@Input() myWorkgroupId: number; | ||
@Input() peerGroup: PeerGroup; | ||
private subscriptions: Subscription = new Subscription(); | ||
private typingDurationBuffer: number = 5000; | ||
private workgroupToLastTypingTimestamp: Map<number, number> = new Map<number, number>(); | ||
|
||
constructor( | ||
private configService: ConfigService, | ||
private dataService: StudentDataService, | ||
private stompService: StompService | ||
) {} | ||
|
||
ngOnInit(): void { | ||
this.intervalId = setInterval(() => { | ||
this.updateMessage(); | ||
}, 1000); | ||
this.subscribeToIsTypingMessages(); | ||
this.subscribeToStudentWork(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
clearInterval(this.intervalId); | ||
this.subscriptions.unsubscribe(); | ||
} | ||
|
||
private updateMessage(): void { | ||
const workgroupsTyping = []; | ||
this.workgroupToLastTypingTimestamp.forEach((lastTypingTimestamp, workgroupId) => { | ||
if (new Date().getTime() - lastTypingTimestamp < this.typingDurationBuffer) { | ||
workgroupsTyping.push(workgroupId); | ||
} | ||
}); | ||
if (workgroupsTyping.length === 0) { | ||
this.message = ''; | ||
} else { | ||
const classmateNames = workgroupsTyping | ||
.map((workgroupId) => | ||
this.configService.getStudentFirstNamesByWorkgroupId(workgroupId).join(', ') | ||
) | ||
.join(', '); | ||
this.message = classmateNames.includes(',') | ||
? $localize`${classmateNames} are typing...` | ||
: $localize`${classmateNames} is typing...`; | ||
} | ||
} | ||
|
||
private subscribeToIsTypingMessages(): void { | ||
this.subscriptions.add( | ||
this.stompService.rxStomp | ||
.watch(`/topic/peer-group/${this.peerGroup.id}/is-typing`) | ||
.subscribe(({ body }) => { | ||
const { nodeId, componentId, workgroupId } = JSON.parse(JSON.parse(body).content); | ||
if ( | ||
nodeId === this.component.nodeId && | ||
componentId === this.component.id && | ||
workgroupId !== this.myWorkgroupId | ||
) { | ||
this.workgroupToLastTypingTimestamp.set(workgroupId, new Date().getTime()); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
private subscribeToStudentWork(): void { | ||
this.subscriptions.add( | ||
this.dataService.studentWorkReceived$.subscribe((componentState) => { | ||
if (this.isMessageFromPeer(componentState)) { | ||
this.workgroupToLastTypingTimestamp.delete(componentState.workgroupId); | ||
this.updateMessage(); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
private isMessageFromPeer(componentState: any): boolean { | ||
return ( | ||
this.isForThisComponent(componentState) && | ||
this.isFromClassmate(componentState) && | ||
componentState.peerGroupId === this.peerGroup.id | ||
); | ||
} | ||
|
||
private isForThisComponent(componentState: any): boolean { | ||
return ( | ||
componentState.nodeId === this.component.nodeId && | ||
componentState.componentId === this.component.id | ||
); | ||
} | ||
|
||
private isFromClassmate(componentState: any): boolean { | ||
return componentState.workgroupId !== this.myWorkgroupId; | ||
} | ||
} |
34 changes: 33 additions & 1 deletion
34
...ts/wise5/components/peerChat/peer-chat-message-input/peer-chat-message-input.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.