Commit 6d84d280 authored by Lucio Maciel's avatar Lucio Maciel

WIP

parent f09e71c4
...@@ -6,6 +6,11 @@ import android.app.Service ...@@ -6,6 +6,11 @@ import android.app.Service
import chat.rocket.android.BuildConfig import chat.rocket.android.BuildConfig
import chat.rocket.android.app.utils.CustomImageFormatConfigurator import chat.rocket.android.app.utils.CustomImageFormatConfigurator
import chat.rocket.android.dagger.DaggerAppComponent import chat.rocket.android.dagger.DaggerAppComponent
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.MultiServerTokenRepository
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.common.model.Token
import chat.rocket.core.TokenRepository
import com.facebook.drawee.backends.pipeline.DraweeConfig import com.facebook.drawee.backends.pipeline.DraweeConfig
import com.facebook.drawee.backends.pipeline.Fresco import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.imagepipeline.core.ImagePipelineConfig import com.facebook.imagepipeline.core.ImagePipelineConfig
...@@ -29,11 +34,24 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -29,11 +34,24 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
lateinit var instance: RocketChatApplication lateinit var instance: RocketChatApplication
} }
// TODO - remove this from here when we have a proper service handling the connection.
@Inject
lateinit var getCurrentServerInteractor: GetCurrentServerInteractor
@Inject
lateinit var multiServerRepository: MultiServerTokenRepository
@Inject
lateinit var settingsRepository: SettingsRepository
@Inject
lateinit var tokenRepository: TokenRepository
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
DaggerAppComponent.builder().application(this).build().inject(this) DaggerAppComponent.builder().application(this).build().inject(this)
// TODO - remove this when we have a proper service handling connection...
initCurrentServer()
AndroidThreeTen.init(this) AndroidThreeTen.init(this)
setupFresco() setupFresco()
...@@ -41,6 +59,16 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje ...@@ -41,6 +59,16 @@ class RocketChatApplication : Application(), HasActivityInjector, HasServiceInje
instance = this instance = this
} }
// TODO - remove this when we have a proper service handling connection...
private fun initCurrentServer() {
val currentServer = getCurrentServerInteractor.get()
val serverToken = currentServer?.let { multiServerRepository.get(currentServer) }
val settings = currentServer?.let { settingsRepository.get(currentServer) }
if (currentServer != null && serverToken != null && settings != null) {
tokenRepository.save(Token(serverToken.userId, serverToken.authToken))
}
}
private fun setupFresco() { private fun setupFresco() {
val imagePipelineConfig = ImagePipelineConfig.newBuilder(this) val imagePipelineConfig = ImagePipelineConfig.newBuilder(this)
.setImageDecoderConfig(CustomImageFormatConfigurator.createImageDecoderConfig()) .setImageDecoderConfig(CustomImageFormatConfigurator.createImageDecoderConfig())
......
package chat.rocket.android.authentication.di package chat.rocket.android.authentication.di
import android.content.Context import android.content.Context
import chat.rocket.android.authentication.infraestructure.SharedPreferencesMultiServerTokenRepository
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.authentication.ui.AuthenticationActivity import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.dagger.scope.PerActivity import chat.rocket.android.dagger.scope.PerActivity
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.MultiServerTokenRepository
import com.squareup.moshi.Moshi
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
...@@ -17,10 +13,4 @@ class AuthenticationModule { ...@@ -17,10 +13,4 @@ class AuthenticationModule {
@Provides @Provides
@PerActivity @PerActivity
fun provideAuthenticationNavigator(activity: AuthenticationActivity, context: Context) = AuthenticationNavigator(activity, context) fun provideAuthenticationNavigator(activity: AuthenticationActivity, context: Context) = AuthenticationNavigator(activity, context)
@Provides
@PerActivity
fun provideMultiServerTokenRepository(repository: LocalRepository, moshi: Moshi): MultiServerTokenRepository {
return SharedPreferencesMultiServerTokenRepository(repository, moshi)
}
} }
\ No newline at end of file
...@@ -8,6 +8,8 @@ import chat.rocket.android.server.infraestructure.RocketChatClientFactory ...@@ -8,6 +8,8 @@ import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.model.BaseRoom import chat.rocket.common.model.BaseRoom
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
import chat.rocket.core.internal.realtime.State
import chat.rocket.core.internal.realtime.connect
import chat.rocket.core.internal.realtime.subscribeRoomMessages import chat.rocket.core.internal.realtime.subscribeRoomMessages
import chat.rocket.core.internal.realtime.unsubscibre import chat.rocket.core.internal.realtime.unsubscibre
import chat.rocket.core.internal.rest.messages import chat.rocket.core.internal.rest.messages
...@@ -15,6 +17,7 @@ import chat.rocket.core.internal.rest.sendMessage ...@@ -15,6 +17,7 @@ import chat.rocket.core.internal.rest.sendMessage
import chat.rocket.core.model.Message import chat.rocket.core.model.Message
import chat.rocket.core.model.Value import chat.rocket.core.model.Value
import kotlinx.coroutines.experimental.CommonPool import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
...@@ -33,16 +36,24 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -33,16 +36,24 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
settings = getSettingsInteractor.get(serverInteractor.get()!!) settings = getSettingsInteractor.get(serverInteractor.get()!!)
} }
fun loadMessages(chatRoomId: String, chatRoomType: String, offset: Int = 0) { private val stateChannel = Channel<State>()
fun loadMessages(chatRoomId: String, chatRoomType: String, offset: Long = 0) {
launchUI(strategy) { launchUI(strategy) {
view.showLoading() view.showLoading()
try { try {
val messages = client.messages(chatRoomId, BaseRoom.RoomType.valueOf(chatRoomType), offset.toLong(), 30).result val messages = client.messages(chatRoomId, BaseRoom.RoomType.valueOf(chatRoomType), offset, 30).result
synchronized(roomMessages) { synchronized(roomMessages) {
roomMessages.addAll(messages) roomMessages.addAll(messages)
} }
val messagesViewModels = MessageViewModelMapper.mapToViewModelList(messages, settings) val messagesViewModels = MessageViewModelMapper.mapToViewModelList(messages, settings)
view.showMessages(messagesViewModels, serverInteractor.get()!!) view.showMessages(messagesViewModels, serverInteractor.get()!!)
// Subscribe after getting the first page of messages from REST
if (offset == 0L) {
subscribeMessages(chatRoomId)
}
} catch (ex: Exception) { } catch (ex: Exception) {
ex.printStackTrace() ex.printStackTrace()
ex.message?.let { ex.message?.let {
...@@ -77,16 +88,46 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -77,16 +88,46 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
} }
fun subscribeMessages(roomId: String) { fun subscribeMessages(roomId: String) {
launchUI(strategy) { client.addStateChannel(stateChannel)
launch(CommonPool + strategy.jobs) {
for (status in stateChannel) {
Timber.d("Changing status to: $status")
when (status) {
State.Authenticating -> Timber.d("Authenticating")
State.Connected -> {
Timber.d("Connected")
subId = client.subscribeRoomMessages(roomId) { subId = client.subscribeRoomMessages(roomId) {
Timber.d("subscribe messages for $roomId: $it") Timber.d("subscribe messages for $roomId: $it")
} }
listenMessages(roomId)
} }
} }
}
Timber.d("Done on statusChannel")
}
when (client.state) {
State.Connected -> {
Timber.d("Already connected")
subId = client.subscribeRoomMessages(roomId) {
Timber.d("subscribe messages for $roomId: $it")
}
}
else -> client.connect()
}
// TODO - when we have a proper service, we won't need to take care of connection, just
// subscribe and listen...
/*launchUI(strategy) {
subId = client.subscribeRoomMessages(roomId) {
Timber.d("subscribe messages for $roomId: $it")
}
listenMessages(roomId)
}*/
}
fun unsubscribeMessages() { fun unsubscribeMessages() {
launch(CommonPool) { launch(CommonPool) {
client.removeStateChannel(stateChannel)
subId?.let { subscriptionId -> subId?.let { subscriptionId ->
client.unsubscibre(subscriptionId) client.unsubscibre(subscriptionId)
} }
......
...@@ -67,7 +67,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView { ...@@ -67,7 +67,6 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
presenter.loadMessages(chatRoomId, chatRoomType) presenter.loadMessages(chatRoomId, chatRoomType)
presenter.subscribeMessages(chatRoomId)
setupComposer() setupComposer()
} }
...@@ -87,7 +86,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView { ...@@ -87,7 +86,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView {
if (dataSet.size >= 30) { if (dataSet.size >= 30) {
recycler_view.addOnScrollListener(object : EndlessRecyclerViewScrollListener(linearLayoutManager) { recycler_view.addOnScrollListener(object : EndlessRecyclerViewScrollListener(linearLayoutManager) {
override fun onLoadMore(page: Int, totalItemsCount: Int, recyclerView: RecyclerView?) { override fun onLoadMore(page: Int, totalItemsCount: Int, recyclerView: RecyclerView?) {
presenter.loadMessages(chatRoomId, chatRoomType, page * 30) presenter.loadMessages(chatRoomId, chatRoomType, page * 30L)
} }
}) })
} }
......
...@@ -13,6 +13,7 @@ import chat.rocket.core.internal.rest.chatRooms ...@@ -13,6 +13,7 @@ import chat.rocket.core.internal.rest.chatRooms
import chat.rocket.core.model.ChatRoom import chat.rocket.core.model.ChatRoom
import chat.rocket.core.model.Room import chat.rocket.core.model.Room
import kotlinx.coroutines.experimental.* import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.Channel
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
...@@ -27,6 +28,8 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView, ...@@ -27,6 +28,8 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
private val currentServer = serverInteractor.get()!! private val currentServer = serverInteractor.get()!!
private var reloadJob: Deferred<List<ChatRoom>>? = null private var reloadJob: Deferred<List<ChatRoom>>? = null
private val stateChannel = Channel<State>()
fun loadChatRooms() { fun loadChatRooms() {
launchUI(strategy) { launchUI(strategy) {
view.showLoading() view.showLoading()
...@@ -80,8 +83,9 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView, ...@@ -80,8 +83,9 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
// TODO - Temporary stuff, remove when adding DB support // TODO - Temporary stuff, remove when adding DB support
private suspend fun subscribeRoomUpdates() { private suspend fun subscribeRoomUpdates() {
client.addStateChannel(stateChannel)
launch(CommonPool + strategy.jobs) { launch(CommonPool + strategy.jobs) {
for (status in client.statusChannel) { for (status in stateChannel) {
Timber.d("Changing status to: $status") Timber.d("Changing status to: $status")
when (status) { when (status) {
State.Authenticating -> Timber.d("Authenticating") State.Authenticating -> Timber.d("Authenticating")
...@@ -243,6 +247,7 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView, ...@@ -243,6 +247,7 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
} }
fun disconnect() { fun disconnect() {
client.removeStateChannel(stateChannel)
client.disconnect() client.disconnect()
} }
} }
\ No newline at end of file
...@@ -7,10 +7,12 @@ import android.content.SharedPreferences ...@@ -7,10 +7,12 @@ import android.content.SharedPreferences
import chat.rocket.android.BuildConfig import chat.rocket.android.BuildConfig
import chat.rocket.android.app.RocketChatDatabase import chat.rocket.android.app.RocketChatDatabase
import chat.rocket.android.authentication.infraestructure.MemoryTokenRepository import chat.rocket.android.authentication.infraestructure.MemoryTokenRepository
import chat.rocket.android.authentication.infraestructure.SharedPreferencesMultiServerTokenRepository
import chat.rocket.android.infrastructure.LocalRepository import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.SharedPrefsLocalRepository import chat.rocket.android.infrastructure.SharedPrefsLocalRepository
import chat.rocket.android.server.domain.ChatRoomsRepository import chat.rocket.android.server.domain.ChatRoomsRepository
import chat.rocket.android.server.domain.CurrentServerRepository import chat.rocket.android.server.domain.CurrentServerRepository
import chat.rocket.android.server.domain.MultiServerTokenRepository
import chat.rocket.android.server.domain.SettingsRepository import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.infraestructure.MemoryChatRoomsRepository import chat.rocket.android.server.infraestructure.MemoryChatRoomsRepository
import chat.rocket.android.server.infraestructure.ServerDao import chat.rocket.android.server.infraestructure.ServerDao
...@@ -135,4 +137,10 @@ class AppModule { ...@@ -135,4 +137,10 @@ class AppModule {
fun provideMoshi(): Moshi { fun provideMoshi(): Moshi {
return Moshi.Builder().add(AppJsonAdapterFactory.INSTANCE).build() return Moshi.Builder().add(AppJsonAdapterFactory.INSTANCE).build()
} }
@Provides
@Singleton
fun provideMultiServerTokenRepository(repository: LocalRepository, moshi: Moshi): MultiServerTokenRepository {
return SharedPreferencesMultiServerTokenRepository(repository, moshi)
}
} }
\ No newline at end of file
...@@ -4,8 +4,8 @@ ext { ...@@ -4,8 +4,8 @@ ext {
compileSdk : 27, compileSdk : 27,
targetSdk : 27, targetSdk : 27,
buildTools : '27.0.3', buildTools : '27.0.3',
kotlin : '1.2.10', kotlin : '1.2.21',
coroutine : '0.21', coroutine : '0.22',
dokka : '0.9.15', dokka : '0.9.15',
// Main dependencies // Main dependencies
......
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