Commit a739a997 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Change async/await constructs for withContext

parent de501c7c
...@@ -2,39 +2,40 @@ package chat.rocket.android.server.domain ...@@ -2,39 +2,40 @@ package chat.rocket.android.server.domain
import chat.rocket.core.model.ChatRoom import chat.rocket.core.model.ChatRoom
import kotlinx.coroutines.experimental.CommonPool import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.async import kotlinx.coroutines.experimental.withContext
import javax.inject.Inject import javax.inject.Inject
class GetChatRoomsInteractor @Inject constructor(private val repository: ChatRoomsRepository) { class GetChatRoomsInteractor @Inject constructor(private val repository: ChatRoomsRepository) {
fun get(url: String) = repository.get(url) fun get(url: String) = repository.get(url)
suspend fun getByName(url: String, name: String): List<ChatRoom> { /**
val chatRooms = async { * Get a list of chat rooms that contains the name parameter.
*
* @param url The server url.
* @param name The name of chat room to look for or a chat room that contains this name.
* @return A list of ChatRoom objects with the given name.
*/
suspend fun getByName(url: String, name: String): List<ChatRoom> = withContext(CommonPool) {
val allChatRooms = repository.get(url) val allChatRooms = repository.get(url)
if (name.isEmpty()) { if (name.isEmpty()) {
return@async allChatRooms return@withContext allChatRooms
} }
return@async allChatRooms.filter { return@withContext allChatRooms.filter {
it.name.contains(name, true) it.name.contains(name, true)
} }
} }
return chatRooms.await()
}
/** /**
* Get a specific room by its id. * Get a specific room by its id.
* *
* @param serverUrl The server url where the room is. * @param serverUrl The server url where the room is.
* @param roomId The id of the room to get. * @param roomId The id of the room to get.
*
* @return The ChatRoom object or null if we couldn't find any. * @return The ChatRoom object or null if we couldn't find any.
*/ */
suspend fun getById(serverUrl: String, roomId: String): ChatRoom? { suspend fun getById(serverUrl: String, roomId: String): ChatRoom? = withContext(CommonPool) {
return async(CommonPool) {
val allChatRooms = repository.get(serverUrl) val allChatRooms = repository.get(serverUrl)
return@async allChatRooms.first { return@withContext allChatRooms.first {
it.id == roomId it.id == roomId
} }
}.await()
} }
} }
\ 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