From bebb9560053e08ec072deff452b05f95ac443c2d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 22 Feb 2021 17:44:11 +0100 Subject: [PATCH] Try to fix crash about UrlPreview (#2640) --- CHANGES.md | 2 +- .../features/home/room/detail/RoomDetailViewModel.kt | 4 ++-- .../room/detail/timeline/url/PreviewUrlRetriever.kt | 11 +++++------ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 46057a37ec..4c87e88e47 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,7 @@ Improvements 🙌: - Fetch homeserver type and version and display in a new setting screen and add info in rageshakes (#2831) Bugfix 🐛: - - + - Try to fix crash about UrlPreview (#2640) Translations 🗣: - diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt index c7a5873a65..601891b15a 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt @@ -132,7 +132,7 @@ class RoomDetailViewModel @AssistedInject constructor( val timeline = room.createTimeline(eventId, timelineSettings) // Same lifecycle than the ViewModel (survive to screen rotation) - val previewUrlRetriever = PreviewUrlRetriever(session) + val previewUrlRetriever = PreviewUrlRetriever(session, viewModelScope) // Slot to keep a pending action during permission request var pendingAction: RoomDetailAction? = null @@ -1425,7 +1425,7 @@ class RoomDetailViewModel @AssistedInject constructor( snapshot .takeIf { state.asyncRoomSummary.invoke()?.isEncrypted == false } ?.forEach { - previewUrlRetriever.getPreviewUrl(it, viewModelScope) + previewUrlRetriever.getPreviewUrl(it) } } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlRetriever.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlRetriever.kt index 54d5fd9eb3..df75c0094b 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlRetriever.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlRetriever.kt @@ -16,17 +16,17 @@ package im.vector.app.features.home.room.detail.timeline.url -import android.os.Handler -import android.os.Looper import im.vector.app.BuildConfig import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.matrix.android.sdk.api.cache.CacheStrategy import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent import org.matrix.android.sdk.api.session.room.timeline.getLatestEventId -class PreviewUrlRetriever(session: Session) { +class PreviewUrlRetriever(session: Session, + private val coroutineScope: CoroutineScope) { private val mediaService = session.mediaService() private data class EventIdPreviewUrlUiState( @@ -38,12 +38,11 @@ class PreviewUrlRetriever(session: Session) { // Keys are the main eventId private val data = mutableMapOf() private val listeners = mutableMapOf>() - private val uiHandler = Handler(Looper.getMainLooper()) // In memory list private val blockedUrl = mutableSetOf() - fun getPreviewUrl(event: TimelineEvent, coroutineScope: CoroutineScope) { + fun getPreviewUrl(event: TimelineEvent) { val eventId = event.root.eventId ?: return val latestEventId = event.getLatestEventId() @@ -115,7 +114,7 @@ class PreviewUrlRetriever(session: Session) { private fun updateState(eventId: String, latestEventId: String, state: PreviewUrlUiState) { data[eventId] = EventIdPreviewUrlUiState(latestEventId, state) // Notify the listener - uiHandler.post { + coroutineScope.launch(Dispatchers.Main) { listeners[eventId].orEmpty().forEach { it.onStateUpdated(state) }