Skip to content

Commit

Permalink
[PBE-3843] Implement Create Poll view on XML (#5361)
Browse files Browse the repository at this point in the history
* Move createPoll logic to MessageComposerController

* Add poll strings

* Add new attrs to enable/disable polls

* Create a View Model to handle the status of the poll creation process

* Create an adapter to render the different options

* Create a fragment to show options to create a poll

* Create a new listener to submit a poll

* Create new tab to create a poll

* Configure AttachmentPicker to show poll tabs only when it is enabled for the channel

* Configure ViewModel and View to send a poll

* Update CHANGELOG.md

---------

Co-authored-by: Kanat Kiialbaev <[email protected]>
  • Loading branch information
JcMinarro and kanat authored Aug 23, 2024
1 parent 134bef3 commit 839c19d
Show file tree
Hide file tree
Showing 35 changed files with 1,294 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

### ✅ Added
- Create a new UI for the poll message. [#5285](https://github.com/GetStream/stream-chat-android/pull/5285)
- Create a new UI to create a poll message. [#5361](https://github.com/GetStream/stream-chat-android/pull/5361)

### ⚠️ Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class MessagesActivity : BaseConnectedActivity() {
onTabClick = { _, tab -> isFullScreenContent = tab.isFullContent },
onAttachmentPickerAction = { action ->
if (action is AttachmentPickerPollCreation) {
listViewModel.createPoll(
composerViewModel.createPoll(
pollConfig = PollConfig(
name = action.question,
options = action.options.filter { it.title.isNotEmpty() }.map { it.title },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2703,6 +2703,7 @@ public final class io/getstream/chat/android/compose/viewmodel/messages/MessageC
public final fun buildNewMessage (Ljava/lang/String;Ljava/util/List;)Lio/getstream/chat/android/models/Message;
public static synthetic fun buildNewMessage$default (Lio/getstream/chat/android/compose/viewmodel/messages/MessageComposerViewModel;Ljava/lang/String;Ljava/util/List;ILjava/lang/Object;)Lio/getstream/chat/android/models/Message;
public final fun clearData ()V
public final fun createPoll (Lio/getstream/chat/android/models/PollConfig;)V
public final fun dismissMessageActions ()V
public final fun getAlsoSendToChannel ()Lkotlinx/coroutines/flow/MutableStateFlow;
public final fun getCommandSuggestions ()Lkotlinx/coroutines/flow/MutableStateFlow;
Expand Down Expand Up @@ -2738,7 +2739,6 @@ public final class io/getstream/chat/android/compose/viewmodel/messages/MessageL
public final fun castVote (Lio/getstream/chat/android/models/Message;Lio/getstream/chat/android/models/Poll;Lio/getstream/chat/android/models/Option;)V
public final fun clearNewMessageState ()V
public final fun closePoll (Ljava/lang/String;)V
public final fun createPoll (Lio/getstream/chat/android/models/PollConfig;)V
public final fun deleteMessage (Lio/getstream/chat/android/models/Message;)V
public final fun deleteMessage (Lio/getstream/chat/android/models/Message;Z)V
public static synthetic fun deleteMessage$default (Lio/getstream/chat/android/compose/viewmodel/messages/MessageListViewModel;Lio/getstream/chat/android/models/Message;ZILjava/lang/Object;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ private fun BoxScope.AttachmentsPickerMenu(
},
onAttachmentPickerAction = { action ->
if (action is AttachmentPickerPollCreation) {
listViewModel.createPoll(
composerViewModel.createPoll(
pollConfig = PollConfig(
name = action.question,
options = action.options.filter { it.title.isNotEmpty() }.map { it.title },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.getstream.chat.android.models.ChannelCapabilities
import io.getstream.chat.android.models.Command
import io.getstream.chat.android.models.LinkPreview
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.PollConfig
import io.getstream.chat.android.models.User
import io.getstream.chat.android.ui.common.feature.messages.composer.MessageComposerController
import io.getstream.chat.android.ui.common.state.messages.Edit
Expand Down Expand Up @@ -171,6 +172,15 @@ public class MessageComposerViewModel(
public fun removeSelectedAttachment(attachment: Attachment): Unit =
messageComposerController.removeSelectedAttachment(attachment)

/**
* Creates a poll with the given [pollConfig].
*
* @param pollConfig Configuration for creating a poll.
*/
public fun createPoll(pollConfig: PollConfig) {
messageComposerController.createPoll(pollConfig = pollConfig)
}

/**
* Sends a given message using our Stream API. Based on the internal state, we either edit an existing message,
* or we send a new message, using our API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import io.getstream.chat.android.models.ConnectionState
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.Option
import io.getstream.chat.android.models.Poll
import io.getstream.chat.android.models.PollConfig
import io.getstream.chat.android.models.User
import io.getstream.chat.android.models.Vote
import io.getstream.chat.android.state.plugin.state.channel.thread.ThreadState
Expand Down Expand Up @@ -433,15 +432,6 @@ public class MessageListViewModel(
messageListController.performGiphyAction(action)
}

/**
* Creates a poll with the given [pollConfig].
*
* @param pollConfig Configuration for creating a poll.
*/
public fun createPoll(pollConfig: PollConfig) {
messageListController.createPoll(pollConfig = pollConfig)
}

/**
* Cast a vote for a poll in a message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ public final class io/getstream/chat/android/ui/common/feature/messages/list/Mes
public final fun clearNewMessageState ()V
public final fun closePoll (Lio/getstream/chat/android/models/Poll;)V
public final fun closePoll (Ljava/lang/String;)V
public final fun createPoll (Lio/getstream/chat/android/models/PollConfig;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun createPoll$default (Lio/getstream/chat/android/ui/common/feature/messages/list/MessageListController;Lio/getstream/chat/android/models/PollConfig;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public final fun deleteMessage (Lio/getstream/chat/android/models/Message;Z)V
public static synthetic fun deleteMessage$default (Lio/getstream/chat/android/ui/common/feature/messages/list/MessageListController;Lio/getstream/chat/android/models/Message;ZILjava/lang/Object;)V
public final fun disableUnreadLabelButton ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import io.getstream.chat.android.models.ChannelCapabilities
import io.getstream.chat.android.models.Command
import io.getstream.chat.android.models.LinkPreview
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.PollConfig
import io.getstream.chat.android.models.User
import io.getstream.chat.android.ui.common.feature.messages.composer.mention.UserLookupHandler
import io.getstream.chat.android.ui.common.feature.messages.composer.typing.TypingSuggester
Expand Down Expand Up @@ -559,6 +560,23 @@ public class MessageComposerController(
handleValidationErrors()
}

/**
* Creates a poll with the given [pollConfig].
*
* @param pollConfig Configuration for creating a poll.
*/
public fun createPoll(pollConfig: PollConfig, onResult: (Result<Message>) -> Unit = {}) {
channelCid.cidToTypeAndId().let { (channelType, channelId) ->
chatClient.sendPoll(
channelType = channelType,
channelId = channelId,
pollConfig = pollConfig,
).enqueue { response ->
onResult(response)
}
}
}

/**
* Clears all the data from the input - both the current [input] value and the
* [selectedAttachments].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.MessagesState
import io.getstream.chat.android.models.Option
import io.getstream.chat.android.models.Poll
import io.getstream.chat.android.models.PollConfig
import io.getstream.chat.android.models.Reaction
import io.getstream.chat.android.models.User
import io.getstream.chat.android.models.Vote
Expand Down Expand Up @@ -1753,28 +1752,6 @@ public class MessageListController(
})
}

/**
* Creates a poll with the given [pollConfig].
*
* @param pollConfig Configuration for creating a poll.
*/
public fun createPoll(pollConfig: PollConfig, onResult: (Result<Message>) -> Unit = {}) {
cid.cidToTypeAndId().let { (channelType, channelId) ->
chatClient.sendPoll(
channelType = channelType,
channelId = channelId,
pollConfig = pollConfig,
).enqueue { response ->
onResult(response)
if (response is Result.Failure) {
onActionResult(response.value) {
ErrorEvent.PollCreationError(it)
}
}
}
}
}

/**
* Cast a vote for a poll in a message.
*
Expand Down
Loading

0 comments on commit 839c19d

Please sign in to comment.