Reputation: 1532
I'm trying to view all already joined rooms in pusher chatkit recycler view however it doesn't seem to be working. It only loads one room after I scroll. When I open the activity, it shows blank. Also, when I click on the room, the intent isn't started, it just stays on the recycler view. It should be going to the selected chat room.
Through Debug, I'm able to see that I am connecting to the accessible rooms(there's 7 of them) however it only seems like one of them is being added to the adapter/recyclerview however I can't find where it is falling short. I am relatively new to kotlin which probably adds to the issue.
Here is the tutorial I am following which I thought would be relatively good to go out of the box but it seems there are some snags.
any and all help is appreciated.
ChatRoomsListActivity
class ChatRoomsListActivity : AppCompatActivity() {
val adapter = ChatRoomsListAdapter();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat_room_list)
initRecyclerView()
initChatManager()
}
private fun initRecyclerView() {
recycler_view.layoutManager = LinearLayoutManager(this@ChatRoomsListActivity)
recycler_view.adapter = adapter
/*
recycler_view.apply {
val topSpacingDecorator = TopSpacingItemDecoration(30)
addItemDecoration(topSpacingDecorator)
recycler_view.layoutManager = LinearLayoutManager(this@ChatRoomsListActivity)
recycler_view.adapter = adapter
adapter = ChatRoomsListAdapter();
adapter = adapter
}
*/
}
private fun initChatManager() {
val chatManager = ChatManager(
instanceLocator = "xxxxxxxxxxxxxx",
userId = "username1-PCKid",
dependencies = AndroidChatkitDependencies(
tokenProvider = ChatkitTokenProvider(
endpoint = "yyyyyyyyyyyyyyyyyyy",
// endpoint = "http://10.0.2.2:3000/auth",
userId = "username1-PCKid"
)
)
)
chatManager.connect(listeners = ChatListeners(
onErrorOccurred = { },
onAddedToRoom = { },
onRemovedFromRoom = { },
onCurrentUserReceived = { },
onNewReadCursor = { },
onRoomDeleted = { },
onRoomUpdated = { },
onPresenceChanged = { u, n, p -> },
onUserJoinedRoom = { u, r -> },
onUserLeftRoom = { u, r -> },
onUserStartedTyping = { u, r -> },
onUserStoppedTyping = { u, r -> }
)) { result ->
when (result) {
is Result.Success -> {
// We have connected!
val currentUser = result.value
AppController.currentUser = currentUser
val userJoinedRooms = ArrayList<Room>(currentUser.rooms)
for (i in 0 until userJoinedRooms.size) {
adapter.addRoom(userJoinedRooms[i]) // reads users rooms
}
currentUser.getJoinableRooms { result ->
when (result) {
is Result.Success -> {
// Do something with List<Room>
val rooms = result.value
runOnUiThread {
for (i in 0 until rooms.size) {
adapter.addRoom(rooms[i])
}
}
}
}
}
adapter.setInterface(object : ChatRoomsListAdapter.RoomClickedInterface {
override fun roomSelected(room: Room) {
if (room.memberUserIds.contains(currentUser.id)) {
// user already belongs to this room
roomJoined(room)
Log.d("roomSelected", "user already belongs to this room: " + roomJoined(room))
} else {
currentUser.joinRoom(
roomId = room.id,
callback = { result ->
when (result) {
is Result.Success -> {
// Joined the room!
roomJoined(result.value)
}
is Result.Failure -> {
Log.d("TAG", result.error.toString())
}
}
}
)
}
}
})
}
is Result.Failure -> {
// Failure
Log.d("TAG", result.error.toString())
}
}
}
}
private fun roomJoined(room: Room) {
val intent = Intent(this@ChatRoomsListActivity, ChatRoomActivity::class.java)
intent.putExtra("room_id", room.id)
intent.putExtra("room_name", room.name)
startActivity(intent)
}
}
ChatRoomsListAdapter
class ChatRoomsListAdapter: RecyclerView.Adapter<ChatRoomsListAdapter.ViewHolder>() {
private var list = ArrayList<Room>()
private var roomClickedInterface: RoomClickedInterface? = null
// lateinit var roomClickedInterface:RoomClickedInterface
fun addRoom(room:Room){
list.add(room)
notifyDataSetChanged()
// Log.d("Rooms", room.toString())
}
fun setInterface(roomClickedInterface:RoomClickedInterface){
this.roomClickedInterface = roomClickedInterface
// roomClickedInterface = roomClickedInterface
}
override fun getItemCount(): Int {
return list.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(
// R.layout.chat_list_row,
android.R.layout.simple_list_item_1,
parent,
false
)
return ViewHolder(view)
}
/*
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent!!.context)
.inflate(
android.R.layout.simple_list_item_1,
parent,
false
)
return ViewHolder(view)
}
*/
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.roomName.text = list[position].name
}
/*
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
holder!!.roomName.text = list[position].name
}
*/
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView), View.OnClickListener {
override fun onClick(p0: View?) {
roomClickedInterface?.roomSelected(list[adapterPosition])
Toast.makeText(itemView.context, "hello test", Toast.LENGTH_LONG).show();
Log.d("test", "testing console log")
}
var roomName: TextView = itemView.findViewById(android.R.id.text1)
init {
itemView.setOnClickListener(this)
}
}
interface RoomClickedInterface{
fun roomSelected(room:Room)
}
}
Rooms that should be shown in Recyclerview 1)
this is what it looks like after i scroll any direction 3)
Upvotes: 0
Views: 58
Reputation: 4091
You need to ensure you are calling adapter.notifyDataSetChanged(). So change
currentUser.getJoinableRooms { result ->
when (result) {
is Result.Success -> {
// Do something with List<Room>
val rooms = result.value
runOnUiThread {
for (i in 0 until rooms.size) {
adapter.addRoom(rooms[i])
}
}
}
}
to
currentUser.getJoinableRooms { result ->
when (result) {
is Result.Success -> {
// Do something with List<Room>
val rooms = result.value
runOnUiThread {
for (i in 0 until rooms.size) {
adapter.addRoom(rooms[i])
}
adapter.notifyDataSetChanged()
}
}
}
Upvotes: 0