Commit a739a997 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Change async/await constructs for withContext

parent de501c7c
...@@ -2,23 +2,27 @@ package chat.rocket.android.server.domain ...@@ -2,23 +2,27 @@ 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.
val allChatRooms = repository.get(url) *
if (name.isEmpty()) { * @param url The server url.
return@async allChatRooms * @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.
return@async allChatRooms.filter { */
it.name.contains(name, true) suspend fun getByName(url: String, name: String): List<ChatRoom> = withContext(CommonPool) {
} val allChatRooms = repository.get(url)
if (name.isEmpty()) {
return@withContext allChatRooms
}
return@withContext allChatRooms.filter {
it.name.contains(name, true)
} }
return chatRooms.await()
} }
/** /**
...@@ -26,15 +30,12 @@ class GetChatRoomsInteractor @Inject constructor(private val repository: ChatRoo ...@@ -26,15 +30,12 @@ class GetChatRoomsInteractor @Inject constructor(private val repository: ChatRoo
* *
* @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@withContext allChatRooms.first {
return@async 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