MOBILE-2632 message: Sort list after new message
parent
7588808b75
commit
81be79b643
|
@ -13,7 +13,7 @@
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
||||||
import { IonicPage, Platform, NavParams } from 'ionic-angular';
|
import { IonicPage, Platform, NavParams, Content } from 'ionic-angular';
|
||||||
import { TranslateService } from '@ngx-translate/core';
|
import { TranslateService } from '@ngx-translate/core';
|
||||||
import { CoreEventsProvider } from '@providers/events';
|
import { CoreEventsProvider } from '@providers/events';
|
||||||
import { CoreSitesProvider } from '@providers/sites';
|
import { CoreSitesProvider } from '@providers/sites';
|
||||||
|
@ -36,6 +36,7 @@ import { CoreUserProvider } from '@core/user/providers/user';
|
||||||
})
|
})
|
||||||
export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
|
export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
|
||||||
@ViewChild(CoreSplitViewComponent) splitviewCtrl: CoreSplitViewComponent;
|
@ViewChild(CoreSplitViewComponent) splitviewCtrl: CoreSplitViewComponent;
|
||||||
|
@ViewChild(Content) content: Content;
|
||||||
|
|
||||||
loaded = false;
|
loaded = false;
|
||||||
loadingMessage: string;
|
loadingMessage: string;
|
||||||
|
@ -85,9 +86,16 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
|
||||||
|
|
||||||
// Update conversations when new message is received.
|
// Update conversations when new message is received.
|
||||||
this.newMessagesObserver = eventsProvider.on(AddonMessagesProvider.NEW_MESSAGE_EVENT, (data) => {
|
this.newMessagesObserver = eventsProvider.on(AddonMessagesProvider.NEW_MESSAGE_EVENT, (data) => {
|
||||||
if (data.conversationId) {
|
|
||||||
// Search the conversation to update.
|
// Search the conversation to update.
|
||||||
const conversation = this.findConversation(data.conversationId);
|
let conversation;
|
||||||
|
|
||||||
|
if (data.conversationId) {
|
||||||
|
conversation = this.findConversation(data.conversationId);
|
||||||
|
} else if (data.userId) {
|
||||||
|
conversation = this.individual.conversations && this.individual.conversations.find((conv) => {
|
||||||
|
return conv.userid == data.userId;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof conversation == 'undefined') {
|
if (typeof conversation == 'undefined') {
|
||||||
// Probably a new conversation, refresh the list.
|
// Probably a new conversation, refresh the list.
|
||||||
|
@ -95,10 +103,25 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
|
||||||
this.refreshData().finally(() => {
|
this.refreshData().finally(() => {
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
});
|
});
|
||||||
} else {
|
} else if (conversation.lastmessage != data.message || conversation.lastmessagedate != data.timecreated / 1000) {
|
||||||
|
const isNewer = data.timecreated / 1000 > conversation.lastmessagedate;
|
||||||
|
|
||||||
// An existing conversation has a new message, update the last message.
|
// An existing conversation has a new message, update the last message.
|
||||||
conversation.lastmessage = data.message;
|
conversation.lastmessage = data.message;
|
||||||
conversation.lastmessagedate = data.timecreated / 1000;
|
conversation.lastmessagedate = data.timecreated / 1000;
|
||||||
|
|
||||||
|
// Sort the affected list.
|
||||||
|
if (conversation.isfavourite) {
|
||||||
|
this.favourites.conversations = this.messagesProvider.sortConversations(this.favourites.conversations);
|
||||||
|
} else if (conversation.type == AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP) {
|
||||||
|
this.group.conversations = this.messagesProvider.sortConversations(this.group.conversations);
|
||||||
|
} else {
|
||||||
|
this.individual.conversations = this.messagesProvider.sortConversations(this.individual.conversations);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNewer) {
|
||||||
|
// The last message is newer than the previous one, scroll to top to keep viewing the conversation.
|
||||||
|
this.domUtils.scrollToTop(this.content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, this.siteId);
|
}, this.siteId);
|
||||||
|
|
Loading…
Reference in New Issue