Skip to content

Instantly share code, notes, and snippets.

View LiveStreamActivity.kt
private fun updateMessagesList(messages: List<Message>) {
adapter.submitList(messages)
adapter.notifyDataSetChanged()
val scrollTarget = adapter.itemCount
messageListSmoothScroller.targetPosition = scrollTarget
messagesList.layoutManager?.startSmoothScroll(messageListSmoothScroller)
}
View LiveStreamActivity.kt
class LiveStreamActivity : AppCompatActivity(R.layout.activity_main) {
private val adapter = MessagesListAdapter()
private val messageListSmoothScroller by lazy {
object : LinearSmoothScroller(this) {
val MILLISECONDS_PER_INCH = 400f
override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics?): Float {
return MILLISECONDS_PER_INCH / displayMetrics!!.densityDpi
}
View LiveStreamActivity.kt
class LiveStreamActivity : AppCompatActivity(R.layout.activity_main) {
private val adapter = MessagesListAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loadMockVideoStream()
messagesList.adapter = adapter
val viewModel: LiveStreamViewModel by viewModels()
View LiveStreamViewModel.kt
private fun requestChannel() {
val channelData = mapOf("name" to "Live stream chat")
val request = QueryChannelRequest()
.withData(channelData)
.withMessages(20)
.withWatch()
channelController.query(request).enqueue {
if (it.isSuccess) {
_viewState.postValue(State.Messages(it.data().messages))
View LiveStreamViewModel.kt
companion object {
private const val USER_ID = "bob"
private const val CHANNEL_TYPE = "livestream"
private const val CHANNEL_ID = "livestream-clone-android" // You'll want to make it unique per video
private const val USER_TOKEN = BuildConfig.USER_TOKEN
private val chatUser = User(id = USER_ID).apply {
name = USER_ID
image = getDummyAvatar(USER_ID)
}
View LiveStreamViewModel.kt
class LiveStreamViewModel() : ViewModel() {
private val chatClient = ChatClient.instance()
private val _viewState = MutableLiveData<State>()
private lateinit var channelController: ChannelController
val viewState: LiveData<State> = _viewState
init {
chatClient.setUser(chatUser, USER_TOKEN, object : InitConnectionListener() {
View LiveStreamActivity.kt
class LiveStreamActivity : AppCompatActivity(R.layout.activity_main) {
private val adapter = MessagesListAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loadMockVideoStream()
messagesList.adapter = adapter
val viewModel: LiveStreamViewModel by viewModels()
View item_message.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/avatarWrapper"
android:layout_width="@dimen/avatar_size"
View MessagesListAdapter.kt
package io.getstream.livestreamclone
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import io.getstream.chat.android.client.models.Message
import kotlinx.android.synthetic.main.item_message.view.*
@samiuelson
samiuelson / activity_main.xml
Created May 6, 2020
Add chat list widget with translucent gradient
View activity_main.xml
...
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView ... />
<!-- Translucent gradient overlay background to make chat more visible -->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/bg_chat"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@+id/messagesList" />
You can’t perform that action at this time.