-
Notifications
You must be signed in to change notification settings - Fork 45
feat: refresh editable assets (WPB-21977) #4460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
8f834c7
29f6382
3b83c22
f237d6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * Wire | ||
| * Copyright (C) 2025 Wire Swiss GmbH | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see http://www.gnu.org/licenses/. | ||
| */ | ||
| package com.wire.android.ui.home.conversations.model.messagetypes.multipart | ||
|
|
||
| import androidx.annotation.VisibleForTesting | ||
| import androidx.annotation.VisibleForTesting.Companion.PRIVATE | ||
| import com.wire.android.ui.common.multipart.MultipartAttachmentUi | ||
| import com.wire.android.ui.common.multipart.toUiModel | ||
| import com.wire.android.util.ExpiringMap | ||
| import com.wire.kalium.cells.domain.usecase.RefreshCellAssetStateUseCase | ||
| import com.wire.kalium.common.functional.onSuccess | ||
| import com.wire.kalium.logic.data.message.MessageAttachment | ||
| import com.wire.kalium.logic.featureFlags.KaliumConfigs | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.SupervisorJob | ||
| import kotlinx.coroutines.cancel | ||
| import kotlinx.coroutines.launch | ||
| import kotlin.time.Duration.Companion.hours | ||
| import kotlin.time.Duration.Companion.seconds | ||
|
|
||
| class CellAssetRefreshHelper( | ||
| private val refreshAsset: RefreshCellAssetStateUseCase, | ||
| private val featureFlags: KaliumConfigs, | ||
| private val coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main), | ||
| private val currentTime: () -> Long = { System.currentTimeMillis() }, | ||
| ) { | ||
|
|
||
| private companion object Companion { | ||
| // Default refresh rate for not editable asset if URL expiration is missing | ||
| val DEFAULT_CONTENT_URL_EXPIRY_MS = 1.hours.inWholeMilliseconds | ||
|
|
||
| // Refresh rate for editable assets | ||
| val EDITABLE_CONTENT_EXPIRY_MS = 30.seconds.inWholeMilliseconds | ||
| } | ||
|
|
||
| /** | ||
| * Regular files. | ||
| * - Refresh on first display. | ||
| * - Refresh when content URL expires (unlikely). | ||
| */ | ||
| @VisibleForTesting(otherwise = PRIVATE) | ||
| val regularAssets = expiringMap( | ||
| expirationMs = DEFAULT_CONTENT_URL_EXPIRY_MS, | ||
| onExpired = { assetId -> | ||
| coroutineScope.launch { refreshAsset(assetId) } | ||
| }, | ||
| ) | ||
|
|
||
| /** | ||
| * Editable files. Could be updated frequently. | ||
| * - Refresh on first display. | ||
| * - Refresh every 30 sec to update preview if currently visible. | ||
| */ | ||
| @VisibleForTesting(otherwise = PRIVATE) | ||
|
Check warning on line 70 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| val visibleEditableAssets: ExpiringMap<String, Unit> = expiringMap( | ||
| expirationMs = EDITABLE_CONTENT_EXPIRY_MS, | ||
| onExpired = { assetId -> | ||
| coroutineScope.launch { refreshAsset(assetId) } | ||
|
Check warning on line 74 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
|
|
||
| // Re-add to schedule next refresh | ||
| visibleEditableAssets[assetId] = Unit | ||
|
Check warning on line 77 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| } | ||
| ) | ||
|
|
||
| private fun expiringMap(expirationMs: Long, onExpired: (String) -> Unit) = ExpiringMap<String, Unit>( | ||
| scope = coroutineScope, | ||
| expirationMs = expirationMs, | ||
| delegate = mutableMapOf(), | ||
| onEntryExpired = { key, _ -> onExpired(key) }, | ||
| currentTime = currentTime, | ||
| ) | ||
|
|
||
| fun onAttachmentsVisible(attachments: List<MessageAttachment>) { | ||
| attachments.forEach { | ||
| onAttachmentVisible(it.toUiModel()) | ||
| } | ||
| } | ||
|
|
||
| fun onAttachmentsHidden(attachments: List<MessageAttachment>) { | ||
| attachments.forEach { | ||
| onAttachmentHidden(it.toUiModel()) | ||
|
Check warning on line 97 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| } | ||
| } | ||
|
|
||
| fun onAttachmentVisible(attachment: MultipartAttachmentUi) { | ||
| if (attachment.isEditSupported && featureFlags.collaboraIntegration) { | ||
|
|
||
| if (visibleEditableAssets.contains(attachment.uuid)) return | ||
|
|
||
| visibleEditableAssets[attachment.uuid] = Unit | ||
|
Check warning on line 106 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
|
|
||
| coroutineScope.launch { | ||
| refreshAsset(attachment.uuid) | ||
|
Check warning on line 109 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| } | ||
| } else { | ||
|
|
||
| if (regularAssets.contains(attachment.uuid)) return | ||
|
|
||
| if (attachment.contentUrlExpiresAt != null) { | ||
| regularAssets.putWithExpireAt(attachment.uuid, Unit, attachment.contentUrlExpiresAt) | ||
| } else { | ||
| regularAssets[attachment.uuid] = Unit | ||
| } | ||
|
|
||
| coroutineScope.launch { | ||
| refreshAsset(attachment.uuid) | ||
| .onSuccess { node -> | ||
| if (node.supportedEditors.isNotEmpty()) { | ||
| regularAssets.remove(attachment.uuid) | ||
| visibleEditableAssets[attachment.uuid] = Unit | ||
|
Check warning on line 126 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun onAttachmentHidden(attachment: MultipartAttachmentUi) { | ||
| if (attachment.isEditSupported) { | ||
| visibleEditableAssets.remove(attachment.uuid) | ||
|
Check warning on line 135 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| } | ||
| } | ||
|
|
||
| fun refresh(uuid: String) { | ||
| coroutineScope.launch { | ||
| refreshAsset(uuid) | ||
|
Check warning on line 141 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| } | ||
| } | ||
|
|
||
| fun close() { | ||
| coroutineScope.cancel() | ||
|
Check warning on line 146 in app/src/main/kotlin/com/wire/android/ui/home/conversations/model/messagetypes/multipart/CellAssetRefreshHelper.kt
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coroutineScope is created with Dispatchers.Main, this launch will run
refreshAsset(assetId)(that is callingfileSystem.delete()fileSystem.exists()) on the UI thread.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, but I will take care of this in the usecase to keep the helper clean from implementation details of asset refresh.