Unverified Commit 820abca1 authored by Lucio Maciel's avatar Lucio Maciel Committed by GitHub

Merge pull request #695 from RocketChat/spotlight

[NEW] Spotlight (local data)
parents 66cff88d fc0ebff2
package chat.rocket.android.chatrooms.presentation
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.server.domain.GetChatRoomsInteractor
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.SaveChatRoomsInteractor
import chat.rocket.android.server.infraestructure.RocketChatClientFactory
import chat.rocket.android.util.launchUI
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.chatRooms
import chat.rocket.core.model.ChatRoom
import kotlinx.coroutines.experimental.async
import javax.inject.Inject
class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
private val strategy: CancelStrategy,
private val serverInteractor: GetCurrentServerInteractor,
private val getChatRoomsInteractor: GetChatRoomsInteractor,
private val saveChatRoomsInteractor: SaveChatRoomsInteractor,
private val factory: RocketChatClientFactory) {
lateinit var client: RocketChatClient
fun chatRooms() {
// TODO - check for current server
client = factory.create(serverInteractor.get()!!)
val currentServer = serverInteractor.get()!!
client = factory.create(currentServer)
launchUI(strategy) {
view.showLoading()
val chatRooms = client.chatRooms().update
val openChatRooms = getOpenChatRooms(chatRooms)
val sortedOpenChatRooms = sortChatRooms(openChatRooms)
saveChatRoomsInteractor.save(currentServer, sortedOpenChatRooms)
view.showChatRooms(sortedOpenChatRooms.toMutableList())
view.hideLoading()
}
}
/**
* Get a ChatRoom list from local repository. ChatRooms returned are filtered by name.
*/
fun chatRoomsByName(name: String) {
val currentServer = serverInteractor.get()!!
launchUI(strategy) {
val roomList = getChatRoomsInteractor.getByName(currentServer, name)
view.showChatRooms(roomList.toMutableList())
}
}
private fun getOpenChatRooms(chatRooms: List<ChatRoom>): List<ChatRoom> {
return chatRooms.filter(ChatRoom::open)
}
......
......@@ -33,9 +33,18 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.chatRooms()
floating_search_view.setOnQueryChangeListener { oldQuery, newQuery ->
floating_search_view.showProgress()
presenter.chatRoomsByName(newQuery)
if (oldQuery.isNotEmpty() && newQuery.isEmpty()) {
floating_search_view.clearSuggestions()
floating_search_view.hideProgress()
}
}
}
override fun showChatRooms(dataSet: MutableList<ChatRoom>) {
floating_search_view.hideProgress()
activity?.apply {
recycler_view.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recycler_view.addItemDecoration(DividerItemDecoration(this, 144, 32))
......
......@@ -9,8 +9,10 @@ import chat.rocket.android.app.RocketChatDatabase
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.SharedPrefsLocalRepository
import chat.rocket.android.server.domain.ChatRoomsRepository
import chat.rocket.android.server.domain.CurrentServerRepository
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.infraestructure.MemoryChatRoomsRepository
import chat.rocket.android.server.infraestructure.MemorySettingsRepository
import chat.rocket.android.server.infraestructure.ServerDao
import chat.rocket.android.server.infraestructure.SharedPrefsCurrentServerRepository
......@@ -112,4 +114,10 @@ class AppModule {
fun provideSettingsRepository(): SettingsRepository {
return MemorySettingsRepository()
}
@Provides
@Singleton
fun provideChatRoomsRepository(): ChatRoomsRepository {
return MemoryChatRoomsRepository()
}
}
package chat.rocket.android.server.domain
import chat.rocket.core.model.ChatRoom
interface ChatRoomsRepository {
fun save(url: String, chatRooms: List<ChatRoom>)
fun get(url: String): List<ChatRoom>
}
\ No newline at end of file
package chat.rocket.android.server.domain
import chat.rocket.core.model.ChatRoom
import kotlinx.coroutines.experimental.async
import javax.inject.Inject
class GetChatRoomsInteractor @Inject constructor(private val repository: ChatRoomsRepository) {
fun get(url: String) = repository.get(url)
suspend fun getByName(url: String, name: String): List<ChatRoom> {
val chatRooms = async {
val allChatRooms = repository.get(url)
if (name.isEmpty()) {
return@async allChatRooms
}
return@async allChatRooms.filter {
it.name.contains(name, true)
}
}
return chatRooms.await()
}
}
\ No newline at end of file
package chat.rocket.android.server.domain
import chat.rocket.core.model.ChatRoom
import javax.inject.Inject
class SaveChatRoomsInteractor @Inject constructor(private val repository: ChatRoomsRepository) {
fun save(url: String, chatRooms: List<ChatRoom>) = repository.save(url, chatRooms)
}
\ No newline at end of file
package chat.rocket.android.server.infraestructure
import chat.rocket.android.server.domain.ChatRoomsRepository
import chat.rocket.core.model.ChatRoom
class MemoryChatRoomsRepository : ChatRoomsRepository {
val cache = HashMap<String, List<ChatRoom>>()
override fun save(url: String, chatRooms: List<ChatRoom>) {
//TODO: should diff the existing chatrooms and new chatroom dataset
cache[url] = chatRooms
}
override fun get(url: String): List<ChatRoom> = cache[url] ?: emptyList()
}
\ 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