Skip to content

Commit

Permalink
[AND-302] menu item stateless components (#5608)
Browse files Browse the repository at this point in the history
* Introduce factory for menu items channel menu, message menu and reactions menu.

* Api Dump and default values changes

* Spotless

* Merge conflict and api & spotless

* Fix lint

* Fix default height of message menu item

* Do not break the old composables by not exposing the parameters

* Spotless & Api Dump

* Make MessageOptionItemDeprecated

* Fix merge issues

* Remove padding declaration in the factory.

* Rename the public function WITHOUT `Generic`

* ApiDump

* spotless

* Fix padding
  • Loading branch information
aleksandar-apostolov authored Feb 5, 2025
1 parent bf50b25 commit 582e8b8
Show file tree
Hide file tree
Showing 15 changed files with 918 additions and 504 deletions.
77 changes: 60 additions & 17 deletions stream-chat-android-compose/api/stream-chat-android-compose.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import io.getstream.chat.android.compose.R
import io.getstream.chat.android.compose.state.channels.list.SearchQuery
import io.getstream.chat.android.compose.ui.channels.info.SelectedChannelMenu
import io.getstream.chat.android.compose.ui.channels.list.ChannelList
import io.getstream.chat.android.compose.ui.components.SearchInput
import io.getstream.chat.android.compose.ui.components.SimpleDialog
Expand Down Expand Up @@ -189,7 +188,7 @@ public fun ChannelsScreen(
enter = fadeIn(),
exit = fadeOut(animationSpec = tween(durationMillis = AnimationConstants.DefaultDurationMillis / 2)),
) {
SelectedChannelMenu(
ChatTheme.componentFactory.ChannelMenu(
modifier = Modifier
.align(Alignment.BottomCenter)
.animateEnterExit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import androidx.compose.ui.unit.dp
import io.getstream.chat.android.compose.state.channels.list.ChannelOptionState
import io.getstream.chat.android.compose.ui.components.SimpleMenu
import io.getstream.chat.android.compose.ui.components.channels.ChannelMembers
import io.getstream.chat.android.compose.ui.components.channels.ChannelOptions
import io.getstream.chat.android.compose.ui.components.channels.buildDefaultChannelOptionsState
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.compose.ui.util.getMembersStatusText
Expand Down Expand Up @@ -79,16 +78,22 @@ public fun SelectedChannelMenu(
shape: Shape = ChatTheme.shapes.bottomSheet,
overlayColor: Color = ChatTheme.colors.overlay,
headerContent: @Composable ColumnScope.() -> Unit = {
DefaultSelectedChannelMenuHeaderContent(
selectedChannel = selectedChannel,
currentUser = currentUser,
)
with(ChatTheme.componentFactory) {
ChannelMenuHeaderContent(
modifier = Modifier,
selectedChannel = selectedChannel,
currentUser = currentUser,
)
}
},
centerContent: @Composable ColumnScope.() -> Unit = {
DefaultSelectedChannelMenuCenterContent(
onChannelOptionClick = onChannelOptionClick,
channelOptions = channelOptions,
)
with(ChatTheme.componentFactory) {
ChannelMenuCenterContent(
modifier = Modifier,
onChannelOptionClick = onChannelOptionClick,
channelOptions = channelOptions,
)
}
},
) {
SimpleMenu(
Expand Down Expand Up @@ -141,20 +146,6 @@ internal fun DefaultSelectedChannelMenuHeaderContent(
ChannelMembers(membersToDisplay)
}

/**
* Represents the default content shown at the center of [SelectedChannelMenu] dialog.
*
* @param onChannelOptionClick Handler for when the user selects a channel option.
* @param channelOptions The list of options to show in the UI, according to user permissions.
*/
@Composable
internal fun DefaultSelectedChannelMenuCenterContent(
onChannelOptionClick: (ChannelAction) -> Unit,
channelOptions: List<ChannelOptionState>,
) {
ChannelOptions(channelOptions, onChannelOptionClick)
}

/**
* Preview of [SelectedChannelMenu] styled as a centered modal dialog.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
Expand Down Expand Up @@ -81,21 +78,13 @@ public fun ChannelOptions(
.background(color = ChatTheme.colors.borders),
)

ChannelOptionsItem(
title = option.title,
titleColor = option.titleColor,
leadingIcon = {
Icon(
modifier = Modifier
.size(56.dp)
.padding(16.dp),
painter = option.iconPainter,
tint = option.iconColor,
contentDescription = null,
)
},
onClick = { onChannelOptionClick(option.action) },
)
with(ChatTheme.componentFactory) {
ChannelOptionsItem(
modifier = Modifier,
option = option,
onClick = { onChannelOptionClick(option.action) },
)
}
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.chat.android.compose.ui.components.common

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Text
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import io.getstream.chat.android.compose.ui.theme.ChatTheme

/**
* Represents a generic menu option item that can be used in a list of options.
*
* @param modifier Modifier for styling.
* @param onClick Handler called when the item is clicked.
* @param leadingIcon The icon to show on the left side of the item.
* @param title The title of the item.
* @param titleColor The color of the title.
* @param style The style of the title.
* @param itemHeight The height of the item.
* @param verticalAlignment Used to apply vertical alignment.
* @param horizontalArrangement Used to apply horizontal arrangement.
*/
@Composable
public fun MenuOptionItem(
modifier: Modifier = Modifier,
onClick: () -> Unit,
leadingIcon: @Composable RowScope.() -> Unit,
title: String,
titleColor: Color,
style: TextStyle = ChatTheme.typography.bodyBold,
itemHeight: Dp = 56.dp,
verticalAlignment: Alignment.Vertical = Alignment.CenterVertically,
horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
) {
Row(
modifier
.fillMaxWidth()
.height(itemHeight)
.clickable(
onClick = onClick,
indication = ripple(),
interactionSource = remember { MutableInteractionSource() },
),
verticalAlignment = verticalAlignment,
horizontalArrangement = horizontalArrangement,
) {
leadingIcon()
Text(
modifier = Modifier.testTag("Stream_ContextMenu_$title"),
text = title,
style = style,
color = titleColor,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@
package io.getstream.chat.android.compose.ui.components.messageoptions

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.getstream.chat.android.compose.R
import io.getstream.chat.android.compose.state.messageoptions.MessageOptionItemState
import io.getstream.chat.android.compose.ui.components.common.MenuOptionItem
import io.getstream.chat.android.compose.ui.theme.ChatTheme
import io.getstream.chat.android.previewdata.PreviewMessageData
import io.getstream.chat.android.ui.common.state.messages.Reply
Expand All @@ -45,33 +43,37 @@ import io.getstream.chat.android.ui.common.state.messages.Reply
* @param horizontalArrangement Used to apply horizontal arrangement.
*/
@Composable
@Deprecated(
message = "This class is deprecated and will be removed in the next version.",
replaceWith = ReplaceWith("GenericMenuOptionItem"),
level = DeprecationLevel.WARNING,
)
public fun MessageOptionItem(
option: MessageOptionItemState,
modifier: Modifier = Modifier,
verticalAlignment: Alignment.Vertical = Alignment.CenterVertically,
horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
onMessageOptionItemSelected: (MessageOptionItemState) -> Unit = {},
) {
val title = stringResource(id = option.title)

Row(
MenuOptionItem(
modifier = modifier,
title = title,
titleColor = option.titleColor,
leadingIcon = {
Icon(
modifier = Modifier.padding(horizontal = 16.dp),
painter = option.iconPainter,
tint = option.iconColor,
contentDescription = title,
)
},
onClick = { onMessageOptionItemSelected(option) },
style = ChatTheme.typography.body,
itemHeight = 56.dp,
verticalAlignment = verticalAlignment,
horizontalArrangement = horizontalArrangement,
) {
Icon(
modifier = Modifier.padding(horizontal = 16.dp),
painter = option.iconPainter,
tint = option.iconColor,
contentDescription = title,
)

Text(
modifier = Modifier.testTag("Stream_ContextMenu_$title"),
text = title,
style = ChatTheme.typography.body,
color = option.titleColor,
)
}
)
}

/**
Expand Down
Loading

0 comments on commit 582e8b8

Please sign in to comment.