From c08c6520808718f0244c91c31f2a69efcf10ef4c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 4 Dec 2020 12:41:26 +0100 Subject: [PATCH] PreviewUrl: handle close (in memory) --- .../home/room/detail/RoomDetailFragment.kt | 6 +++-- .../timeline/TimelineEventController.kt | 2 +- .../timeline/url/PreviewUrlRetriever.kt | 27 +++++++++++++++++-- .../detail/timeline/url/PreviewUrlUiState.kt | 4 ++- .../detail/timeline/url/PreviewUrlView.kt | 17 +++++++++--- .../src/main/res/drawable/ic_close_24dp.xml | 10 +++++++ vector/src/main/res/layout/url_preview.xml | 12 +++++++++ 7 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 vector/src/main/res/drawable/ic_close_24dp.xml diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt index 5e8d41c545..f211e89e7d 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt @@ -140,6 +140,7 @@ import im.vector.app.features.home.room.detail.timeline.item.MessageInformationD import im.vector.app.features.home.room.detail.timeline.item.MessageTextItem import im.vector.app.features.home.room.detail.timeline.item.ReadReceiptData import im.vector.app.features.home.room.detail.timeline.reactions.ViewReactionsBottomSheet +import im.vector.app.features.home.room.detail.timeline.url.PreviewUrlRetriever import im.vector.app.features.home.room.detail.widget.RoomWidgetsBottomSheet import im.vector.app.features.html.EventHtmlRenderer import im.vector.app.features.html.PillImageSpan @@ -215,6 +216,7 @@ class RoomDetailFragment @Inject constructor( private val session: Session, private val avatarRenderer: AvatarRenderer, private val timelineEventController: TimelineEventController, + private val previewUrlRetriever: PreviewUrlRetriever, autoCompleterFactory: AutoCompleter.Factory, private val permalinkHandler: PermalinkHandler, private val notificationDrawerManager: NotificationDrawerManager, @@ -1656,8 +1658,8 @@ class RoomDetailFragment @Inject constructor( onUrlClicked(url, url) } - override fun onPreviewUrlCloseClicked(url: String) { - TODO("Not yet implemented") + override fun onPreviewUrlCloseClicked(eventId: String, url: String) { + previewUrlRetriever.doNotShowPreviewUrlFor(eventId, url) } private fun onShareActionClicked(action: EventSharedAction.Share) { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/TimelineEventController.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/TimelineEventController.kt index 31c4ceaff5..693383c751 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/TimelineEventController.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/TimelineEventController.kt @@ -126,7 +126,7 @@ class TimelineEventController @Inject constructor(private val dateFormatter: Vec interface PreviewUrlCallback { fun onPreviewUrlClicked(url: String) - fun onPreviewUrlCloseClicked(url: String) + fun onPreviewUrlCloseClicked(eventId: String, url: String) } // Map eventId to adapter position 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 0153a38719..b4fbeaf325 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 @@ -32,13 +32,18 @@ class PreviewUrlRetriever @Inject constructor( private val data = mutableMapOf() private val listeners = mutableMapOf>() + // In memory list + private val blockedUrl = mutableSetOf() + fun getPreviewUrl(event: Event, coroutineScope: CoroutineScope) { val eventId = event.eventId ?: return synchronized(data) { if (data[eventId] == null) { // Keep only the first URL for the moment - val url = session.mediaService().extractUrls(event).firstOrNull() + val url = session.mediaService().extractUrls(event) + .firstOrNull() + ?.takeIf { it !in blockedUrl } if (url == null) { updateState(eventId, PreviewUrlUiState.NoUrl) } else { @@ -60,7 +65,12 @@ class PreviewUrlRetriever @Inject constructor( }.fold( { synchronized(data) { - updateState(eventId, PreviewUrlUiState.Data(urlToRetrieve, it)) + // Blocked after the request has been sent? + if (urlToRetrieve in blockedUrl) { + updateState(eventId, PreviewUrlUiState.NoUrl) + } else { + updateState(eventId, PreviewUrlUiState.Data(eventId, urlToRetrieve, it)) + } } }, { @@ -73,6 +83,19 @@ class PreviewUrlRetriever @Inject constructor( } } + fun doNotShowPreviewUrlFor(eventId: String, url: String) { + blockedUrl.add(url) + + // Notify the listener + synchronized(data) { + data[eventId] + ?.takeIf { it is PreviewUrlUiState.Data && it.url == url } + ?.let { + updateState(eventId, PreviewUrlUiState.NoUrl) + } + } + } + private fun updateState(eventId: String, state: PreviewUrlUiState) { data[eventId] = state // Notify the listener diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlUiState.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlUiState.kt index d86633b1e4..a8f8f7b0cb 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlUiState.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlUiState.kt @@ -35,5 +35,7 @@ sealed class PreviewUrlUiState { data class Error(val throwable: Throwable) : PreviewUrlUiState() // PreviewUrl data - data class Data(val url: String, val previewUrlData: PreviewUrlData) : PreviewUrlUiState() + data class Data(val eventId: String, + val url: String, + val previewUrlData: PreviewUrlData) : PreviewUrlUiState() } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlView.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlView.kt index 29701cacbe..b314344356 100755 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlView.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/url/PreviewUrlView.kt @@ -53,6 +53,9 @@ class PreviewUrlView @JvmOverloads constructor( @BindView(R.id.url_preview_site) lateinit var siteView: TextView + @BindView(R.id.url_preview_close) + lateinit var closeView: View + var delegate: TimelineEventController.PreviewUrlCallback? = null init { @@ -78,10 +81,10 @@ class PreviewUrlView @JvmOverloads constructor( hideAll() when (newState) { PreviewUrlUiState.Unknown, - PreviewUrlUiState.NoUrl -> renderHidden() - PreviewUrlUiState.Loading -> renderLoading() + PreviewUrlUiState.NoUrl -> renderHidden() + PreviewUrlUiState.Loading -> renderLoading() is PreviewUrlUiState.Error -> renderHidden() - is PreviewUrlUiState.Data -> renderData(newState.previewUrlData, imageContentRenderer) + is PreviewUrlUiState.Data -> renderData(newState.previewUrlData, imageContentRenderer) } } @@ -92,6 +95,13 @@ class PreviewUrlView @JvmOverloads constructor( } } + private fun onCloseClick() { + when (val finalState = state) { + is PreviewUrlUiState.Data -> delegate?.onPreviewUrlCloseClicked(finalState.eventId, finalState.url) + else -> Unit + } + } + // PRIVATE METHODS **************************************************************************************************************************************** private fun setupView() { @@ -99,6 +109,7 @@ class PreviewUrlView @JvmOverloads constructor( ButterKnife.bind(this) setOnClickListener(this) + closeView.setOnClickListener { onCloseClick() } } private fun renderHidden() { diff --git a/vector/src/main/res/drawable/ic_close_24dp.xml b/vector/src/main/res/drawable/ic_close_24dp.xml new file mode 100644 index 0000000000..d69c331210 --- /dev/null +++ b/vector/src/main/res/drawable/ic_close_24dp.xml @@ -0,0 +1,10 @@ + + + diff --git a/vector/src/main/res/layout/url_preview.xml b/vector/src/main/res/layout/url_preview.xml index ad6259a57e..a08c60c663 100644 --- a/vector/src/main/res/layout/url_preview.xml +++ b/vector/src/main/res/layout/url_preview.xml @@ -26,6 +26,7 @@ android:textColor="?riotx_text_primary" android:textSize="14sp" android:textStyle="bold" + app:layout_constraintEnd_toStartOf="@+id/url_preview_close" app:layout_constraintStart_toStartOf="@+id/url_preview_left_border" app:layout_constraintTop_toTopOf="parent" tools:text="Jo Malone denounces her former brand's John Boyega decision" /> @@ -71,4 +72,15 @@ app:layout_constraintTop_toBottomOf="@+id/url_preview_description" tools:text="BBC News" /> + + \ No newline at end of file