Try to fix crash about UrlPreview (#2640)

This commit is contained in:
Benoit Marty 2021-02-22 17:44:11 +01:00
parent b38ff46af4
commit bebb956005
3 changed files with 8 additions and 9 deletions

View file

@ -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 🗣:
-

View file

@ -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)
}
}
}

View file

@ -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<String, EventIdPreviewUrlUiState>()
private val listeners = mutableMapOf<String, MutableSet<PreviewUrlRetrieverListener>>()
private val uiHandler = Handler(Looper.getMainLooper())
// In memory list
private val blockedUrl = mutableSetOf<String>()
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)
}