Skip to content

Commit

Permalink
[AND-151] Improve attachment factories (#5494)
Browse files Browse the repository at this point in the history
* Add filter option to default factories method

* Fallback to upload file on case the asset url is null
  • Loading branch information
JcMinarro authored Dec 5, 2024
1 parent adab353 commit 731832d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
### ⬆️ Improved

### ✅ Added
- The `StreamAttachmentFactories.defaultFactories()` method now accepts a `skipTypes` parameter to skip specific factory types. [#5494](https://github.com/GetStream/stream-chat-android/pull/5494)

### ⚠️ Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ public final class io/getstream/chat/android/compose/ui/attachments/AttachmentFa
public final class io/getstream/chat/android/compose/ui/attachments/StreamAttachmentFactories {
public static final field $stable I
public static final field INSTANCE Lio/getstream/chat/android/compose/ui/attachments/StreamAttachmentFactories;
public final fun defaultFactories (Lkotlin/jvm/functions/Function0;ILio/getstream/chat/android/ui/common/utils/GiphyInfoType;Lio/getstream/chat/android/ui/common/utils/GiphySizingMode;Landroidx/compose/ui/layout/ContentScale;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function6;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public static synthetic fun defaultFactories$default (Lio/getstream/chat/android/compose/ui/attachments/StreamAttachmentFactories;Lkotlin/jvm/functions/Function0;ILio/getstream/chat/android/ui/common/utils/GiphyInfoType;Lio/getstream/chat/android/ui/common/utils/GiphySizingMode;Landroidx/compose/ui/layout/ContentScale;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function6;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Ljava/util/List;
public final fun defaultFactories (Lkotlin/jvm/functions/Function0;ILio/getstream/chat/android/ui/common/utils/GiphyInfoType;Lio/getstream/chat/android/ui/common/utils/GiphySizingMode;Landroidx/compose/ui/layout/ContentScale;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function6;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Ljava/util/List;)Ljava/util/List;
public static synthetic fun defaultFactories$default (Lio/getstream/chat/android/compose/ui/attachments/StreamAttachmentFactories;Lkotlin/jvm/functions/Function0;ILio/getstream/chat/android/ui/common/utils/GiphyInfoType;Lio/getstream/chat/android/ui/common/utils/GiphySizingMode;Landroidx/compose/ui/layout/ContentScale;ZLkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function6;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Ljava/util/List;ILjava/lang/Object;)Ljava/util/List;
public final fun defaultQuotedFactories ()Ljava/util/List;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public object StreamAttachmentFactories {
* @param onMediaContentItemClick Lambda called when a image or video attachment content item gets clicked.
* @param onFileContentItemClick Lambda called when a file attachment content item gets clicked.
* @param showFileSize Lambda called to determine if the file size should be shown for a given attachment.
* @param skipTypes A list of [AttachmentFactory.Type] that should be skipped from the default factories.
*
* @return A [List] of various [AttachmentFactory] instances that provide different attachments support.
*/
Expand Down Expand Up @@ -108,6 +109,7 @@ public object StreamAttachmentFactories {
previewHandlers: List<AttachmentPreviewHandler>,
attachment: Attachment,
) -> Unit = ::onFileAttachmentContentItemClick,
skipTypes: List<AttachmentFactory.Type> = emptyList(),
): List<AttachmentFactory> = listOf(
UploadAttachmentFactory(
onContentItemClick = onUploadContentItemClick,
Expand Down Expand Up @@ -138,7 +140,7 @@ public object StreamAttachmentFactories {
onContentItemClick = onFileContentItemClick,
),
UnsupportedAttachmentFactory,
)
).filterNot { skipTypes.contains(it.type) }

/**
* Default quoted attachment factories we provide, which can transform image, file and link attachments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import io.getstream.chat.android.client.extensions.durationInMs
Expand Down Expand Up @@ -140,7 +141,10 @@ public fun AudioRecordAttachmentContent(
val viewModel = viewModel(AudioPlayerViewModel::class.java, factory = viewModelFactory)

val audioRecordings = attachmentState.message.attachments
.filter { attachment -> attachment.isAudioRecording() && attachment.assetUrl != null }
.filter { attachment ->
val attachmentUrl = attachment.assetUrl ?: attachment.upload?.toUri()?.toString()
attachment.isAudioRecording() && attachmentUrl != null
}

val playerState by viewModel.state.collectAsStateWithLifecycle()

Expand Down Expand Up @@ -241,7 +245,7 @@ internal fun AudioRecordAttachmentContentItemBase(
onThumbDragStop: (Attachment, Float) -> Unit = { _, _ -> },
tailContent: @Composable (isPlaying: Boolean) -> Unit = {},
) {
val attachmentUrl = attachment.assetUrl
val attachmentUrl = attachment.assetUrl ?: attachment.upload?.toUri()?.toString()
val isCurrentAttachment = attachmentUrl == playerState.current.audioUri
val trackProgress = playerState.current.playingProgress.takeIf { isCurrentAttachment }
?: attachmentUrl?.let { playerState.seekTo.getOrDefault(it.hashCode(), 0f) } ?: 0f
Expand Down

0 comments on commit 731832d

Please sign in to comment.