Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildscript {
dependencies {
classpath libs.androidGradlePlugin
classpath libs.kotlinGradlePlugin
classpath libs.composeCompilerGradlePlugin
classpath libs.navigationSafeArgsGradlePlugin
classpath libs.spotlessPlugin
}
Expand Down
11 changes: 3 additions & 8 deletions compose-custom-attachments/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'org.jetbrains.kotlin.plugin.compose'
}

android {
compileSdk 34
compileSdk 36

defaultConfig {
applicationId "io.getstream.chat.android.compose.customattachments"
Expand All @@ -24,9 +25,6 @@ android {
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion libs.versions.composeCompiler.get()
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand All @@ -35,16 +33,13 @@ android {

dependencies {
// Stream SDK
implementation "io.getstream:stream-chat-android-compose:6.1.1"
implementation "io.getstream:stream-chat-android-state:6.1.1"
implementation "io.getstream:stream-chat-android-offline:6.1.1"
implementation "io.getstream:stream-chat-android-compose:7.13.0"

implementation libs.androidxCoreKtx
implementation libs.androidxAppCompat
implementation libs.materialComponents

// Compose
implementation libs.composeCompiler
implementation libs.composeUi
implementation libs.composeUiTooling
implementation libs.composeFoundation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ package io.getstream.chat.android.compose.customattachments

import android.app.Application
import io.getstream.chat.android.client.ChatClient
import io.getstream.chat.android.client.api.ChatClientConfig
import io.getstream.chat.android.client.logger.ChatLogLevel
import io.getstream.chat.android.models.UploadAttachmentsNetworkType
import io.getstream.chat.android.models.User
import io.getstream.chat.android.offline.plugin.factory.StreamOfflinePluginFactory
import io.getstream.chat.android.state.plugin.config.StatePluginConfig
import io.getstream.chat.android.state.plugin.factory.StreamStatePluginFactory

class CustomAttachmentsApp : Application() {
override fun onCreate() {
Expand All @@ -41,18 +39,10 @@ class CustomAttachmentsApp : Application() {
}

private fun setupStreamSdk() {
val offlinePluginFactory = StreamOfflinePluginFactory(appContext = applicationContext)
val statePluginFactory = StreamStatePluginFactory(
config = StatePluginConfig(
backgroundSyncEnabled = true,
userPresence = true,
),
appContext = applicationContext,
)
ChatClient.Builder("qx5us2v6xvmh", applicationContext)
.logLevel(ChatLogLevel.ALL)
.uploadAttachmentsNetworkType(UploadAttachmentsNetworkType.NOT_ROAMING)
.withPlugins(offlinePluginFactory, statePluginFactory)
.config(ChatClientConfig(offlineEnabled = true, userPresence = true))
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ package io.getstream.chat.android.compose.customattachments.activity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import io.getstream.chat.android.compose.customattachments.ui.dateAttachmentFactory
import io.getstream.chat.android.compose.ui.attachments.StreamAttachmentFactories
import io.getstream.chat.android.compose.customattachments.ui.CustomChatComponentFactory
import io.getstream.chat.android.compose.ui.channels.ChannelsScreen
import io.getstream.chat.android.compose.ui.theme.ChatTheme

Expand All @@ -37,13 +36,10 @@ class ChannelsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val customFactories = listOf(dateAttachmentFactory)
val defaultFactories = StreamAttachmentFactories.defaultFactories()

setContent {
ChatTheme(attachmentFactories = customFactories + defaultFactories) {
ChatTheme(componentFactory = CustomChatComponentFactory) {
ChannelsScreen(
onItemClick = { channel ->
onChannelClick = { channel ->
startActivity(MessagesActivity.getIntent(this, channel.cid))
},
onBackPressed = { finish() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import io.getstream.chat.android.compose.customattachments.ui.CustomChatComponentFactory
import io.getstream.chat.android.compose.customattachments.ui.CustomMessagesScreen
import io.getstream.chat.android.compose.customattachments.ui.dateAttachmentFactory
import io.getstream.chat.android.compose.customattachments.ui.quotedDateAttachmentFactory
import io.getstream.chat.android.compose.ui.attachments.StreamAttachmentFactories
import io.getstream.chat.android.compose.ui.theme.ChatTheme

class MessagesActivity : AppCompatActivity() {
Expand All @@ -41,17 +39,8 @@ class MessagesActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
val channelId = requireNotNull(intent.getStringExtra(KEY_CHANNEL_ID))

val customFactories = listOf(dateAttachmentFactory)
val defaultFactories = StreamAttachmentFactories.defaultFactories()

val customQuotedFactories = listOf(quotedDateAttachmentFactory)
val defaultQuotedFactories = StreamAttachmentFactories.defaultQuotedFactories()

setContent {
ChatTheme(
attachmentFactories = customFactories + defaultFactories,
quotedAttachmentFactories = customQuotedFactories + defaultQuotedFactories
) {
ChatTheme(componentFactory = CustomChatComponentFactory) {
CustomMessagesScreen(
channelId = channelId,
onBackPressed = { finish() }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* The MIT License (MIT)
*
* Copyright 2022 Stream.IO, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.getstream.chat.android.compose.customattachments.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import io.getstream.chat.android.compose.customattachments.R
import io.getstream.chat.android.compose.ui.components.ComposerCancelIcon
import io.getstream.chat.android.compose.ui.theme.ChatComponentFactory
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.compose.ui.theme.CustomAttachmentContentParams
import io.getstream.chat.android.compose.ui.theme.MessageComposerAttachmentsParams
import io.getstream.chat.android.compose.ui.theme.MessageComposerLeadingContentParams
import io.getstream.chat.android.compose.ui.theme.MessageComposerQuotedMessageParams
import io.getstream.chat.android.compose.ui.theme.MessageQuotedContentParams

/**
* A custom [ChatComponentFactory] that adds support for date attachments.
*
* It renders date attachments in the message list, in the composer preview and in quoted
* messages, and replaces the composer's leading button with a calendar button.
*/
object CustomChatComponentFactory : ChatComponentFactory {

@Composable
override fun CustomAttachmentContent(params: CustomAttachmentContentParams) {
if (params.state.message.attachments.any { it.type == "date" }) {
DateAttachmentContent(
modifier = params.modifier,
attachmentState = params.state
)
}
}

@Composable
override fun MessageComposerAttachments(params: MessageComposerAttachmentsParams) {
val (dateAttachments, otherAttachments) = params.attachments.partition { it.type == "date" }

Column(modifier = params.modifier) {
if (dateAttachments.isNotEmpty()) {
DateAttachmentPreviewContent(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
attachments = dateAttachments,
onAttachmentRemoved = params.onAttachmentRemoved
)
}
if (otherAttachments.isNotEmpty()) {
super.MessageComposerAttachments(
params = params.copy(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
attachments = otherAttachments
)
)
}
}
}

@Composable
override fun MessageQuotedContent(params: MessageQuotedContentParams) {
if (params.message.attachments.any { it.type == "date" }) {
QuotedDateAttachmentContent(
modifier = params.modifier,
message = params.message
)
} else {
super.MessageQuotedContent(params)
}
}

@Composable
override fun MessageComposerQuotedMessage(params: MessageComposerQuotedMessageParams) {
if (params.quotedMessage.attachments.any { it.type == "date" }) {
Box(modifier = params.modifier.padding(horizontal = 8.dp)) {
QuotedDateAttachmentContent(message = params.quotedMessage)

ComposerCancelIcon(
modifier = Modifier
.align(Alignment.TopEnd)
.offset(x = 4.dp, y = (-4).dp),
onClick = params.onCancelClick
)
}
} else {
super.MessageComposerQuotedMessage(params)
}
}

@Composable
override fun MessageComposerLeadingContent(params: MessageComposerLeadingContentParams) {
IconButton(
modifier = params.modifier
.size(48.dp)
.padding(12.dp),
content = {
Icon(
painter = painterResource(id = R.drawable.ic_calendar),
contentDescription = null,
tint = ChatTheme.colors.textSecondary
)
},
onClick = params.onAttachmentsClick
)
}
}
Loading
Loading