2021-03-12 11:56:54 +01:00
|
|
|
import { AfterViewChecked, Component, ElementRef, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core';
|
2021-03-18 09:25:59 +01:00
|
|
|
import { AnimationController, ModalController, NavParams, PopoverController } from '@ionic/angular';
|
2021-03-04 18:49:50 +01:00
|
|
|
import { Status } from 'src/app/models/chat/status.model';
|
2021-04-13 14:14:55 +01:00
|
|
|
import { AlertService } from 'src/app/services/alert.service';
|
2021-03-04 18:49:50 +01:00
|
|
|
import { AuthService } from 'src/app/services/auth.service';
|
|
|
|
|
import { ChatService } from 'src/app/services/chat.service';
|
|
|
|
|
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';
|
2021-05-27 16:21:04 +01:00
|
|
|
import { ProfileComponent } from '../../headers/header-no-search/profile/profile.page';
|
2021-03-12 11:56:54 +01:00
|
|
|
import { ContactsPage } from '../new-group/contacts/contacts.page';
|
2021-03-04 18:49:50 +01:00
|
|
|
|
|
|
|
|
@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, OnChanges {
|
2021-03-04 18:49:50 +01:00
|
|
|
showLoader: boolean;
|
|
|
|
|
|
|
|
|
|
@ViewChild('scrollMe') private myScrollContainer: ElementRef;
|
|
|
|
|
|
|
|
|
|
loggedUser: any;
|
|
|
|
|
|
|
|
|
|
message = '';
|
|
|
|
|
messages:any;
|
|
|
|
|
dm:any;
|
|
|
|
|
userPresence='';
|
|
|
|
|
dmUsers:any;
|
|
|
|
|
|
2021-03-12 11:56:54 +01:00
|
|
|
@Input() roomId:string;
|
2021-03-04 18:49:50 +01:00
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
public popoverController: PopoverController,
|
|
|
|
|
private modalController: ModalController,
|
2021-03-12 11:56:54 +01:00
|
|
|
/* private navParams: NavParams, */
|
2021-03-04 18:49:50 +01:00
|
|
|
private chatService: ChatService,
|
|
|
|
|
private authService: AuthService,
|
2021-03-18 09:25:59 +01:00
|
|
|
private animationController: AnimationController,
|
2021-04-13 14:14:55 +01:00
|
|
|
private alertService: AlertService,
|
2021-03-04 18:49:50 +01:00
|
|
|
) {
|
2021-03-12 11:56:54 +01:00
|
|
|
/* this.dm = this.navParams.get('dm'); */
|
|
|
|
|
}
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
console.log(this.roomId);
|
|
|
|
|
this.load();
|
|
|
|
|
|
|
|
|
|
//throw new Error('Method not implemented.');
|
2021-03-04 18:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
|
|
|
|
|
this.scrollToBottom();
|
|
|
|
|
|
|
|
|
|
this.authService.userData$.subscribe((res:any)=>{
|
|
|
|
|
this.loggedUser=res;
|
|
|
|
|
console.log(this.loggedUser);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* setInterval(()=>{ */
|
|
|
|
|
this.load();
|
|
|
|
|
/* }, 9000); */
|
2021-03-12 11:56:54 +01:00
|
|
|
console.log(this.roomId);
|
|
|
|
|
|
2021-03-04 18:49:50 +01:00
|
|
|
|
|
|
|
|
}
|
2021-04-13 14:14:55 +01:00
|
|
|
|
|
|
|
|
notImplemented(){
|
|
|
|
|
this.alertService.presentAlert('Funcionalidade em desenvolvimento');
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 18:49:50 +01:00
|
|
|
load(){
|
|
|
|
|
this.loadMessages();
|
|
|
|
|
this.getChatMembers();
|
|
|
|
|
}
|
2021-03-12 11:56:54 +01:00
|
|
|
|
|
|
|
|
|
2021-03-04 18:49:50 +01:00
|
|
|
doRefresh(ev:any){
|
|
|
|
|
this.load();
|
|
|
|
|
ev.target.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewChecked() {
|
|
|
|
|
this.scrollToBottom();
|
2021-03-12 11:56:54 +01:00
|
|
|
console.log(this.roomId);
|
2021-03-04 18:49:50 +01:00
|
|
|
}
|
|
|
|
|
scrollToBottom(): void {
|
|
|
|
|
try {
|
|
|
|
|
this.myScrollContainer.nativeElement.scrollTop = this.myScrollContainer.nativeElement.scrollHeight;
|
|
|
|
|
} catch(err) { }
|
|
|
|
|
}
|
|
|
|
|
loadMoreMessages(ev:any){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendMessage(){
|
|
|
|
|
|
|
|
|
|
let body = {
|
|
|
|
|
"message":
|
|
|
|
|
{
|
2021-03-12 11:56:54 +01:00
|
|
|
"rid": this.roomId, "msg": this.message
|
2021-03-04 18:49:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.chatService.sendMessage(body).subscribe(res=> {
|
|
|
|
|
this.loadMessages();
|
|
|
|
|
});
|
|
|
|
|
this.message = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadMessages(){
|
|
|
|
|
this.showLoader = true;
|
2021-03-12 11:56:54 +01:00
|
|
|
this.chatService.getRoomMessages(this.roomId).subscribe(res => {
|
2021-03-04 18:49:50 +01:00
|
|
|
/* console.log(res); */
|
|
|
|
|
this.messages = res['messages'].reverse();
|
|
|
|
|
console.log(this.messages);
|
|
|
|
|
this.showLoader = false;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
getChatMembers(){
|
2021-03-12 11:56:54 +01:00
|
|
|
console.log(this.roomId);
|
|
|
|
|
|
2021-03-04 18:49:50 +01:00
|
|
|
this.showLoader = true;
|
2021-03-12 11:56:54 +01:00
|
|
|
this.chatService.getMembers(this.roomId).subscribe(res=> {
|
2021-03-04 18:49:50 +01:00
|
|
|
this.dmUsers = res['members'].filter(data => data.username != this.loggedUser.me.username)
|
|
|
|
|
console.log(res);
|
|
|
|
|
console.log(this.dmUsers);
|
|
|
|
|
this.showLoader = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async openMessagesOptions(ev: any) {
|
|
|
|
|
const popover = await this.popoverController.create({
|
|
|
|
|
component: MessagesOptionsPage,
|
|
|
|
|
componentProps: {
|
|
|
|
|
roomId: this.dm._id,
|
|
|
|
|
},
|
|
|
|
|
cssClass: 'messages-options',
|
|
|
|
|
event: ev,
|
|
|
|
|
translucent: true,
|
|
|
|
|
});
|
|
|
|
|
return await popover.present();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addContacts(){
|
|
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
component: ContactsPage,
|
|
|
|
|
componentProps: {},
|
|
|
|
|
cssClass: 'contacts',
|
|
|
|
|
backdropDismiss: false
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await modal.present();
|
|
|
|
|
|
|
|
|
|
modal.onDidDismiss();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-03 14:56:53 +01:00
|
|
|
openSendMessageOptions(ev?:any){
|
2021-03-18 16:30:03 +01:00
|
|
|
if(window.innerWidth <= 1024){
|
|
|
|
|
console.log('mobile');
|
|
|
|
|
this.openChatOptions(ev);
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
console.log('desktop');
|
|
|
|
|
this._openChatOptions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 18:49:50 +01:00
|
|
|
async openChatOptions(ev: any) {
|
|
|
|
|
const popover = await this.popoverController.create({
|
|
|
|
|
component: ChatOptionsPopoverPage,
|
|
|
|
|
cssClass: 'chat-options-popover',
|
|
|
|
|
event: ev,
|
|
|
|
|
translucent: true
|
|
|
|
|
});
|
|
|
|
|
return await popover.present();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 09:25:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async _openMessagesOptions() {
|
|
|
|
|
|
|
|
|
|
const enterAnimation = (baseEl: any) => {
|
|
|
|
|
const backdropAnimation = this.animationController.create()
|
|
|
|
|
.addElement(baseEl.querySelector('ion-backdrop')!)
|
|
|
|
|
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');
|
|
|
|
|
|
|
|
|
|
const wrapperAnimation = this.animationController.create()
|
|
|
|
|
.addElement(baseEl.querySelector('.modal-wrapper')!)
|
|
|
|
|
.keyframes([
|
|
|
|
|
{ offset: 0, opacity: '1', right: '-100%' },
|
|
|
|
|
{ offset: 1, opacity: '1', right: '0px' }
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return this.animationController.create()
|
|
|
|
|
.addElement(baseEl)
|
|
|
|
|
.easing('ease-out')
|
|
|
|
|
.duration(500)
|
|
|
|
|
.addAnimation([backdropAnimation, wrapperAnimation]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const leaveAnimation = (baseEl: any) => {
|
|
|
|
|
return enterAnimation(baseEl).direction('reverse');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* const popover = await this.popoverController.create({
|
|
|
|
|
component: MessagesOptionsPage,
|
|
|
|
|
componentProps: {
|
|
|
|
|
roomId: this.dm._id,
|
|
|
|
|
},
|
|
|
|
|
cssClass: 'messages-options',
|
|
|
|
|
event: ev,
|
|
|
|
|
translucent: true,
|
|
|
|
|
});
|
|
|
|
|
return await popover.present(); */
|
|
|
|
|
|
|
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
enterAnimation,
|
|
|
|
|
leaveAnimation,
|
|
|
|
|
component: MessagesOptionsPage,
|
|
|
|
|
cssClass: 'model profile-modal search-submodal',
|
|
|
|
|
componentProps: {
|
|
|
|
|
roomId: this.roomId,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return await modal.present();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 16:30:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async _openChatOptions() {
|
|
|
|
|
|
|
|
|
|
const enterAnimation = (baseEl: any) => {
|
|
|
|
|
const backdropAnimation = this.animationController.create()
|
|
|
|
|
.addElement(baseEl.querySelector('ion-backdrop')!)
|
|
|
|
|
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');
|
|
|
|
|
|
|
|
|
|
const wrapperAnimation = this.animationController.create()
|
|
|
|
|
.addElement(baseEl.querySelector('.modal-wrapper')!)
|
|
|
|
|
.keyframes([
|
|
|
|
|
{ offset: 0, opacity: '1', right: '-100%' },
|
|
|
|
|
{ offset: 1, opacity: '1', right: '0px' }
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return this.animationController.create()
|
|
|
|
|
.addElement(baseEl)
|
|
|
|
|
.easing('ease-out')
|
|
|
|
|
.duration(500)
|
|
|
|
|
.addAnimation([backdropAnimation, wrapperAnimation]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const leaveAnimation = (baseEl: any) => {
|
|
|
|
|
return enterAnimation(baseEl).direction('reverse');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* const popover = await this.popoverController.create({
|
|
|
|
|
component: MessagesOptionsPage,
|
|
|
|
|
componentProps: {
|
|
|
|
|
roomId: this.dm._id,
|
|
|
|
|
},
|
|
|
|
|
cssClass: 'messages-options',
|
|
|
|
|
event: ev,
|
|
|
|
|
translucent: true,
|
|
|
|
|
});
|
|
|
|
|
return await popover.present(); */
|
|
|
|
|
|
|
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
enterAnimation,
|
|
|
|
|
leaveAnimation,
|
|
|
|
|
component: ChatOptionsPopoverPage,
|
|
|
|
|
cssClass: 'model profile-modal search-submodal',
|
|
|
|
|
componentProps: {
|
|
|
|
|
roomId: this.roomId,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return await modal.present();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-04 18:49:50 +01:00
|
|
|
}
|
2021-03-12 11:56:54 +01:00
|
|
|
|