Refactor the QML past events loading code
- Make the code cleaner and more declarative - Start loading past events when the join state of the room changes, e.g. when accepting an invite, load history right then - Properly cancel the loading task when leaving the room chat page
This commit is contained in:
parent
702497cb48
commit
78acb51220
@ -219,12 +219,9 @@ Rectangle {
|
|||||||
HListView {
|
HListView {
|
||||||
id: eventList
|
id: eventList
|
||||||
|
|
||||||
property string inviter: chat.roomInfo.inviter || ""
|
|
||||||
property real yPos: visibleArea.yPosition
|
|
||||||
property bool canLoad: true
|
|
||||||
property bool loading: false
|
|
||||||
|
|
||||||
property Future updateMarkerFuture: null
|
property Future updateMarkerFuture: null
|
||||||
|
property Future loadPastEventsFuture: null
|
||||||
|
property bool moreToLoad: true
|
||||||
|
|
||||||
property bool ownEventsOnLeft:
|
property bool ownEventsOnLeft:
|
||||||
window.settings.ownMessagesOnLeftAboveWidth < 0 ?
|
window.settings.ownMessagesOnLeftAboveWidth < 0 ?
|
||||||
@ -236,6 +233,12 @@ Rectangle {
|
|||||||
|
|
||||||
property alias cursorShape: cursorShapeArea.cursorShape
|
property alias cursorShape: cursorShapeArea.cursorShape
|
||||||
|
|
||||||
|
readonly property bool shouldLoadPastEvents:
|
||||||
|
! chat.roomInfo.inviter_id &&
|
||||||
|
! chat.roomInfo.left &&
|
||||||
|
moreToLoad &&
|
||||||
|
visibleArea.yPosition < 0.1
|
||||||
|
|
||||||
readonly property var thumbnailCachedPaths: ({}) // {event.id: path}
|
readonly property var thumbnailCachedPaths: ({}) // {event.id: path}
|
||||||
|
|
||||||
readonly property var redactableCheckedEvents:
|
readonly property var redactableCheckedEvents:
|
||||||
@ -321,35 +324,15 @@ Rectangle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadPastEvents() {
|
function loadPastEvents() {
|
||||||
// try/catch blocks to hide pyotherside error when the
|
loadPastEventsFuture = py.callClientCoro(
|
||||||
// component is destroyed but func is still running
|
chat.userId,
|
||||||
|
"load_past_events",
|
||||||
try {
|
[chat.roomId],
|
||||||
eventList.canLoad = false
|
more => {
|
||||||
eventList.loading = true
|
moreToLoad = more
|
||||||
|
loadPastEventsFuture = null
|
||||||
py.callClientCoro(
|
}
|
||||||
chat.userId,
|
)
|
||||||
"load_past_events",
|
|
||||||
[chat.roomId],
|
|
||||||
moreToLoad => {
|
|
||||||
try {
|
|
||||||
eventList.canLoad = moreToLoad
|
|
||||||
|
|
||||||
// Call yPosChanged() to run this func again
|
|
||||||
// if the loaded messages aren't enough to fill
|
|
||||||
// the screen.
|
|
||||||
if (moreToLoad) yPosChanged()
|
|
||||||
|
|
||||||
eventList.loading = false
|
|
||||||
} catch (err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
} catch (err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getFocusedOrSelectedOrLastMediaEvents(acceptLinks=false) {
|
function getFocusedOrSelectedOrLastMediaEvents(acceptLinks=false) {
|
||||||
@ -502,7 +485,7 @@ Rectangle {
|
|||||||
footer: Item {
|
footer: Item {
|
||||||
width: eventList.width
|
width: eventList.width
|
||||||
height: (button.height + theme.spacing * 2) * opacity
|
height: (button.height + theme.spacing * 2) * opacity
|
||||||
opacity: eventList.loading ? 1 : 0
|
opacity: eventList.loadPastEventsFuture ? 1 : 0
|
||||||
visible: opacity > 0
|
visible: opacity > 0
|
||||||
|
|
||||||
Behavior on opacity { HNumberAnimation {} }
|
Behavior on opacity { HNumberAnimation {} }
|
||||||
@ -533,12 +516,19 @@ Rectangle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onYPosChanged:
|
Timer {
|
||||||
if (canLoad && yPos < 0.1) Qt.callLater(loadPastEvents)
|
interval: 200
|
||||||
|
running:
|
||||||
|
eventList.shouldLoadPastEvents &&
|
||||||
|
! eventList.loadPastEventsFuture
|
||||||
|
triggeredOnStart: true
|
||||||
|
onTriggered: eventList.loadPastEvents()
|
||||||
|
|
||||||
// When an invited room becomes joined, we should now be able to
|
}
|
||||||
// fetch past events.
|
|
||||||
onInviterChanged: canLoad = true
|
Component.onDestruction: {
|
||||||
|
if (loadPastEventsFuture) loadPastEventsFuture.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
MouseArea {
|
MouseArea {
|
||||||
id: cursorShapeArea
|
id: cursorShapeArea
|
||||||
|
Loading…
x
Reference in New Issue
Block a user