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 ...@@ -3,26 +3,23 @@ package chat.rocket.android.fragment.chatroom.list
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import chat.rocket.android.R 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.RoomMemberListAdapter
import chat.rocket.android.layouthelper.chatroom.list.RoomMessagesAdapter import chat.rocket.android.layouthelper.chatroom.list.RoomMessagesAdapter
import chat.rocket.core.models.Message import chat.rocket.core.models.Message
import chat.rocket.core.models.User import chat.rocket.core.models.User
import kotlinx.android.synthetic.main.fragment_room_list.* 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 { 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 { companion object {
fun newInstance(actionId: Int, fun newInstance(actionId: Int,
...@@ -48,6 +45,7 @@ class RoomListFragment : Fragment(), RoomListContract.View { ...@@ -48,6 +45,7 @@ class RoomListFragment : Fragment(), RoomListContract.View {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
activity.title = ""
actionId = arguments.getInt("actionId") actionId = arguments.getInt("actionId")
roomId = arguments.getString("roomId") roomId = arguments.getString("roomId")
roomType = arguments.getString("roomType") roomType = arguments.getString("roomType")
...@@ -66,28 +64,28 @@ class RoomListFragment : Fragment(), RoomListContract.View { ...@@ -66,28 +64,28 @@ class RoomListFragment : Fragment(), RoomListContract.View {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (!isDataRequested) { if (!isDataRequested) {
requestData() requestData(0)
isDataRequested = true isDataRequested = true
} }
} }
private fun requestData() { private fun requestData(offset: Int) {
when (actionId) { when (actionId) {
R.id.action_pinned_messages -> { R.id.action_pinned_messages -> {
activity.title = getString(R.string.fragment_room_list_pinned_message_title)
presenter.requestPinnedMessages(roomId, presenter.requestPinnedMessages(roomId,
roomType, roomType,
hostname, hostname,
token, token,
userId) userId,
offset)
} }
R.id.action_favorite_messages -> { R.id.action_favorite_messages -> {
activity.title = getString(R.string.fragment_room_list_favorite_message_title)
presenter.requestFavoriteMessages(roomId, presenter.requestFavoriteMessages(roomId,
roomType, roomType,
hostname, hostname,
token, token,
userId) userId,
offset)
} }
// R.id.action_file_list -> { // R.id.action_file_list -> {
// activity.title = getString(R.string.fragment_room_list_file_list_title) // activity.title = getString(R.string.fragment_room_list_file_list_title)
...@@ -98,60 +96,96 @@ class RoomListFragment : Fragment(), RoomListContract.View { ...@@ -98,60 +96,96 @@ class RoomListFragment : Fragment(), RoomListContract.View {
// userId) // userId)
// } // }
R.id.action_member_list -> { R.id.action_member_list -> {
activity.title = getString(R.string.fragment_room_list_member_list_title)
presenter.requestMemberList(roomId, presenter.requestMemberList(roomId,
roomType, roomType,
hostname, hostname,
token, token,
userId) userId,
offset)
} }
} }
} }
override fun showPinnedMessages(dataSet: ArrayList<Message>) { override fun showPinnedMessages(dataSet: ArrayList<Message>, total: String) {
waitingView.visibility = View.GONE activity.title = getString(R.string.fragment_room_list_pinned_message_title, total)
if (dataSet.isEmpty()) { if (recyclerView.adapter == null) {
showMessage(getString(R.string.fragment_room_list_no_pinned_message_to_show))
} else {
recyclerView.adapter = RoomMessagesAdapter(dataSet, hostname, context) 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 { } else {
waitingView.visibility = View.GONE (recyclerView.adapter as RoomMessagesAdapter).setDataSet(dataSet)
recyclerView.adapter = RoomMessagesAdapter(dataSet, hostname, context)
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
} }
} }
override fun showFileList(dataSet: ArrayList<String>) { override fun showFavoriteMessages(dataSet: ArrayList<Message>, total: String) {
if (dataSet.isEmpty()) { activity.title = getString(R.string.fragment_room_list_favorite_message_title, total)
showMessage(getString(R.string.fragment_room_list_no_file_list_to_show)) 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 { } else {
waitingView.visibility = View.GONE (recyclerView.adapter as RoomMessagesAdapter).setDataSet(dataSet)
recyclerView.adapter = RoomFileListAdapter(dataSet)
recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
} }
} }
// TODO (after REST api fixes)
override fun showFileList(dataSet: ArrayList<String>, total: String) {}
override fun showMemberList(dataSet: ArrayList<User>, total: String) { override fun showMemberList(dataSet: ArrayList<User>, total: String) {
if (dataSet.isEmpty()) { activity.title = getString(R.string.fragment_room_list_member_list_title, total)
showMessage(getString(R.string.fragment_room_list_no_member_list_to_show)) if (recyclerView.adapter == null) {
} else {
waitingView.visibility = View.GONE
activity.title = getString(R.string.fragment_room_list_member_list_title) + " (" + total + ")"
recyclerView.adapter = RoomMemberListAdapter(dataSet, hostname, context) 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) { override fun showMessage(message: String) {
waitingView.visibility = View.GONE
messageText.text = message messageText.text = message
messageText.visibility = View.VISIBLE 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