Commit b29e71b5 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update RoomListFragment.kt

parent 41bae5a7
......@@ -3,26 +3,23 @@ package chat.rocket.android.fragment.chatroom.list
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import chat.rocket.android.R
import chat.rocket.android.layouthelper.chatroom.list.RoomFileListAdapter
import chat.rocket.android.helper.EndlessRecyclerViewScrollListener
import chat.rocket.android.layouthelper.chatroom.list.RoomMemberListAdapter
import chat.rocket.android.layouthelper.chatroom.list.RoomMessagesAdapter
import chat.rocket.core.models.Message
import chat.rocket.core.models.User
import kotlinx.android.synthetic.main.fragment_room_list.*
import java.util.*
/**
* Created by Filipe de Lima Brito (filipedelimabrito@gmail.com) on 9/22/17.
*/
class RoomListFragment : Fragment(), RoomListContract.View {
var actionId: Int = 0
lateinit var roomId: String
lateinit var roomType: String
lateinit var hostname: String
lateinit var token: String
lateinit var userId: String
lateinit var presenter: RoomListPresenter
var isDataRequested: Boolean = false
companion object {
fun newInstance(actionId: Int,
......@@ -48,6 +45,7 @@ class RoomListFragment : Fragment(), RoomListContract.View {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity.title = ""
actionId = arguments.getInt("actionId")
roomId = arguments.getString("roomId")
roomType = arguments.getString("roomType")
......@@ -66,28 +64,28 @@ class RoomListFragment : Fragment(), RoomListContract.View {
override fun onResume() {
super.onResume()
if (!isDataRequested) {
requestData()
requestData(0)
isDataRequested = true
}
}
private fun requestData() {
private fun requestData(offset: Int) {
when (actionId) {
R.id.action_pinned_messages -> {
activity.title = getString(R.string.fragment_room_list_pinned_message_title)
presenter.requestPinnedMessages(roomId,
roomType,
hostname,
token,
userId)
userId,
offset)
}
R.id.action_favorite_messages -> {
activity.title = getString(R.string.fragment_room_list_favorite_message_title)
presenter.requestFavoriteMessages(roomId,
roomType,
hostname,
token,
userId)
userId,
offset)
}
// R.id.action_file_list -> {
// activity.title = getString(R.string.fragment_room_list_file_list_title)
......@@ -98,60 +96,96 @@ class RoomListFragment : Fragment(), RoomListContract.View {
// userId)
// }
R.id.action_member_list -> {
activity.title = getString(R.string.fragment_room_list_member_list_title)
presenter.requestMemberList(roomId,
roomType,
hostname,
token,
userId)
userId,
offset)
}
}
}
override fun showPinnedMessages(dataSet: ArrayList<Message>) {
waitingView.visibility = View.GONE
if (dataSet.isEmpty()) {
showMessage(getString(R.string.fragment_room_list_no_pinned_message_to_show))
} else {
override fun showPinnedMessages(dataSet: ArrayList<Message>, total: String) {
activity.title = getString(R.string.fragment_room_list_pinned_message_title, total)
if (recyclerView.adapter == null) {
recyclerView.adapter = RoomMessagesAdapter(dataSet, hostname, context)
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
recyclerView.layoutManager = linearLayoutManager
if (dataSet.size >= 50) {
recyclerView.addOnScrollListener(object : EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, recyclerView: RecyclerView?) {
loadNextDataFromApi(page)
}
})
}
override fun showFavoriteMessages(dataSet: ArrayList<Message>) {
if (dataSet.isEmpty()) {
showMessage(getString(R.string.fragment_room_list_no_favorite_message_to_show))
} else {
waitingView.visibility = View.GONE
recyclerView.adapter = RoomMessagesAdapter(dataSet, hostname, context)
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
(recyclerView.adapter as RoomMessagesAdapter).setDataSet(dataSet)
}
}
override fun showFileList(dataSet: ArrayList<String>) {
if (dataSet.isEmpty()) {
showMessage(getString(R.string.fragment_room_list_no_file_list_to_show))
override fun showFavoriteMessages(dataSet: ArrayList<Message>, total: String) {
activity.title = getString(R.string.fragment_room_list_favorite_message_title, total)
if (recyclerView.adapter == null) {
recyclerView.adapter = RoomMessagesAdapter(dataSet, hostname, context)
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
recyclerView.layoutManager = linearLayoutManager
if (dataSet.size >= 50) {
recyclerView.addOnScrollListener(object : EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, recyclerView: RecyclerView?) {
loadNextDataFromApi(page)
}
})
}
} else {
waitingView.visibility = View.GONE
recyclerView.adapter = RoomFileListAdapter(dataSet)
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
(recyclerView.adapter as RoomMessagesAdapter).setDataSet(dataSet)
}
}
// TODO (after REST api fixes)
override fun showFileList(dataSet: ArrayList<String>, total: String) {}
override fun showMemberList(dataSet: ArrayList<User>, total: String) {
if (dataSet.isEmpty()) {
showMessage(getString(R.string.fragment_room_list_no_member_list_to_show))
} else {
waitingView.visibility = View.GONE
activity.title = getString(R.string.fragment_room_list_member_list_title) + " (" + total + ")"
activity.title = getString(R.string.fragment_room_list_member_list_title, total)
if (recyclerView.adapter == null) {
recyclerView.adapter = RoomMemberListAdapter(dataSet, hostname, context)
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
recyclerView.layoutManager = linearLayoutManager
if (dataSet.size >= 50) {
recyclerView.addOnScrollListener(object : EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, recyclerView: RecyclerView?) {
loadNextDataFromApi(page)
}
})
}
} else {
(recyclerView.adapter as RoomMemberListAdapter).setDataSet(dataSet)
}
}
override fun showMessage(message: String) {
waitingView.visibility = View.GONE
messageText.text = message
messageText.visibility = View.VISIBLE
}
override fun showWaitingView(shouldShow: Boolean) {
if (shouldShow) {
waitingView.visibility = View.VISIBLE
} else {
waitingView.visibility = View.GONE
}
}
private fun loadNextDataFromApi(page: Int) {
requestData(page * 50)
}
private var actionId: Int = 0
private lateinit var roomId: String
private lateinit var roomType: String
private lateinit var hostname: String
private lateinit var token: String
private lateinit var userId: String
private lateinit var presenter: RoomListPresenter
private var isDataRequested: Boolean = false
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment