Skip to content

Commit

Permalink
Add timestamp to multiplayer chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Jan 12, 2025
1 parent 0e17608 commit c0a2792
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 12 additions & 0 deletions res/layout/multiplayer_room_chat_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
android:gravity="right"
android:paddingHorizontal="6dp">

<TextView
android:id="@+id/timestamp_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="right"
android:singleLine="true"
android:textColor="#ABABAB"
android:textSize="12sp"
tools:text="Timestamp"
tools:visibility="visible" />

<TextView
android:id="@+id/sender_text"
android:layout_width="160dp"
Expand Down
15 changes: 12 additions & 3 deletions src/com/reco1l/osu/multiplayer/RoomChat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.reco1l.ibancho.data.RoomPlayer
import com.reco1l.osu.mainThread
import com.reco1l.toolkt.android.*
import com.reco1l.toolkt.kotlin.async
import java.text.SimpleDateFormat
import org.anddev.andengine.input.touch.TouchEvent
import ru.nsu.ccfit.zuev.osu.GlobalManager
import ru.nsu.ccfit.zuev.osu.RGBColor
Expand Down Expand Up @@ -290,6 +291,8 @@ class RoomChat : BaseFragment(), OnEditorActionListener, OnKeyListener {


data class Message(val senderUid: Long?, val text: String, val color: Int? = null) {
val timestamp = System.currentTimeMillis()

val senderUsername =
if (senderUid == null) "System"
else Multiplayer.room?.playersMap?.get(senderUid)?.name ?: "Unknown Player ($senderUid)"
Expand Down Expand Up @@ -333,18 +336,23 @@ class MessageAdapter : RecyclerView.Adapter<MessageViewHolder>() {
class MessageViewHolder(private val root: LinearLayout) : RecyclerView.ViewHolder(root) {


private lateinit var timestampText: TextView

private lateinit var senderText: TextView

private lateinit var messageText: TextView


fun bind(msg: Message, showSender: Boolean, tintBackground: Boolean) {

timestampText = root.findViewById(R.id.timestamp_text)!!
senderText = root.findViewById(R.id.sender_text)!!
messageText = root.findViewById(R.id.message_text)!!

root.backgroundColor = if (tintBackground) 0xFF1A1A25.toInt() else Color.TRANSPARENT

timestampText.text = timestampFormatter.format(msg.timestamp)

messageText.verticalPadding = 0
messageText.gravity = Gravity.LEFT
messageText.fontColor = msg.color ?: Color.WHITE

Expand All @@ -360,8 +368,6 @@ class MessageViewHolder(private val root: LinearLayout) : RecyclerView.ViewHolde
return
}

messageText.verticalPadding = 0.dp

if (showSender) {

val isRoomHost = msg.senderUid == Multiplayer.room!!.host
Expand Down Expand Up @@ -390,4 +396,7 @@ class MessageViewHolder(private val root: LinearLayout) : RecyclerView.ViewHolde
messageText.text = msg.text
}

companion object {
private val timestampFormatter = SimpleDateFormat("HH:mm:ss")
}
}

0 comments on commit c0a2792

Please sign in to comment.