Commit dbee09cd authored by Lucio Maciel's avatar Lucio Maciel

More idiomatic kotlin

parent 0c94ddd8
......@@ -24,4 +24,7 @@ class ChatRoomsRepository @Inject constructor(val dao: ChatRoomDao){
NAME,
GROUPED_NAME,
}
}
\ No newline at end of file
}
fun ChatRoomsRepository.Order.isGrouped(): Boolean = this == ChatRoomsRepository.Order.GROUPED_ACTIVITY
|| this == ChatRoomsRepository.Order.GROUPED_NAME
\ No newline at end of file
......@@ -8,12 +8,15 @@ import chat.rocket.android.chatrooms.adapter.ItemHolder
import chat.rocket.android.chatrooms.adapter.RoomMapper
import chat.rocket.android.chatrooms.domain.FetchChatRoomsInteractor
import chat.rocket.android.chatrooms.infrastructure.ChatRoomsRepository
import chat.rocket.android.chatrooms.infrastructure.isGrouped
import chat.rocket.android.server.infraestructure.ConnectionManager
import chat.rocket.android.util.livedata.TransformedLiveData
import chat.rocket.android.util.livedata.transform
import chat.rocket.core.internal.realtime.socket.model.State
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.newSingleThreadContext
import me.henrytao.livedataktx.distinct
import me.henrytao.livedataktx.map
import me.henrytao.livedataktx.nonNull
import timber.log.Timber
......@@ -33,25 +36,25 @@ class ChatRoomsViewModel(
fun getChatRooms(): LiveData<List<ItemHolder<*>>> {
return Transformations.switchMap(ordering) { order ->
Timber.d("Querying rooms for order: $order")
val grouped = order == ChatRoomsRepository.Order.GROUPED_ACTIVITY
|| order == ChatRoomsRepository.Order.GROUPED_NAME
val roomData = repository.getChatRooms(order).nonNull().distinct()
TransformedLiveData(runContext, roomData) { rooms ->
rooms?.let {
mapper.map(rooms, grouped)
}
}.nonNull()
repository.getChatRooms(order)
.nonNull()
.distinct()
.transform(runContext) { rooms ->
rooms?.let {
mapper.map(rooms, order.isGrouped())
}
}
}
}
fun getStatus(): MutableLiveData<State> {
return Transformations.map(connectionManager.statusLiveData.nonNull().distinct()) { state ->
return connectionManager.statusLiveData.nonNull().distinct().map { state ->
if (state is State.Connected) {
// TODO - add a loading status...
fetchRooms()
}
state
}.nonNull()
}
}
private fun fetchRooms() {
......
......@@ -6,7 +6,7 @@ import javax.inject.Singleton
typealias TupleGroupIdMessageCount = Pair<Int, AtomicInteger>
class GroupedPush {
// Notifications received from the same server are grouped in a single bundled notification.
// Notifications received from the same server are isGrouped in a single bundled notification.
// This map associates a host to a group id.
val groupMap = HashMap<String, TupleGroupIdMessageCount>()
......
......@@ -273,7 +273,7 @@ class PushManager @Inject constructor(
private fun getContentIntent(context: Context, notificationId: Int, pushMessage: PushMessage, grouped: Boolean = false): PendingIntent {
val notificationIntent = context.changeServerIntent(pushMessage.info.host)
// TODO - add support to go directly to the chatroom
/*if (!grouped) {
/*if (!isGrouped) {
notificationIntent.putExtra(EXTRA_ROOM_ID, pushMessage.info.roomId)
}*/
return PendingIntent.getActivity(context, randomizer.nextInt(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)
......
......@@ -7,7 +7,6 @@ import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import timber.log.Timber
import kotlin.coroutines.experimental.CoroutineContext
......@@ -22,6 +21,8 @@ class TransformedLiveData<Source, Output>(
job?.cancel()
job = launch(runContext) {
transformation(source)?.let { transformed ->
// Could have used postValue instead, but using the UI context I can guarantee that
// a canceled job will never emit values.
withContext(UI) {
value = transformed
}
......@@ -30,13 +31,15 @@ class TransformedLiveData<Source, Output>(
}
override fun onActive() {
Timber.d("Attaching observer")
source.observeForever(observer)
}
override fun onInactive() {
Timber.d("Detaching observer")
job?.cancel()
source.removeObserver(observer)
}
}
\ No newline at end of file
}
fun <Source, Output> LiveData<Source>.transform(
runContext: CoroutineContext = CommonPool,
transformation: (Source?) -> Output?) = TransformedLiveData(runContext, this, transformation)
\ 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