Skip to content

Instantly share code, notes, and snippets.

@TobiAlbert
Created June 17, 2019 11:27
Show Gist options
  • Save TobiAlbert/3e0b1cf7f770b20257f6fe10aabbe351 to your computer and use it in GitHub Desktop.
Save TobiAlbert/3e0b1cf7f770b20257f6fe10aabbe351 to your computer and use it in GitHub Desktop.
class RoomActivity: AppCompatActivity() {
private lateinit var mAdapter: RoomAdapter
private val userManager: UserManager by inject() // koin user injection
override fun onCreate(savedInstance: Bundle?) {
super.onCreate(savedInstance)
mAdapter = RoomAdapter(userManager.currentUser.id)
// recycler view in layout
room_list_chat.apply {
adapter = mAdapter
layoutManager = LinearLayoutManager(this@RoomActivity).also { it.stackFromEnd = true }
}
// subscribe to room; getting all messages
val messageEvent = { message: Message -> mAdapter.addMessage(message) }
val errorEvent = { error: elements.Error -> makeToast(error.reason) }
val roomListeners = RoomListeners(
onMultipartMessage = messageEvent,
onErrorOccurred = errorEvent
)
val messageCallback: (Subscription) -> Unit = {
runOnUiThread {
mAdapter.notifyDataSetChanged()
}
}
if (room.id.isNotEmpty()) {
currentUser?.subscribeToRoomMultipart(
roomId = room.id,
messageLimit = 30,
listeners = roomListeners,
callback = messageCallback
)
}
}
}
@TobiAlbert
Copy link
Author

Initially, I did not include the runOnUiThread function but added it because it seemed to display the messages without the user touching the screen, but it doesn't display all the messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment