Files
doneit-web/src/app/pages/chat/messages/messages.page.ts
T

194 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-03-12 11:56:54 +01:00
import { AfterViewChecked, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
2021-07-27 00:21:30 +01:00
import {ActivatedRoute, Router} from '@angular/router'
2021-01-13 10:02:30 +01:00
import { ModalController, NavParams, PopoverController } from '@ionic/angular';
import { Status } from 'src/app/models/chat/status.model';
2021-03-12 11:56:54 +01:00
import { ContactsPage } from 'src/app/pages/chat/messages/contacts/contacts.page';
2021-04-13 14:14:55 +01:00
import { AlertService } from 'src/app/services/alert.service';
2021-01-13 10:02:30 +01:00
import { AuthService } from 'src/app/services/auth.service';
import { ChatService } from 'src/app/services/chat.service';
2021-07-12 11:13:29 +01:00
import { ToastService } from 'src/app/services/toast.service';
2020-12-30 11:13:50 +01:00
import { ChatOptionsPopoverPage } from 'src/app/shared/popover/chat-options-popover/chat-options-popover.page';
import { MessagesOptionsPage } from 'src/app/shared/popover/messages-options/messages-options.page';
@Component({
selector: 'app-messages',
templateUrl: './messages.page.html',
styleUrls: ['./messages.page.scss'],
})
2021-03-12 11:56:54 +01:00
export class MessagesPage implements OnInit, AfterViewChecked {
2021-01-27 16:01:49 +01:00
showLoader: boolean;
2021-01-13 10:02:30 +01:00
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
loggedUser: any;
2020-12-30 11:13:50 +01:00
message = '';
2021-01-13 10:02:30 +01:00
messages:any;
userPresence='';
dmUsers:any;
2021-03-12 11:56:54 +01:00
roomId:string;
2021-07-23 14:43:51 +01:00
el:any;
2021-01-13 10:02:30 +01:00
2020-12-30 11:13:50 +01:00
constructor(
public popoverController: PopoverController,
private modalController: ModalController,
2021-03-12 11:56:54 +01:00
private navParams: NavParams,
2021-01-13 10:02:30 +01:00
private chatService: ChatService,
private authService: AuthService,
2021-04-13 14:14:55 +01:00
private alertService: AlertService,
2021-07-12 11:13:29 +01:00
private toastService: ToastService,
2021-07-27 00:21:30 +01:00
private route: Router,
private activatedRoute: ActivatedRoute,
2021-07-23 14:43:51 +01:00
) {
2021-07-27 00:21:30 +01:00
/* this.activatedRoute.paramMap.subscribe(params => {
if(params["params"].SerialNumber) {
this.roomId = params["params"].roomId;
}
}); */
2021-07-23 14:43:51 +01:00
this.loggedUser = authService.ValidatedUserChat['data'];
2021-03-12 11:56:54 +01:00
this.roomId = this.navParams.get('roomId');
2021-01-13 10:02:30 +01:00
}
2020-12-30 11:13:50 +01:00
ngOnInit() {
2021-01-13 10:02:30 +01:00
this.scrollToBottom();
2021-01-20 10:23:59 +01:00
/* setInterval(()=>{ */
2021-01-27 16:01:49 +01:00
this.load();
/* }, 9000); */
2021-07-26 09:44:11 +01:00
/* this.el = document.getElementById("scrollToBottom");
this.el.scrollTop = this.el.scrollHeight - this.el.scrollTop; */
2021-07-23 14:43:51 +01:00
2021-01-13 10:02:30 +01:00
}
2021-04-13 14:14:55 +01:00
notImplemented(){
this.alertService.presentAlert('Funcionalidade em desenvolvimento');
}
2021-03-12 11:56:54 +01:00
close(){
this.modalController.dismiss();
}
2021-07-26 09:55:57 +01:00
2021-01-27 16:01:49 +01:00
load(){
2021-07-26 19:31:19 +01:00
this.serverLongPull();
2021-01-27 16:01:49 +01:00
this.getChatMembers();
2021-01-13 10:02:30 +01:00
}
2021-07-26 09:55:57 +01:00
2021-01-27 16:01:49 +01:00
doRefresh(ev:any){
this.load();
ev.target.complete();
}
2021-07-23 14:43:51 +01:00
ngAfterViewChecked() {
2021-07-26 20:52:03 +01:00
//this.scrollToBottom();
2021-07-23 14:43:51 +01:00
}
2021-07-12 11:13:29 +01:00
2021-07-23 14:43:51 +01:00
scrollToBottom(): void {
2021-01-13 10:02:30 +01:00
try {
2021-07-12 11:13:29 +01:00
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
2021-07-23 14:43:51 +01:00
} catch(err) { }
2021-01-13 10:02:30 +01:00
}
sendMessage(){
let body = {
2021-07-23 14:43:51 +01:00
"message":
{
"rid": this.roomId, "msg": this.message
2021-01-13 10:02:30 +01:00
}
}
this.chatService.sendMessage(body).subscribe(res=> {
2021-07-23 14:43:51 +01:00
this.loadMessages();
2021-01-13 10:02:30 +01:00
});
2021-01-13 11:05:21 +01:00
this.message = "";
2021-01-13 10:02:30 +01:00
}
2021-07-23 14:43:51 +01:00
2021-01-13 10:02:30 +01:00
loadMessages(){
2021-01-27 16:01:49 +01:00
this.showLoader = true;
2021-03-11 16:21:09 +01:00
this.chatService.getRoomMessages(this.roomId).subscribe(res => {
/* console.log(res); */
2021-01-13 10:02:30 +01:00
this.messages = res['messages'].reverse();
console.log(this.messages);
2021-02-11 10:40:12 +01:00
this.showLoader = false;
2021-01-13 10:02:30 +01:00
})
}
getChatMembers(){
2021-01-27 16:01:49 +01:00
this.showLoader = true;
2021-03-11 16:21:09 +01:00
this.chatService.getMembers(this.roomId).subscribe(res=> {
2021-01-13 10:02:30 +01:00
this.dmUsers = res['members'].filter(data => data.username != this.loggedUser.me.username)
console.log(res);
console.log(this.dmUsers);
2021-01-27 16:01:49 +01:00
this.showLoader = false;
2021-01-13 10:02:30 +01:00
});
2020-12-30 11:13:50 +01:00
}
2021-06-03 17:07:29 +01:00
async openMessagesOptions(ev?: any) {
2020-12-30 11:13:50 +01:00
const popover = await this.popoverController.create({
component: MessagesOptionsPage,
2021-01-13 10:02:30 +01:00
componentProps: {
2021-03-12 11:56:54 +01:00
roomId: this.roomId,
2021-01-13 10:02:30 +01:00
},
2020-12-30 11:13:50 +01:00
cssClass: 'messages-options',
event: ev,
2021-01-13 10:02:30 +01:00
translucent: true,
2020-12-30 11:13:50 +01:00
});
return await popover.present();
}
async addContacts(){
const modal = await this.modalController.create({
component: ContactsPage,
2021-07-23 14:43:51 +01:00
componentProps: {},
2020-12-30 11:13:50 +01:00
cssClass: 'contacts',
backdropDismiss: false
});
await modal.present();
modal.onDidDismiss();
}
2021-06-03 17:07:29 +01:00
async openChatOptions(ev?: any) {
2020-12-30 11:13:50 +01:00
const popover = await this.popoverController.create({
component: ChatOptionsPopoverPage,
cssClass: 'chat-options-popover',
event: ev,
translucent: true
});
return await popover.present();
}
2021-07-26 19:31:19 +01:00
async serverLongPull(){
this.chatService.getRoomMessages(this.roomId).subscribe(async res => {
if (res == 502) {
// Connection timeout
// happens when the connection was pending for too long
// let's reconnect
await this.serverLongPull();
} else if (res != 200) {
// Show Error
//showMessage(response.statusText);
//this.loadMessages()
this.messages = res['messages'].reverse();
console.log(this.messages);
// Reconnect in one second
if(this.route.url != "/home/chat"){
console.log("Timer message stop")
} else {
2021-08-17 14:17:19 +01:00
await new Promise(resolve => setTimeout(resolve, 1000));
2021-07-26 19:31:19 +01:00
await this.serverLongPull();
console.log('Timer message running')
}
2021-07-26 20:52:03 +01:00
2021-07-26 19:31:19 +01:00
} else {
// Got message
//let message = await response.text();
//this.loadMessages()
await this.serverLongPull();
}
});
}
2021-07-23 14:43:51 +01:00
}