Timeline: url preview listen to Flow in ViewModel

This commit is contained in:
ganfra 2021-12-09 12:28:13 +01:00
parent c830d49924
commit 31ba912d6e

View file

@ -160,6 +160,7 @@ class RoomDetailViewModel @AssistedInject constructor(
observeMyRoomMember()
observeActiveRoomWidgets()
observePowerLevel()
setupPreviewUrlObservers()
room.getRoomSummaryLive()
viewModelScope.launch(Dispatchers.IO) {
tryOrNull { room.markAsRead(ReadService.MarkAsReadParams.READ_RECEIPT) }
@ -263,6 +264,30 @@ class RoomDetailViewModel @AssistedInject constructor(
}
}
private fun setupPreviewUrlObservers() {
if (!vectorPreferences.showUrlPreviews()) {
return
}
combine(
timelineEvents,
room.flow().liveRoomSummary()
.unwrap()
.map { it.isEncrypted }
.distinctUntilChanged()
) { snapshot, isRoomEncrypted ->
if (isRoomEncrypted) {
return@combine
}
withContext(Dispatchers.Default) {
Timber.v("On new timeline events for urlpreview on ${Thread.currentThread()}")
snapshot.forEach {
previewUrlRetriever.getPreviewUrl(it)
}
}
}
.launchIn(viewModelScope)
}
fun getOtherUserIds() = room.roomSummary()?.otherMemberIds
override fun handle(action: RoomDetailAction) {
@ -1031,16 +1056,6 @@ class RoomDetailViewModel @AssistedInject constructor(
// tryEmit doesn't work with SharedFlow without cache
timelineEvents.emit(snapshot)
}
// PreviewUrl
if (vectorPreferences.showUrlPreviews()) {
withState { state ->
snapshot
.takeIf { state.asyncRoomSummary.invoke()?.isEncrypted == false }
?.forEach {
previewUrlRetriever.getPreviewUrl(it)
}
}
}
}
override fun onTimelineFailure(throwable: Throwable) {