Commit 611b0086 authored by Shailesh Baldaniya's avatar Shailesh Baldaniya

feat: Add horizontal and vertical buttons

parent 40004009
package chat.rocket.android.chatroom.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import chat.rocket.android.chatroom.uimodel.ActionsAttachmentUiModel
import chat.rocket.android.emoji.EmojiReactionListener
import chat.rocket.core.model.attachment.actions.Action
import chat.rocket.core.model.attachment.actions.ButtonAction
import kotlinx.android.synthetic.main.item_actions_attachment.view.*
import androidx.recyclerview.widget.LinearLayoutManager
import chat.rocket.android.R
import chat.rocket.android.util.TimberLogger
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.view.SimpleDraweeView
import com.google.android.material.button.MaterialButton
import timber.log.Timber
class ActionsAttachmentViewHolder(
itemView: View,
......@@ -31,75 +24,21 @@ class ActionsAttachmentViewHolder(
override fun bindViews(data: ActionsAttachmentUiModel) {
val actions = data.actions
TimberLogger.debug("no of actions : ${actions.size} : $actions")
val alignment = data.buttonAlignment
Timber.d("no of actions : ${actions.size} : $actions")
with(itemView) {
title.text = data.title ?: ""
actions_list.layoutManager = LinearLayoutManager(itemView.context)
actions_list.layoutManager = LinearLayoutManager(itemView.context,
when (alignment) {
"vertical" -> LinearLayoutManager.VERTICAL
"horizontal" -> LinearLayoutManager.HORIZONTAL
else -> LinearLayoutManager.VERTICAL //Default
}, false)
actions_list.adapter = ActionsListAdapter(actions, actionAttachmentOnClickListener)
}
}
}
interface ActionAttachmentOnClickListener {
fun onActionClicked(action: Action)
}
class ActionsListAdapter(actions: List<Action>, var actionAttachmentOnClickListener: ActionAttachmentOnClickListener) : RecyclerView.Adapter<ActionsListAdapter.ViewHolder>() {
var actions: List<Action> = actions
inner class ViewHolder(var layout: View) : RecyclerView.ViewHolder(layout) {
lateinit var action: ButtonAction
var button: MaterialButton = layout.findViewById(R.id.action_button)
val image: SimpleDraweeView = layout.findViewById(R.id.action_image_button)
private val onClickListener = View.OnClickListener {
actionAttachmentOnClickListener.onActionClicked(action)
}
init {
button.setOnClickListener(onClickListener)
image.setOnClickListener(onClickListener)
}
fun bindAction(action: Action) {
TimberLogger.debug("action : $action")
this.action = action as ButtonAction
//TODO
if (action.imageUrl != null) {
button.visibility = View.GONE
image.visibility = View.VISIBLE
//Image button
val controller = Fresco.newDraweeControllerBuilder().apply {
setUri(action.imageUrl)
autoPlayAnimations = true
oldController = image.controller
}.build()
image.controller = controller
} else if (action.text != null) {
button.visibility = View.VISIBLE
image.visibility = View.GONE
this.button.setText(action.text)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_action_button, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return actions.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val action = actions[position]
holder.bindAction(action)
}
fun onActionClicked(view: View, action: Action)
}
\ No newline at end of file
package chat.rocket.android.chatroom.adapter
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import chat.rocket.android.R
import chat.rocket.android.util.extensions.inflate
import chat.rocket.core.model.attachment.actions.Action
import chat.rocket.core.model.attachment.actions.ButtonAction
import com.facebook.drawee.backends.pipeline.Fresco
import kotlinx.android.synthetic.main.item_action_button.view.*
import timber.log.Timber
class ActionsListAdapter(actions: List<Action>, var actionAttachmentOnClickListener: ActionAttachmentOnClickListener) : RecyclerView.Adapter<ActionsListAdapter.ViewHolder>() {
var actions: List<Action> = actions
inner class ViewHolder(var layout: View) : RecyclerView.ViewHolder(layout) {
lateinit var action: ButtonAction
private val onClickListener = View.OnClickListener {
actionAttachmentOnClickListener.onActionClicked(it, action)
}
init {
with(itemView) {
action_button.setOnClickListener(onClickListener)
action_image_button.setOnClickListener(onClickListener)
}
}
fun bindAction(action: Action) {
with(itemView) {
Timber.d("action : $action")
this@ViewHolder.action = action as ButtonAction
if (action.imageUrl != null) {
action_button.isVisible = false
action_image_button.isVisible = true
//Image button
val controller = Fresco.newDraweeControllerBuilder().apply {
setUri(action.imageUrl)
autoPlayAnimations = true
oldController = action_image_button.controller
}.build()
action_image_button.controller = controller
} else if (action.text != null) {
action_button.isVisible = true
action_image_button.isVisible = false
this.action_button.setText(action.text)
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = parent.inflate(R.layout.item_action_button)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return actions.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val action = actions[position]
holder.bindAction(action)
}
}
\ No newline at end of file
......@@ -5,10 +5,8 @@ import android.content.Context
import android.net.Uri
import androidx.recyclerview.widget.RecyclerView
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.res.ResourcesCompat
import chat.rocket.android.R
import chat.rocket.android.chatroom.presentation.ChatRoomPresenter
import chat.rocket.android.chatroom.uimodel.*
......@@ -219,24 +217,15 @@ class ChatRoomAdapter(
}
private val actionAttachmentOnClickListener = object : ActionAttachmentOnClickListener {
override fun onActionClicked(action: Action) {
override fun onActionClicked(view: View, action: Action) {
val temp = action as ButtonAction
if (temp.url != null && temp.isWebView != null) {
if (temp.isWebView!!) {
//Open in a configurable sizable webview
Toast.makeText(context, "Open in a configurable sizable webview", Toast.LENGTH_SHORT).show()
//TODO: Open in a configurable sizable webview
Timber.d("Open in a configurable sizable webview")
} else {
//Open in chrome custom tab
with(this) {
val customTabsBuilder = CustomTabsIntent.Builder()
customTabsBuilder.setToolbarColor(ResourcesCompat.getColor(context!!.resources, R.color.colorPrimary, context.theme))
val customTabsIntent = customTabsBuilder.build()
try {
customTabsIntent.launchUrl(context, Uri.parse(temp.url!!))
} catch (ex: Exception) {
Timber.d(ex, "Unable to launch URL")
}
}
view.openTabbedUrl(Uri.parse(temp.url))
}
} else if (temp.message != null && temp.isMessageInChatWindow != null) {
if (temp.isMessageInChatWindow!!) {
......@@ -247,8 +236,8 @@ class ChatRoomAdapter(
}
}
} else {
//Send to bot but not in chat window
Toast.makeText(context, "Send to bot but not in chat window", Toast.LENGTH_SHORT).show()
//TODO: Send to bot but not in chat window
Timber.d("Send to bot but not in chat window")
}
}
}
......
......@@ -9,6 +9,7 @@ data class ActionsAttachmentUiModel(
override val attachmentUrl: String,
val title: String?,
val actions: List<Action>,
val buttonAlignment: String,
override val message: Message,
override val rawData: ActionsAttachment,
override val messageId: String,
......
......@@ -319,7 +319,7 @@ class UiModelMapper @Inject constructor(
val dayMarkerText = DateTimeHelper.getFormattedDateForMessages(localDateTime, context)
ActionsAttachmentUiModel(attachmentUrl = url, title = title,
actions = actions, message = message, rawData = attachment,
actions = actions, buttonAlignment = buttonAlignment, message = message, rawData = attachment,
messageId = message.id, reactions = getReactions(message),
preview = message.copy(message = content.message), unread = message.unread,
showDayMarker = false, currentDayMarkerText = dayMarkerText)
......
......@@ -6,18 +6,22 @@
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button"
android:id="@+id/action_button"
android:textSize="12sp"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginStart="2dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textSize="12sp" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/action_image_button"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginBottom="10dp"
android:layout_marginEnd="2dp"
android:layout_marginStart="2dp"
android:visibility="gone"
fresco:actualImageScaleType="fitStart"
fresco:placeholderImage="@drawable/image_dummy" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment