Created
June 17, 2019 11:27
-
-
Save TobiAlbert/3e0b1cf7f770b20257f6fe10aabbe351 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.