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

Update RoomDialogPresenter.kt

parent 3c0c1303
...@@ -7,6 +7,10 @@ import chat.rocket.android.helper.OkHttpHelper ...@@ -7,6 +7,10 @@ import chat.rocket.android.helper.OkHttpHelper
import chat.rocket.core.models.Room import chat.rocket.core.models.Room
import okhttp3.* import okhttp3.*
import java.io.IOException import java.io.IOException
import okhttp3.HttpUrl
import org.json.JSONArray
import org.json.JSONObject
class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.View): RoomDialogContract.Presenter { class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.View): RoomDialogContract.Presenter {
override fun getDataSet(roomId: String, override fun getDataSet(roomId: String,
...@@ -17,8 +21,20 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie ...@@ -17,8 +21,20 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie
userId: String, userId: String,
action: Int) { action: Int) {
when (action) { when (action) {
R.id.action_pinned_messages -> getPinnedMessages(roomType, hostname, token, userId) R.id.action_pinned_messages -> {
R.id.action_favorite_messages -> getFavoriteMessages(roomType, hostname, token, userId) getPinnedMessages(roomId,
roomType,
hostname,
token,
userId)
}
R.id.action_favorite_messages -> {
getFavoriteMessages(roomId,
roomType,
hostname,
token,
userId)
}
R.id.action_file_list -> { R.id.action_file_list -> {
getFileList(roomId, getFileList(roomId,
roomType, roomType,
...@@ -26,16 +42,32 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie ...@@ -26,16 +42,32 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie
token, token,
userId) userId)
} }
R.id.action_member_list -> getMemberList(roomType, hostname, token, userId) R.id.action_member_list -> {
getMemberList(roomId,
roomType,
hostname,
token,
userId)
} }
} }
}
private fun getPinnedMessages(roomId: String,
roomType: String,
hostname: String,
token: String,
userId: String) {
// TODO("not implemented")
private fun getPinnedMessages(roomType: String, hostname: String, token: String, userId: String) {
view.showPinnedMessages()
} }
private fun getFavoriteMessages(roomType: String, hostname: String, token: String, userId: String) { private fun getFavoriteMessages(roomId: String,
view.showFavoriteMessages() roomType: String,
hostname: String,
token: String,
userId: String) {
// TODO("not implemented")
} }
private fun getFileList(roomId: String, private fun getFileList(roomId: String,
...@@ -43,33 +75,75 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie ...@@ -43,33 +75,75 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie
hostname: String, hostname: String,
token: String, token: String,
userId: String) { userId: String) {
OkHttpHelper.getClient() OkHttpHelper.getClient()
.newCall(getRequest(roomId, .newCall(getRequestForFileList(roomId,
roomType, roomType,
hostname, hostname,
token, token,
userId)) userId))
.enqueue(object : Callback { .enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) { override fun onFailure(call: Call, e: IOException) {
Log.i("REST", "FAIL = " + e.message) view.showMessage(e.printStackTrace().toString())
} }
@Throws(IOException::class) @Throws(IOException::class)
override fun onResponse(call: Call, response: Response) { override fun onResponse(call: Call, response: Response) {
Log.i("REST", "SUCCESS = " + response.body()?.string()) if (response.isSuccessful) {
val res = response.body() val jSONObject = JSONObject(response.body()?.string())
Log.i("REST", " = " + jSONObject.toString())
Log.i("REST", " = " + jSONObject)
val filesJSONArray = jSONObject.get("files") as JSONArray
val total = filesJSONArray.length()
val dataSet = ArrayList<String>(total)
(0 until total).mapTo(dataSet) { filesJSONArray.get(it).toString() }
view.showFileList(dataSet)
} else {
// TODO("move to strings.xml")
view.showMessage("Response is not successful")
}
} }
}) })
view.showFileList()
} }
private fun getMemberList(roomType: String, hostname: String, token: String, userId: String) { private fun getMemberList(roomId: String,
view.showMemberList() roomType: String,
hostname: String,
token: String,
userId: String) {
OkHttpHelper.getClient()
.newCall(getRequestForMemberList(roomId,
roomType,
hostname,
token,
userId))
.enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
view.showMessage(e.printStackTrace().toString())
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
val jSONObject = JSONObject(response.body()?.string())
val membersJSONArray = jSONObject.get("members") as JSONArray
val total = membersJSONArray.length()
val dataSet = ArrayList<String>(total)
(0 until total).mapTo(dataSet) { membersJSONArray.get(it).toString() }
view.showMemberList(dataSet)
} else {
// TODO("move to strings.xml")
view.showMessage("Response is not successful")
}
}
})
} }
/** /**
* Returns an OkHttp3 request corresponding to the Rest API call. * Returns an OkHttp3 request for file list accordingly with the room type.
* *
* @param roomId The ID of the room. * @param roomId The ID of the room.
* @param roomType The type of the room. * @param roomType The type of the room.
...@@ -78,20 +152,46 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie ...@@ -78,20 +152,46 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie
* @param userId The user Id. * @param userId The user Id.
* @return A OkHttp3 request. * @return A OkHttp3 request.
*/ */
private fun getRequest(roomId: String, private fun getRequestForFileList(roomId: String,
roomType: String, roomType: String,
hostname: String, hostname: String,
token: String, token: String,
userId: String): Request { userId: String): Request {
val parsedHttpUrl = HttpUrl.parse(getEndpointUrlForFileList(roomType, hostname))
?.newBuilder()
?.addQueryParameter("roomId", roomId)
?.build()
val httpUrl = HttpUrl.Builder() return Request.Builder()
.scheme("http") .url(parsedHttpUrl)
.host(getEndpointUrl(roomType, hostname)) .get()
.addQueryParameter("roomId", roomId) .addHeader("X-Auth-Token", token)
.addHeader("X-User-Id", userId)
.build() .build()
}
/**
* Returns an OkHttp3 request for member list accordingly with the room type.
*
* @param roomId The ID of the room.
* @param roomType The type of the room.
* @param hostname The server hostname.
* @param token The token.
* @param userId The user Id.
* @return A OkHttp3 request.
*/
private fun getRequestForMemberList(roomId: String,
roomType: String,
hostname: String,
token: String,
userId: String): Request {
val parsedHttpUrl = HttpUrl.parse(getEndpointUrlForMemberList(roomType, hostname))
?.newBuilder()
?.addQueryParameter("roomId", roomId)
?.build()
return Request.Builder() return Request.Builder()
.url(httpUrl) .url(parsedHttpUrl)
.get() .get()
.addHeader("X-Auth-Token", token) .addHeader("X-Auth-Token", token)
.addHeader("X-User-Id", userId) .addHeader("X-User-Id", userId)
...@@ -99,18 +199,28 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie ...@@ -99,18 +199,28 @@ class RoomDialogPresenter(val context: Context, val view: RoomDialogContract.Vie
} }
/** /**
* Returns an endpoint URL (without the https or http schemas) corresponding to the Rest API call. * Returns a Rest API endpoint URL for member list accordingly with the room type.
* *
* @param roomType The type of the room. * @param roomType The type of the room.
* @param hostname The server hostname. * @param hostname The server hostname.
* @return A Rest API URL endpoint starting with www. * @return A Rest API URL endpoint.
*/ */
private fun getEndpointUrl(roomType: String, hostname: String): String = "www." + private fun getEndpointUrlForMemberList(roomType: String, hostname: String): String = "https://" +
hostname.replace("http://", "") hostname.replace("http://", "").replace("https://", "") +
.replace("https://", "")
.replace("www", "") +
getRestApiUrlForMemberList(roomType) getRestApiUrlForMemberList(roomType)
/**
* Returns a Rest API endpoint URL for file list. accordingly with the room type
*
* @param roomType The type of the room.
* @param hostname The server hostname.
* @return A Rest API URL endpoint.
*/
private fun getEndpointUrlForFileList(roomType: String, hostname: String): String = "https://" +
hostname.replace("http://", "").replace("https://", "") +
getRestApiUrlForFileList(roomType)
/** /**
* Returns the correspondent Rest API URL accordingly with the room type to get its members list. * Returns the correspondent Rest API URL accordingly with the room type to get its members list.
* *
......
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