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

Update RoomListPresenter.kt

parent aa288e56
...@@ -2,6 +2,7 @@ package chat.rocket.android.fragment.chatroom.list ...@@ -2,6 +2,7 @@ package chat.rocket.android.fragment.chatroom.list
import android.content.Context import android.content.Context
import android.os.Handler import android.os.Handler
import android.util.Log
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.api.rest.RestApiHelper import chat.rocket.android.api.rest.RestApiHelper
import chat.rocket.android.helper.OkHttpHelper import chat.rocket.android.helper.OkHttpHelper
...@@ -11,6 +12,7 @@ import chat.rocket.core.models.User ...@@ -11,6 +12,7 @@ import chat.rocket.core.models.User
import okhttp3.Call import okhttp3.Call
import okhttp3.Callback import okhttp3.Callback
import okhttp3.Response import okhttp3.Response
import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import java.io.IOException import java.io.IOException
import java.sql.Timestamp import java.sql.Timestamp
...@@ -147,72 +149,80 @@ class RoomListPresenter(val context: Context, val view: RoomListContract.View) : ...@@ -147,72 +149,80 @@ class RoomListPresenter(val context: Context, val view: RoomListContract.View) :
} }
private fun handleMessagesJson(json: String, isPinnedMessage: Boolean) { private fun handleMessagesJson(json: String, isPinnedMessage: Boolean) {
val jSONObject = JSONObject(json) try {
val messagesJSONArray = jSONObject.getJSONArray("messages") val jSONObject = JSONObject(json)
val messagesJSONArray = jSONObject.getJSONArray("messages")
val total = messagesJSONArray.length() val total = messagesJSONArray.length()
val dataSet = ArrayList<Message>(total) val dataSet = ArrayList<Message>(total)
(0 until total).mapTo(dataSet) { (0 until total).mapTo(dataSet) {
val messageJsonObject = messagesJSONArray.getJSONObject(it) val messageJsonObject = messagesJSONArray.getJSONObject(it)
val userJsonObject = messageJsonObject.getJSONObject("u") val userJsonObject = messageJsonObject.getJSONObject("u")
val timestampString = messageJsonObject.optString("ts") val timestampString = messageJsonObject.optString("ts")
val timestamp = if (timestampString.isBlank()) { val timestamp = if (timestampString.isBlank()) {
0 0
} else { } else {
Timestamp.valueOf(timestampString.replace("T", " ").replace("Z", "")).time Timestamp.valueOf(timestampString.replace("T", " ").replace("Z", "")).time
} }
val editedAtString = messageJsonObject.optString("_updatedAt") val editedAtString = messageJsonObject.optString("_updatedAt")
val editedAt = if (editedAtString.isBlank()) { val editedAt = if (editedAtString.isBlank()) {
0 0
} else { } else {
Timestamp.valueOf(editedAtString.replace("T", " ").replace("Z", "")).time Timestamp.valueOf(editedAtString.replace("T", " ").replace("Z", "")).time
} }
Message.builder() Message.builder()
.setId(messageJsonObject.optString("_id")) .setId(messageJsonObject.optString("_id"))
.setRoomId(messageJsonObject.optString("rid")) .setRoomId(messageJsonObject.optString("rid"))
.setMessage(messageJsonObject.optString("msg")) .setMessage(messageJsonObject.optString("msg"))
.setUser(getUserFromJsonObject(userJsonObject)) .setUser(getUserFromJsonObject(userJsonObject))
.setTimestamp(timestamp) .setTimestamp(timestamp)
.setEditedAt(editedAt) .setEditedAt(editedAt)
.setGroupable(messageJsonObject.optBoolean("groupable")) .setGroupable(messageJsonObject.optBoolean("groupable"))
.setSyncState(SyncState.SYNCED) .setSyncState(SyncState.SYNCED)
.build() .build()
} }
if (dataSet.isEmpty() && !hasItem) { if (dataSet.isEmpty() && !hasItem) {
showEmptyViewMessage(context.getString(R.string.fragment_room_list_no_favorite_message_to_show)) showEmptyViewMessage(context.getString(R.string.fragment_room_list_no_favorite_message_to_show))
} else { } else {
if (dataSet.isNotEmpty()) { if (dataSet.isNotEmpty()) {
hasItem = true hasItem = true
if (isPinnedMessage) { if (isPinnedMessage) {
showPinnedMessageList(dataSet, jSONObject.optString("total")) showPinnedMessageList(dataSet, jSONObject.optString("total"))
} else { } else {
showFavoriteMessageList(dataSet, jSONObject.optString("total")) showFavoriteMessageList(dataSet, jSONObject.optString("total"))
}
} }
} }
} catch (exception: JSONException) {
showInvalidRequest()
} }
} }
private fun handleMembersJson(json: String) { private fun handleMembersJson(json: String) {
val jsonObject = JSONObject(json) try {
val membersJsonArray = jsonObject.getJSONArray("members") val jsonObject = JSONObject(json)
val membersJsonArray = jsonObject.getJSONArray("members")
val total = membersJsonArray.length() val total = membersJsonArray.length()
val dataSet = ArrayList<User>(total) val dataSet = ArrayList<User>(total)
(0 until total).mapTo(dataSet) { (0 until total).mapTo(dataSet) {
getUserFromJsonObject(membersJsonArray.getJSONObject(it)) getUserFromJsonObject(membersJsonArray.getJSONObject(it))
} }
if (dataSet.isEmpty() && !hasItem) { if (dataSet.isEmpty() && !hasItem) {
showEmptyViewMessage(context.getString(R.string.fragment_room_list_no_member_list_to_show)) showEmptyViewMessage(context.getString(R.string.fragment_room_list_no_member_list_to_show))
} else { } else {
if (dataSet.isNotEmpty()) { if (dataSet.isNotEmpty()) {
hasItem = true hasItem = true
showMemberList(dataSet, jsonObject.optString("total")) showMemberList(dataSet, jsonObject.optString("total"))
}
} }
}catch (exception: JSONException) {
showInvalidRequest()
} }
} }
...@@ -247,6 +257,13 @@ class RoomListPresenter(val context: Context, val view: RoomListContract.View) : ...@@ -247,6 +257,13 @@ class RoomListPresenter(val context: Context, val view: RoomListContract.View) :
} }
} }
private fun showInvalidRequest() {
mainHandler.post {
view.showWaitingView(false)
view.showMessage(context.getString(R.string.fragment_room_list_could_not_load_your_request, context.getString(R.string.make_sure_your_server_version_is_up_to_date)))
}
}
private fun showEmptyViewMessage(message: String) { private fun showEmptyViewMessage(message: String) {
mainHandler.post { mainHandler.post {
view.showWaitingView(false) view.showWaitingView(false)
......
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