src/pages/lista-conversazioni/lista-conversazioni.ts
| selector | page-lista-conversazioni |
| templateUrl | lista-conversazioni.html |
Properties |
|
Methods |
constructor(popoverCtrl: PopoverController, modalCtrl: ModalController, navCtrl: NavController, navParams: NavParams, navProxy: NavProxyService, userService: UserService, chatConversationsHandler: ChatConversationsHandler, events: Events, chatManager: ChatManager, databaseProvider: DatabaseProvider)
|
||||||||||||||||||||||||||||||||||||||||||||
|
Parameters :
|
| goToconversationMessageList | ||||||||
goToconversationMessageList(uid: )
|
||||||||
|
metodo richiamato ogni volta che viene aggiunta una nuova conversazione 1 - recupero uid conversazione da aprire passato nell'url ( se esiste ) 2 - cerco nell'array conversazioni se esiste una con uid uguale a uid conversazione selezionata 2.1 - ESISTE: apro pagina elenco messaggi 2.2 - NON ESISTE: apro pagina wellcome
Parameters :
Returns :
void
|
| initConversationsHandler |
initConversationsHandler()
|
|
se non esiste un handler delle conversazioni ne creo uno nuovo lo salvo in chatmanager e mi sottoscrivo al nodo (connect) inizializzo la pagina
Returns :
void
|
| initialize |
initialize()
|
|
imposto i subscribe sul logini/logout e su conversations
Returns :
void
|
| loadListConversations |
loadListConversations()
|
|
1 - salvo in memoria uid ultima conversazione aperta 2 - cerco conv selezionata 3 - cerco precedente conv attiva 4 - cambio stato conversazione a letto 5 - se non c'è una conv attiva oppure esiste ma è diversa da quella selezionata
Returns :
void
|
| logOut |
logOut()
|
|
modulo richiamato se premo su logout 1 - aggiungo placeholderPage 2 - richiamo il logout di firebase
Returns :
void
|
| ngOnInit |
ngOnInit()
|
|
Returns :
void
|
| openMessageList | ||||||||
openMessageList(uidConvSelected: )
|
||||||||
|
Parameters :
Returns :
void
|
| openUsersList | ||||||||
openUsersList(event: )
|
||||||||
|
apro pagina elenco users metodo richiamato da html
Parameters :
Returns :
void
|
| presentPopover | ||||||||
presentPopover(event: )
|
||||||||
|
apro il menu delle opzioni (metodo richiamato da html) alla chiusura controllo su quale opzione ho premuto e attivo l'azione corrispondete
Parameters :
Returns :
void
|
| Public chatConversationsHandler |
chatConversationsHandler:
|
Type : ChatConversationsHandler
|
| Public chatManager |
chatManager:
|
Type : ChatManager
|
| Private conversations |
conversations:
|
Type : Array<ConversationModel>
|
Default value : []
|
| Private conversationsHandler |
conversationsHandler:
|
Type : ChatConversationsHandler
|
| Private conversationWith |
conversationWith:
|
Type : string
|
| Public databaseProvider |
databaseProvider:
|
Type : DatabaseProvider
|
| Public events |
events:
|
Type : Events
|
| Public LABEL_MSG_PUSH_START_CHAT |
LABEL_MSG_PUSH_START_CHAT:
|
Default value : LABEL_MSG_PUSH_START_CHAT
|
| Public LABEL_MSG_START_CHAT |
LABEL_MSG_START_CHAT:
|
Default value : LABEL_MSG_START_CHAT
|
| Public modalCtrl |
modalCtrl:
|
Type : ModalController
|
| Public navCtrl |
navCtrl:
|
Type : NavController
|
| Public navParams |
navParams:
|
Type : NavParams
|
| Public navProxy |
navProxy:
|
Type : NavProxyService
|
| Public popoverCtrl |
popoverCtrl:
|
Type : PopoverController
|
| Private uidConvSelected |
uidConvSelected:
|
Type : string
|
| Private uidReciverFromUrl |
uidReciverFromUrl:
|
Type : string
|
| Public userService |
userService:
|
Type : UserService
|
import { Component } from '@angular/core';
import { Events, PopoverController, IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';
// models
import { ConversationModel } from '../../models/conversation';
// pages
import { LoginPage } from '../authentication/login/login';
import { PlaceholderPage } from '../placeholder/placeholder';
import { DettaglioConversazionePage } from '../dettaglio-conversazione/dettaglio-conversazione';
import { UsersPage } from '../users/users';
import { _MasterPage } from '../_MasterPage';
import { PopoverPage } from '../popover/popover';
import { ProfilePage } from '../profile/profile';
// utils
import { windowsMatchMedia, searchIndexInArrayForUid } from '../../utils/utils';
import { LABEL_MSG_PUSH_START_CHAT, LABEL_MSG_START_CHAT } from '../../utils/constants';
// services
import { ChatManager } from '../../providers/chat-manager/chat-manager';
import { NavProxyService } from '../../providers/nav-proxy';
import { UserService } from '../../providers/user/user';
import { ChatConversationsHandler} from '../../providers/chat-conversations-handler';
import { DatabaseProvider } from '../../providers/database/database';
@IonicPage()
@Component({
selector: 'page-lista-conversazioni',
templateUrl: 'lista-conversazioni.html',
})
export class ListaConversazioniPage extends _MasterPage {
private conversations: Array<ConversationModel> = [];
private conversationWith: string;
private uidReciverFromUrl: string;
//private uidLastOpenConversation: string;
private conversationsHandler: ChatConversationsHandler;
private uidConvSelected: string;
// private updatingMessageList: boolean;
// in html
public LABEL_MSG_START_CHAT = LABEL_MSG_START_CHAT;
public LABEL_MSG_PUSH_START_CHAT = LABEL_MSG_PUSH_START_CHAT;
constructor(
public popoverCtrl: PopoverController,
public modalCtrl: ModalController,
public navCtrl: NavController,
public navParams: NavParams,
public navProxy: NavProxyService,
public userService: UserService,
public chatConversationsHandler: ChatConversationsHandler,
public events: Events,
public chatManager: ChatManager,
public databaseProvider: DatabaseProvider
) {
super();
// RECUPERO ID CONVERSAZIONE
// se vengo da dettaglio conversazione
// o da users con conversazione attiva recupero conversationWith
this.conversationWith = navParams.get('conversationWith');
this.uidReciverFromUrl = location.search.split('recipient=')[1];
// this.updatingMessageList = false;
if(this.uidReciverFromUrl){
//that.uidLastOpenConversation = that.uidReciverFromUrl;
this.uidConvSelected = this.uidReciverFromUrl;
this.uidReciverFromUrl = null;
}
/** SUBSCRIBE USER LOGGED
* sul LOGIN:
* 1 - dismetto modale login se è visibile
* 2 - carico elenco conversazioni
* sul LOGOUT
* 1 - aggiungo placeholder alla pg delle conversazioni
* 2 - resetto array conversazioni
* 3 - visualizzo la pg di login
* 4 - resetto conversationWith
*/
const profileModal = this.modalCtrl.create(LoginPage, null, { enableBackdropDismiss: false });
this.events.subscribe('loggedUser:login', user => {
console.log('************** LOGGED currentUser:', user);
profileModal.dismiss({animate: false, duration: 0})
const that = this;
this.loadListConversations();
// se dopo 1 sec nn ci sono conversazioni carico placeholder
setTimeout(function(){
console.log('************** that.conversations.length:', that.conversations.length);
if(that.conversations.length <= 0){
that.navProxy.pushDetail(PlaceholderPage,{});
}
}, 1000);
});
this.events.subscribe('loggedUser:logout', user => {
console.log('************** NN LOGGED currentUser:', user);
this.navProxy.pushDetail(PlaceholderPage,{});
this.conversations = [];
//const profileModal = this.modalCtrl.create(LoginPage, null, { enableBackdropDismiss: false });
profileModal.present();
this.conversationWith = null;
});
/** SUBSCRIBE CONVERSATIONS LIST
* sottosrcizione all'elenco delle conversazioni
* l'eleco viene aggiornato ogni volta che viene aggiunta una nuova connversazione
* la nuova conversazione viene aggiunta come primo elemento dell'array in posizione 0
* apro la pagina della conversazione
*/
this.events.subscribe('conversations:added', (conversation) => {
console.log('************** conversations added:', conversation);
this.conversations.splice(0, 0, conversation);
//this.conversations = conversations;
this.goToconversationMessageList(conversation.uid);
});
this.events.subscribe('conversations:changed', conversation => {
const index = searchIndexInArrayForUid(this.conversations, conversation.uid);
console.log("child_changed", index);
if(this.uidConvSelected == conversation.uid){
conversation.status = '0';
conversation.selected = true;
}
this.conversations.splice(index, 1, conversation);
// se la conversazione cambiata è la conversazione aperta imposto status a 0 (visto)
// console.log('************** conversations changed:', conversations, this.uidConvSelected);
// if(this.uidConvSelected == uid){
// var conversationSelected = this.conversations.find(item => item.uid === this.uidConvSelected);
// conversationSelected.status = '0';
// conversationSelected.selected = true;
// }
// this.conversations = conversations;
});
this.events.subscribe('uidConvSelected:changed', uidConvSelected => {
console.log('************** uidConvSelected:changed:', uidConvSelected);
// SE E' DIVERSO DA this.uidConvSelected
// 1 - cerco conv con id == this.uidConvSelected e imposto select a FALSE
// 2 - cerco conv con id == nw uidConvSelected se esiste:
// 2.1 - imposto status a 0 come letto
// 2.2 - seleziono conv selected == TRUE
// 2.3 - imposto nw uidConvSelected come this.uidConvSelected
// 2.4 - apro conv
// 3 salvo id conv nello storage
this.openMessageList(uidConvSelected);
});
}
/**
*
*/
ngOnInit() {
//console.log('ngOnInit');
//this.initialize();
}
/**
* imposto i subscribe sul logini/logout e su conversations
*/
initialize(){
//console.log('**************initialize: LOG',this.events);
}
openMessageList(uidConvSelected){
if(uidConvSelected == this.uidConvSelected && windowsMatchMedia()){
return
}
var oldConversationSelected = this.conversations.find(item => item.uid === this.uidConvSelected);
if(oldConversationSelected){
oldConversationSelected.selected = false;
}
var conversationSelected = this.conversations.find(item => item.uid === uidConvSelected);
if(conversationSelected){
conversationSelected.status = '0';
conversationSelected.selected = true;
this.navProxy.pushDetail(DettaglioConversazionePage, {
conversationWith: uidConvSelected,
conversationWithFullname: conversationSelected.conversation_with_fullname
});
this.databaseProvider.setUidLastOpenConversation(uidConvSelected);
}
this.conversationWith = uidConvSelected;
this.uidConvSelected = uidConvSelected;
}
/**
* 1 - salvo in memoria uid ultima conversazione aperta
* 2 - cerco conv selezionata
* 3 - cerco precedente conv attiva
* 4 - cambio stato conversazione a letto
* 5 - se non c'è una conv attiva oppure esiste ma è diversa da quella selezionata
* - imposto la nuova conv attiva
* - arico l'elenco dei messaggi della conversazione nella navProxy
* - imposto conversationWith con uid conversazione selezionata
*/
// openMessageListOld(uidConvSelected){
// // imposto id conv selezionata
// this.databaseProvider.setUidLastOpenConversation(uidConvSelected);
// this.uidLastOpenConversation = uidConvSelected;
// var conversationSelected = this.conversations.find(item => item.uid === uidConvSelected);
// var conversationOldSelected = this.conversations.find(item => item.selected === true);
// conversationSelected.status = '0';
// this.conversationsHandler.setConversationSelected(uidConvSelected);
// console.log('openMessageList -> uidConvSelected: ' + conversationOldSelected, uidConvSelected, conversationSelected);
// if(!conversationOldSelected || (conversationOldSelected && conversationOldSelected.uid !== uidConvSelected)){
// this.navProxy.pushDetail(DettaglioConversazionePage, {
// conversationWith: uidConvSelected,
// conversationWithFullname: conversationSelected.conversation_with_fullname
// });
// this.conversationWith = uidConvSelected;
// }
// }
/**
* START GESTIONE CONVERSAZIONI
* carico elenco conversazioni
* (al caricamento delle conversazioni -> PUBLIC conversations:update)
* inizializzo l'handler delle conversazioni
*/
loadListConversations() {
const that = this;
this.databaseProvider.getUidLastOpenConversation()
.then(function(uid) {
console.log('getUidLastOpenConversation ::' + uid);
if(uid){
that.uidConvSelected = uid;
}
that.initConversationsHandler();
});
}
/**
* se non esiste un handler delle conversazioni ne creo uno nuovo
* lo salvo in chatmanager
* e mi sottoscrivo al nodo (connect)
* inizializzo la pagina
*/
initConversationsHandler() {
console.log('initConversationsHandler ::');
const tenant = this.chatManager.getTenant();
const loggedUser = this.chatManager.getLoggedUser();
let handler = this.chatManager.conversationsHandler;
if (!handler) {
console.log('handler NON ESISTE ::');
handler = this.chatConversationsHandler.initWithTenant(tenant,loggedUser);
this.chatManager.setConversationsHandler(handler);
handler.connect();
this.conversationsHandler = handler;
} else {
console.log('handler ESISTE ::', handler);
//this.chatManager.setConversationsHandler(handler);
//handler.delegateView = self;
this.conversationsHandler = handler;
}
}
/**
* metodo richiamato ogni volta che viene aggiunta una nuova conversazione
* 1 - recupero uid conversazione da aprire passato nell'url ( se esiste )
* 2 - cerco nell'array conversazioni se esiste una con uid uguale a uid conversazione selezionata
* 2.1 - ESISTE: apro pagina elenco messaggi
* 2.2 - NON ESISTE: apro pagina wellcome
*/
goToconversationMessageList(uid){
// se uid == this.uidConvSelected
if(uid == this.uidConvSelected){
var conversationSelected = this.conversations.find(item => item.uid === uid);
if(conversationSelected){
conversationSelected.status = '0';
conversationSelected.selected = true;
this.navProxy.pushDetail(DettaglioConversazionePage, {
conversationWith: uid,
conversationWithFullname: conversationSelected.conversation_with_fullname,
channel_type: conversationSelected.channel_type
});
}
}
//const that = this;
///const conversationSelected = this.conversations.find(item => item.uid === that.uidLastOpenConversation);
//const isExists = this.conversations.find(i => i.uid === that.uidConvSelected);
// var conversationSelected = this.conversations.find(item => item.uid === this.uidConvSelected);
// if(conversationSelected){
// conversationSelected.selected = true;
//this.openMessageList(uid);
// } else{
// this.styleMessageWelcome = true;
// }
//console.log('responseSearch -> ', conversationSelected, this.uidConvSelected);
// if (isExists){
// that.openMessageList(that.uidConvSelected);
// }
// else {
// that.styleMessageWelcome = true;
// }
}
/**
* apro pagina elenco users
* metodo richiamato da html
*/
openUsersList(event) {
//this.navCtrl.setRoot(UsersPage);
this.navCtrl.push(UsersPage, {
contacts: ""
});
}
/**
* apro il menu delle opzioni
* (metodo richiamato da html)
* alla chiusura controllo su quale opzione ho premuto e attivo l'azione corrispondete
*/
presentPopover(event) {
let popover = this.popoverCtrl.create(PopoverPage);
popover.present({
ev: event
});
popover.onDidDismiss((data: string) => {
console.log(" ********* data::: ", data);
if (data == 'logOut') {
this.logOut();
}
else if (data == 'ProfilePage') {
if(this.chatManager.getLoggedUser()){
this.navCtrl.push(ProfilePage);
}
}
});
}
/**
* modulo richiamato se premo su logout
* 1 - aggiungo placeholderPage
* 2 - richiamo il logout di firebase
*/
logOut() {
//this.navProxy.pushDetail(PlaceholderPage,{});
this.userService.logoutUser();
}
}
<ion-header>
<ion-navbar>
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>
<img src="../assets/chat21-logo.svg" alt="{{this.tenant}}">
</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="openUsersList($event)">
<ion-icon name="create"></ion-icon>
</button>
<button ion-button icon-only (click)="presentPopover($event)">
<ion-icon name="more"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content class="list-avatar-page">
<ion-list>
<span *ngIf="conversations.length > 0;then contentConversations else contentMessageWelcome">here is ignored</span>
<ng-template #contentConversations>
<!-- <ion-item [style.background-color]="conversation.selected === true ? bck_color_selected : bck_color_unselected" ion-item *ngFor="let conversation of conversations" (click)="goToConversationDetail(conversation.uid,conversation.convers_with)"> -->
<ion-item no-lines [ngClass]="conversation.selected === true ? 'selected' : 'unselected'" ion-item *ngFor="let conversation of conversations" (click)="openMessageList(conversation.uid)">
<ion-avatar item-start>
<img src="{{conversation.image}}" alt="user" onError="this.src='assets/img/no_image.png';">
</ion-avatar>
<h2 class="truncate">{{conversation.conversation_with_fullname}}</h2>
<span *ngIf="conversation.status == 1;then content else other_content">here is ignored</span>
<ng-template #content>
<p class="truncate not-read" [innerHTML]="conversation.last_message_text"></p>
</ng-template>
<ng-template #other_content>
<p class="truncate read" [innerHTML]="conversation.last_message_text"></p>
</ng-template>
<ion-note item-end>{{conversation.time_last_message}}</ion-note>
</ion-item>
</ng-template>
<ng-template #contentMessageWelcome >
<div id="content_message_wellcome" >
<!-- [style.display]="styleMessageWelcome === true ? 'inline' : 'none'" -->
<div class="messaggio">{{LABEL_MSG_PUSH_START_CHAT}}</div>
<div class="pulsanti">
<button ion-button icon-start color="273e53" (click)="openUsersList($event)">
{{LABEL_MSG_START_CHAT}}
</button>
</div>
</div>
</ng-template>
</ion-list>
</ion-content>
<!--<ion-content padding>
<ion-searchbar (input)="search($event)"></ion-searchbar>
<ion-list>
<button ion-item *ngFor="let conversazione of conversations | async" (click)="goToDetails(conversazione)">
<ion-avatar item-start>
<img src= {{conversazione.image}} alt="user" onError="this.src='assets/img/no_image.svg';">
</ion-avatar>
<h2>{{conversazione.convers_with_fullname}}</h2>
<span *ngIf="conversazione.status == 1;then content else other_content">here is ignored</span>
<ng-template #content><p style="font-weight: bold;">{{conversazione.last_message_text}}</p></ng-template>
<ng-template #other_content><p>{{conversazione.last_message_text}}</p></ng-template>
<ion-note item-end class="date-right">{{getTimeLastMessage(conversazione.timestamp)}}</ion-note>
</button>
</ion-list>
</ion-content>-->