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