ChatView.qml get rid of polling

Autoscroll to last message on update
This commit is contained in:
Gioacchino Mazzurco 2017-04-03 21:48:01 +02:00
parent 2786cec76a
commit 7d9e89e3d2

View File

@ -24,17 +24,28 @@ Item
{ {
id: chatView id: chatView
property string chatId property string chatId
property int token: 0
function refreshData() function refreshData()
{ {
console.log("chatView.refreshData()", visible)
if(!visible) return
rsApi.request( "/chat/messages/"+chatId, "", function(par) rsApi.request( "/chat/messages/"+chatId, "", function(par)
{ {
chatModel.json = par.response chatModel.json = par.response
if(visible) rsApi.request("/chat/mark_chat_as_read/"+chatId, "", token = JSON.parse(par.response).statetoken
null) mainWindow.registerToken(token, refreshData)
if(chatListView.visible)
{
chatListView.positionViewAtEnd()
rsApi.request("/chat/mark_chat_as_read/"+chatId)
}
} ) } )
} }
Component.onCompleted: refreshData()
onFocusChanged: focus && refreshData() onFocusChanged: focus && refreshData()
JSONListModel JSONListModel
@ -59,6 +70,7 @@ Item
ListView ListView
{ {
id: chatListView
width: parent.width width: parent.width
height: 300 height: 300
model: chatModel.model model: chatModel.model
@ -87,14 +99,4 @@ Item
function(par) { msgComposer.text = ""; } ) function(par) { msgComposer.text = ""; } )
} }
} }
Timer
{
id: refreshTimer
interval: 800
repeat: true
triggeredOnStart: true
onTriggered: if(chatView.visible) chatView.refreshData()
Component.onCompleted: start()
}
} }