Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
fix: Members Endless RecyclerView
Browse files Browse the repository at this point in the history
  • Loading branch information
Shailesh351 committed Jun 24, 2019
1 parent e7e2866 commit f87f265
Showing 3 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -31,23 +31,21 @@ class MembersPresenter @Inject constructor(
private val userHelper: UserHelper
) {
private val client: RocketChatClient = factory.get(currentServer)
private var offset: Long = 0

/**
* Loads all the chat room members for the given room id.
*
* @param roomId The id of the room to get chat room members from.
*/
fun loadChatRoomsMembers(roomId: String) {
fun loadChatRoomsMembers(roomId: String, offset: Long = 0, clearDataset: Boolean = false) {
launchUI(strategy) {
try {
view.showLoading()
dbManager.getRoom(roomId)?.let {
val members =
client.getMembers(roomId, roomTypeOf(it.chatRoom.type), offset, 60)
val memberUiModels = mapper.mapToUiModelList(members.result)
view.showMembers(memberUiModels, members.total)
offset += 1 * 60L
view.showMembers(memberUiModels, members.total, clearDataset)
}.ifNull {
Timber.e("Couldn't find a room with id: $roomId at current server.")
}
Original file line number Diff line number Diff line change
@@ -11,8 +11,9 @@ interface MembersView: LoadingView, MessageView {
*
* @param dataSet The data set to show.
* @param total The total number of members.
* @param clearDataSet If true it will clear the previous data set.
*/
fun showMembers(dataSet: List<MemberUiModel>, total: Long)
fun showMembers(dataSet: List<MemberUiModel>, total: Long, clearDataset: Boolean)

/**
* Show Invite Users Button
37 changes: 21 additions & 16 deletions app/src/main/java/chat/rocket/android/members/ui/MembersFragment.kt
Original file line number Diff line number Diff line change
@@ -43,8 +43,8 @@ class MembersFragment : Fragment(), MembersView {
lateinit var analyticsManager: AnalyticsManager
private val adapter: MembersAdapter =
MembersAdapter { memberUiModel -> presenter.toMemberDetails(memberUiModel, chatRoomId) }
private val linearLayoutManager = LinearLayoutManager(context)
private lateinit var chatRoomId: String
private lateinit var endlessRecyclerViewScrollListener: EndlessRecyclerViewScrollListener

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -67,28 +67,26 @@ class MembersFragment : Fragment(), MembersView {
setupRecyclerView()
setupListeners()
presenter.checkInviteUserPermission(chatRoomId)
presenter.loadChatRoomsMembers(chatRoomId)
presenter.loadChatRoomsMembers(chatRoomId, clearDataset = true)

analyticsManager.logScreenView(ScreenViewEvent.Members)
}

override fun showMembers(dataSet: List<MemberUiModel>, total: Long) {
override fun onDestroyView() {
recycler_view.removeOnScrollListener(endlessRecyclerViewScrollListener)
super.onDestroyView()
}

override fun showMembers(dataSet: List<MemberUiModel>, total: Long, clearDataset: Boolean) {
ui {
setupToolbar(total)

if (clearDataset) {
adapter.clearData()
}

if (adapter.itemCount == 0) {
adapter.prependData(dataSet)
if (dataSet.size >= 59) { // TODO Check why the API returns the specified count -1
recycler_view.addOnScrollListener(object :
EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore(
page: Int,
totalItemsCount: Int,
recyclerView: RecyclerView
) {
presenter.loadChatRoomsMembers(chatRoomId)
}
})
}
} else {
adapter.appendData(dataSet)
}
@@ -123,14 +121,21 @@ class MembersFragment : Fragment(), MembersView {

private fun setupRecyclerView() {
ui {
recycler_view.layoutManager = LinearLayoutManager(context)
val linearLayoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
recycler_view.layoutManager = linearLayoutManager
recycler_view.addItemDecoration(
DividerItemDecoration(
it,
DividerItemDecoration.HORIZONTAL
)
)
endlessRecyclerViewScrollListener = object : EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, recyclerView: RecyclerView) {
presenter.loadChatRoomsMembers(chatRoomId, page * 60L)
}
}
recycler_view.adapter = adapter
recycler_view.addOnScrollListener(endlessRecyclerViewScrollListener)
}
}

0 comments on commit f87f265

Please sign in to comment.