Fix network timeline gap loading
This commit is contained in:
parent
5986bd80b2
commit
23ff74d2e4
|
@ -136,6 +136,10 @@ class NetworkTimelineViewModel @Inject constructor(
|
||||||
override fun loadMore(placeholderId: String) {
|
override fun loadMore(placeholderId: String) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
try {
|
try {
|
||||||
|
val placeholderIndex =
|
||||||
|
statusData.indexOfFirst { it is StatusViewData.Placeholder && it.id == placeholderId }
|
||||||
|
statusData[placeholderIndex] = StatusViewData.Placeholder(placeholderId, isLoading = true)
|
||||||
|
|
||||||
val statusResponse = fetchStatusesForKind(
|
val statusResponse = fetchStatusesForKind(
|
||||||
fromId = placeholderId.inc(),
|
fromId = placeholderId.inc(),
|
||||||
uptoId = null,
|
uptoId = null,
|
||||||
|
@ -148,28 +152,46 @@ class NetworkTimelineViewModel @Inject constructor(
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
statusData.removeAt(placeholderIndex)
|
||||||
|
|
||||||
val activeAccount = accountManager.activeAccount!!
|
val activeAccount = accountManager.activeAccount!!
|
||||||
|
|
||||||
val data = statuses.map { status ->
|
val data = statuses.map { status ->
|
||||||
val oldStatus = statusData.find { s ->
|
|
||||||
s.asStatusOrNull()?.id == status.id
|
|
||||||
}?.asStatusOrNull()
|
|
||||||
|
|
||||||
val contentShowing = oldStatus?.isShowingContent ?: activeAccount.alwaysShowSensitiveMedia || !status.actionableStatus.sensitive
|
|
||||||
val expanded = oldStatus?.isExpanded ?: activeAccount.alwaysOpenSpoiler
|
|
||||||
val contentCollapsed = oldStatus?.isCollapsed ?: true
|
|
||||||
|
|
||||||
status.toViewData(
|
status.toViewData(
|
||||||
isShowingContent = contentShowing,
|
isShowingContent = activeAccount.alwaysShowSensitiveMedia || !status.actionableStatus.sensitive,
|
||||||
isExpanded = expanded,
|
isExpanded = activeAccount.alwaysOpenSpoiler,
|
||||||
isCollapsed = contentCollapsed
|
isCollapsed = true
|
||||||
)
|
)
|
||||||
|
}.toMutableList()
|
||||||
|
|
||||||
|
if (statuses.isNotEmpty()) {
|
||||||
|
val firstId = statuses.first().id.hashCode().toLong()
|
||||||
|
val lastId = statuses.last().id.hashCode().toLong()
|
||||||
|
val overlappedFrom = statusData.indexOfFirst { it.viewDataId <= firstId }
|
||||||
|
val overlappedTo = statusData.indexOfFirst { it.viewDataId < lastId }
|
||||||
|
|
||||||
|
if (overlappedFrom < overlappedTo) {
|
||||||
|
repeat(overlappedTo - overlappedFrom) {
|
||||||
|
statusData[overlappedFrom].asStatusOrNull()?.let { oldStatus ->
|
||||||
|
val dataIndex = statuses.indexOfFirst { it.id == oldStatus.id }
|
||||||
|
if (dataIndex == -1) {
|
||||||
|
return@let
|
||||||
|
}
|
||||||
|
data[dataIndex] = data[dataIndex]
|
||||||
|
.copy(
|
||||||
|
isShowingContent = oldStatus.isShowingContent,
|
||||||
|
isExpanded = oldStatus.isExpanded,
|
||||||
|
isCollapsed = oldStatus.isCollapsed,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
statusData.removeAt(overlappedFrom)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
statusData.add(overlappedFrom, StatusViewData.Placeholder(statuses.last().id.dec(), isLoading = false))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val index =
|
statusData.addAll(placeholderIndex, data)
|
||||||
statusData.indexOfFirst { it is StatusViewData.Placeholder && it.id == placeholderId }
|
|
||||||
statusData.removeAt(index)
|
|
||||||
statusData.addAll(index, data)
|
|
||||||
|
|
||||||
currentSource?.invalidate()
|
currentSource?.invalidate()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
Loading…
Reference in New Issue