Commit 7e6f4841 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Gets the room from DB instead of in-memory.

parent 0f21d5e0
...@@ -3,34 +3,54 @@ package chat.rocket.android.mentions.di ...@@ -3,34 +3,54 @@ package chat.rocket.android.mentions.di
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.dagger.scope.PerFragment import chat.rocket.android.dagger.scope.PerFragment
import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.db.DatabaseManagerFactory
import chat.rocket.android.mentions.presentention.MentionsView import chat.rocket.android.mentions.presentention.MentionsView
import chat.rocket.android.mentions.ui.MentionsFragment import chat.rocket.android.mentions.ui.MentionsFragment
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import kotlinx.coroutines.experimental.Job import kotlinx.coroutines.experimental.Job
import javax.inject.Named
@Module @Module
class MentionsFragmentModule { class MentionsFragmentModule {
@Provides @Provides
@PerFragment @PerFragment
fun provideJob() = Job() fun provideMentionsView(frag: MentionsFragment): MentionsView {
return frag
}
@Provides @Provides
@PerFragment @PerFragment
fun provideLifecycleOwner(frag: MentionsFragment): LifecycleOwner { @Named("currentServer")
return frag fun provideCurrentServer(currentServerInteractor: GetCurrentServerInteractor): String {
return currentServerInteractor.get()!!
} }
@Provides @Provides
@PerFragment @PerFragment
fun provideCancelStrategy(owner: LifecycleOwner, jobs: Job): CancelStrategy { fun provideDatabaseManager(
return CancelStrategy(owner, jobs) factory: DatabaseManagerFactory,
@Named("currentServer") currentServer: String
): DatabaseManager {
return factory.create(currentServer)
} }
@Provides @Provides
@PerFragment @PerFragment
fun provideMentionsView(frag: MentionsFragment): MentionsView { fun provideJob() = Job()
@Provides
@PerFragment
fun provideLifecycleOwner(frag: MentionsFragment): LifecycleOwner {
return frag return frag
} }
@Provides
@PerFragment
fun provideCancelStrategy(owner: LifecycleOwner, jobs: Job): CancelStrategy {
return CancelStrategy(owner, jobs)
}
} }
\ No newline at end of file
...@@ -2,39 +2,40 @@ package chat.rocket.android.mentions.presentention ...@@ -2,39 +2,40 @@ package chat.rocket.android.mentions.presentention
import chat.rocket.android.chatroom.uimodel.UiModelMapper import chat.rocket.android.chatroom.uimodel.UiModelMapper
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.server.domain.ChatRoomsInteractor import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.extensions.launchUI import chat.rocket.android.util.extensions.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
import chat.rocket.core.internal.rest.getMentions import chat.rocket.core.internal.rest.getMentions
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Named
class MentionsPresenter @Inject constructor( class MentionsPresenter @Inject constructor(
private val view: MentionsView, private val view: MentionsView,
private val dbManager: DatabaseManager,
@Named("currentServer") private val currentServer: String,
private val strategy: CancelStrategy, private val strategy: CancelStrategy,
private val roomsInteractor: ChatRoomsInteractor,
private val mapper: UiModelMapper, private val mapper: UiModelMapper,
val serverInteractor: GetCurrentServerInteractor,
val factory: RocketChatClientFactory val factory: RocketChatClientFactory
) { ) {
private val serverUrl = serverInteractor.get()!! private val client = factory.create(currentServer)
private val client = factory.create(serverUrl)
private var offset: Long = 0 private var offset: Long = 0
/** /**
* Loads all mentions for the given room id. * Loads all the authenticated user mentions for the given room id.
* *
* @param roomId The id of the room to get the mentions from. * @param roomId The id of the room to get the mentions for the authenticated user from.
*/ */
fun loadMentions(roomId: String) { fun loadMentions(roomId: String) {
launchUI(strategy) { launchUI(strategy) {
try { try {
view.showLoading() view.showLoading()
roomsInteractor.getById(serverUrl, roomId)?.let { dbManager.getRoom(roomId)?.let {
val mentions = client.getMentions(roomId, it.type, offset, 30) val mentions =
client.getMentions(roomId, roomTypeOf(it.chatRoom.type), offset, 30)
val mentionsList = mapper.map(mentions.result, asNotReversed = true) val mentionsList = mapper.map(mentions.result, asNotReversed = true)
view.showMentions(mentionsList) view.showMentions(mentionsList)
offset += 1 * 30 offset += 1 * 30
...@@ -43,6 +44,11 @@ class MentionsPresenter @Inject constructor( ...@@ -43,6 +44,11 @@ class MentionsPresenter @Inject constructor(
} }
} catch (exception: RocketChatException) { } catch (exception: RocketChatException) {
Timber.e(exception) Timber.e(exception)
exception.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
} finally { } finally {
view.hideLoading() view.hideLoading()
} }
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".mentions.ui.MentionsFragment"> tools:context=".mentions.ui.MentionsFragment">
......
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