Merge pull request #2905 from vector-im/feature/bma/preview_fix

Try to fix crash about UrlPreview (#2640)
This commit is contained in:
Benoit Marty 2021-02-23 16:07:16 +01:00 committed by GitHub
commit 60cc441092
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 9 deletions

View File

@ -9,7 +9,7 @@ Improvements 🙌:
- Improve initial sync performance (#983)
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)
}