Commit 8eaf1c4b authored by Leonardo Aramaki's avatar Leonardo Aramaki

Logout and clear all data related to the current server

parent f09e71c4
...@@ -3,6 +3,7 @@ package chat.rocket.android.authentication.infraestructure ...@@ -3,6 +3,7 @@ package chat.rocket.android.authentication.infraestructure
import chat.rocket.android.authentication.domain.model.TokenModel import chat.rocket.android.authentication.domain.model.TokenModel
import chat.rocket.android.dagger.scope.PerActivity import chat.rocket.android.dagger.scope.PerActivity
import chat.rocket.android.infrastructure.LocalRepository import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.LocalRepository.Companion.TOKEN_KEY
import chat.rocket.android.server.domain.MultiServerTokenRepository import chat.rocket.android.server.domain.MultiServerTokenRepository
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
...@@ -28,6 +29,7 @@ class SharedPreferencesMultiServerTokenRepository(private val repository: LocalR ...@@ -28,6 +29,7 @@ class SharedPreferencesMultiServerTokenRepository(private val repository: LocalR
repository.save("$TOKEN_KEY$server", adapter.toJson(token)) repository.save("$TOKEN_KEY$server", adapter.toJson(token))
} }
} override fun clear(server: String) {
repository.clear("$TOKEN_KEY$server")
const val TOKEN_KEY = "token_" }
\ No newline at end of file }
\ No newline at end of file
package chat.rocket.android.chatrooms.presentation package chat.rocket.android.chatrooms.presentation
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.GetChatRoomsInteractor import chat.rocket.android.server.domain.GetChatRoomsInteractor
import chat.rocket.android.server.domain.GetCurrentServerInteractor import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.SaveChatRoomsInteractor import chat.rocket.android.server.domain.SaveChatRoomsInteractor
...@@ -10,6 +11,8 @@ import chat.rocket.core.RocketChatClient ...@@ -10,6 +11,8 @@ import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.model.Subscription import chat.rocket.core.internal.model.Subscription
import chat.rocket.core.internal.realtime.* import chat.rocket.core.internal.realtime.*
import chat.rocket.core.internal.rest.chatRooms import chat.rocket.core.internal.rest.chatRooms
import chat.rocket.core.internal.rest.logout
import chat.rocket.core.internal.rest.unregisterPushToken
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.*
...@@ -22,6 +25,7 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView, ...@@ -22,6 +25,7 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
private val serverInteractor: GetCurrentServerInteractor, private val serverInteractor: GetCurrentServerInteractor,
private val getChatRoomsInteractor: GetChatRoomsInteractor, private val getChatRoomsInteractor: GetChatRoomsInteractor,
private val saveChatRoomsInteractor: SaveChatRoomsInteractor, private val saveChatRoomsInteractor: SaveChatRoomsInteractor,
private val localRepository: LocalRepository,
factory: RocketChatClientFactory) { factory: RocketChatClientFactory) {
private val client: RocketChatClient = factory.create(serverInteractor.get()!!) private val client: RocketChatClient = factory.create(serverInteractor.get()!!)
private val currentServer = serverInteractor.get()!! private val currentServer = serverInteractor.get()!!
...@@ -245,4 +249,29 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView, ...@@ -245,4 +249,29 @@ class ChatRoomsPresenter @Inject constructor(private val view: ChatRoomsView,
fun disconnect() { fun disconnect() {
client.disconnect() client.disconnect()
} }
fun logout() {
launchUI(strategy) {
try {
clearTokens()
client.logout()
//TODO: Add the code to unsubscribe to all subscriptions.
client.disconnect()
view.onLogOut()
} catch (e: Exception) {
Timber.e(e)
view.showMessage(e.message!!)
}
}
}
private suspend fun clearTokens() {
serverInteractor.clear()
val pushToken = localRepository.get(LocalRepository.KEY_PUSH_TOKEN)
if (pushToken != null) {
client.unregisterPushToken(pushToken)
localRepository.clear(LocalRepository.KEY_PUSH_TOKEN)
}
localRepository.clearAllFromServer(currentServer)
}
} }
\ No newline at end of file
...@@ -12,4 +12,9 @@ interface ChatRoomsView : LoadingView, MessageView { ...@@ -12,4 +12,9 @@ interface ChatRoomsView : LoadingView, MessageView {
* @param dataSet The data set to show. * @param dataSet The data set to show.
*/ */
suspend fun updateChatRooms(newDataSet: List<ChatRoom>) suspend fun updateChatRooms(newDataSet: List<ChatRoom>)
/**
* User has successfully logged out from the current server.
*/
fun onLogOut()
} }
\ No newline at end of file
package chat.rocket.android.chatrooms.ui package chat.rocket.android.chatrooms.ui
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
...@@ -10,6 +11,7 @@ import android.support.v7.widget.SearchView ...@@ -10,6 +11,7 @@ import android.support.v7.widget.SearchView
import android.view.* import android.view.*
import android.widget.Toast import android.widget.Toast
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.chatrooms.presentation.ChatRoomsPresenter import chat.rocket.android.chatrooms.presentation.ChatRoomsPresenter
import chat.rocket.android.chatrooms.presentation.ChatRoomsView import chat.rocket.android.chatrooms.presentation.ChatRoomsView
import chat.rocket.android.util.setVisibility import chat.rocket.android.util.setVisibility
...@@ -90,6 +92,13 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView { ...@@ -90,6 +92,13 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
}) })
} }
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.action_logout -> presenter.logout()
}
return true
}
override suspend fun updateChatRooms(newDataSet: List<ChatRoom>) { override suspend fun updateChatRooms(newDataSet: List<ChatRoom>) {
activity.apply { activity.apply {
launch(UI) { launch(UI) {
...@@ -112,6 +121,15 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView { ...@@ -112,6 +121,15 @@ class ChatRoomsFragment : Fragment(), ChatRoomsView {
override fun showGenericErrorMessage() = showMessage(getString(R.string.msg_generic_error)) override fun showGenericErrorMessage() = showMessage(getString(R.string.msg_generic_error))
override fun onLogOut() {
activity?.apply {
finish()
val intent = Intent(this, AuthenticationActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
private fun queryChatRoomsByName(name: String?): Boolean { private fun queryChatRoomsByName(name: String?): Boolean {
presenter.chatRoomsByName(name ?: "") presenter.chatRoomsByName(name ?: "")
return true return true
......
...@@ -3,10 +3,16 @@ package chat.rocket.android.infrastructure ...@@ -3,10 +3,16 @@ package chat.rocket.android.infrastructure
interface LocalRepository { interface LocalRepository {
companion object { companion object {
val KEY_PUSH_TOKEN = "KEY_PUSH_TOKEN" const val KEY_PUSH_TOKEN = "KEY_PUSH_TOKEN"
const val TOKEN_KEY = "token_"
const val SETTINGS_KEY = "settings_"
} }
fun save(key: String, value: String?) fun save(key: String, value: String?)
fun get(key: String): String? fun get(key: String): String?
fun clear(key: String)
fun clearAllFromServer(server: String)
} }
\ No newline at end of file
...@@ -11,4 +11,14 @@ class SharedPrefsLocalRepository(private val preferences: SharedPreferences) : L ...@@ -11,4 +11,14 @@ class SharedPrefsLocalRepository(private val preferences: SharedPreferences) : L
override fun get(key: String): String? { override fun get(key: String): String? {
return preferences.getString(key, null) return preferences.getString(key, null)
} }
override fun clear(key: String) {
preferences.edit().remove(key).apply()
}
override fun clearAllFromServer(server: String) {
clear(LocalRepository.KEY_PUSH_TOKEN)
clear(LocalRepository.TOKEN_KEY + server)
clear(LocalRepository.SETTINGS_KEY + server)
}
} }
\ No newline at end of file
...@@ -3,4 +3,5 @@ package chat.rocket.android.server.domain ...@@ -3,4 +3,5 @@ package chat.rocket.android.server.domain
interface CurrentServerRepository { interface CurrentServerRepository {
fun save(url: String) fun save(url: String)
fun get(): String? fun get(): String?
fun clear()
} }
\ No newline at end of file
...@@ -4,4 +4,8 @@ import javax.inject.Inject ...@@ -4,4 +4,8 @@ import javax.inject.Inject
class GetCurrentServerInteractor @Inject constructor(private val repository: CurrentServerRepository) { class GetCurrentServerInteractor @Inject constructor(private val repository: CurrentServerRepository) {
fun get(): String? = repository.get() fun get(): String? = repository.get()
fun clear() {
repository.clear()
}
} }
\ No newline at end of file
...@@ -6,4 +6,6 @@ interface MultiServerTokenRepository { ...@@ -6,4 +6,6 @@ interface MultiServerTokenRepository {
fun get(server: String): TokenModel? fun get(server: String): TokenModel?
fun save(server: String, token: TokenModel) fun save(server: String, token: TokenModel)
fun clear(server: String)
} }
\ No newline at end of file
package chat.rocket.android.server.infraestructure package chat.rocket.android.server.infraestructure
import chat.rocket.android.infrastructure.LocalRepository import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.LocalRepository.Companion.SETTINGS_KEY
import chat.rocket.android.server.domain.SettingsRepository import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.core.internal.SettingsAdapter import chat.rocket.core.internal.SettingsAdapter
import chat.rocket.core.model.Value import chat.rocket.core.model.Value
...@@ -21,8 +22,4 @@ class SharedPreferencesSettingsRepository(private val localRespository: LocalRep ...@@ -21,8 +22,4 @@ class SharedPreferencesSettingsRepository(private val localRespository: LocalRep
return null return null
} }
companion object {
private const val SETTINGS_KEY = "settings_"
}
} }
\ No newline at end of file
...@@ -16,4 +16,8 @@ class SharedPrefsCurrentServerRepository(private val preferences: SharedPreferen ...@@ -16,4 +16,8 @@ class SharedPrefsCurrentServerRepository(private val preferences: SharedPreferen
companion object { companion object {
private const val CURRENT_SERVER_KEY = "current_server" private const val CURRENT_SERVER_KEY = "current_server"
} }
override fun clear() {
preferences.edit().remove(CURRENT_SERVER_KEY).apply()
}
} }
\ No newline at end of file
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
<item <item
android:id="@+id/action_search" android:id="@+id/action_search"
android:icon="@drawable/ic_search_white_24px" android:icon="@drawable/ic_search_white_24px"
android:title="@string/search" android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView" app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" /> app:showAsAction="ifRoom|collapseActionView" />
<item android:id="@+id/action_logout"
android:title="@string/action_logout"
app:showAsAction="never"/>
</menu> </menu>
\ No newline at end of file
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
<string name="action_send">Enviar</string> <string name="action_send">Enviar</string>
<string name="action_terms_of_service">Termos de Serviço</string> <string name="action_terms_of_service">Termos de Serviço</string>
<string name="action_privacy_policy">Política de Privacidade</string> <string name="action_privacy_policy">Política de Privacidade</string>
<string name="search">Pesquisar</string> <string name="action_search">Pesquisar</string>
<string name="action_logout">Sair</string>
<!-- Regular information messages --> <!-- Regular information messages -->
<string name="msg_no_internet_connection">Sem conexão à internet</string> <string name="msg_no_internet_connection">Sem conexão à internet</string>
......
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
<string name="action_send">Send</string> <string name="action_send">Send</string>
<string name="action_terms_of_service">Terms of Service</string> <string name="action_terms_of_service">Terms of Service</string>
<string name="action_privacy_policy">Privacy Policy</string> <string name="action_privacy_policy">Privacy Policy</string>
<string name="search">Search</string> <string name="action_search">Search</string>
<string name="action_logout">Log Out</string>
<!-- Regular information messages --> <!-- Regular information messages -->
<string name="msg_no_internet_connection">No internet connection</string> <string name="msg_no_internet_connection">No internet connection</string>
......
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