Unverified Commit c53b4e73 authored by Rafael Kellermann Streit's avatar Rafael Kellermann Streit Committed by GitHub

Merge pull request #1483 from RocketChat/fix/cannot-change-server

[FIX] Cannot change server
parents 71652d27 bd5ed027
......@@ -38,20 +38,20 @@ import timber.log.Timber
import javax.inject.Inject
class MainPresenter @Inject constructor(
private val view: MainView,
private val strategy: CancelStrategy,
private val navigator: MainNavigator,
private val tokenRepository: TokenRepository,
private val serverInteractor: GetCurrentServerInteractor,
private val localRepository: LocalRepository,
private val navHeaderMapper: NavHeaderUiModelMapper,
private val saveAccountInteractor: SaveAccountInteractor,
private val getAccountsInteractor: GetAccountsInteractor,
private val removeAccountInteractor: RemoveAccountInteractor,
private val factory: RocketChatClientFactory,
dbManagerFactory: DatabaseManagerFactory,
getSettingsInteractor: GetSettingsInteractor,
managerFactory: ConnectionManagerFactory
private val view: MainView,
private val strategy: CancelStrategy,
private val navigator: MainNavigator,
private val tokenRepository: TokenRepository,
private val serverInteractor: GetCurrentServerInteractor,
private val localRepository: LocalRepository,
private val navHeaderMapper: NavHeaderUiModelMapper,
private val saveAccountInteractor: SaveAccountInteractor,
private val getAccountsInteractor: GetAccountsInteractor,
private val removeAccountInteractor: RemoveAccountInteractor,
private val factory: RocketChatClientFactory,
dbManagerFactory: DatabaseManagerFactory,
getSettingsInteractor: GetSettingsInteractor,
managerFactory: ConnectionManagerFactory
) : CheckServerPresenter(strategy, factory, view = view) {
private val currentServer = serverInteractor.get()!!
private val manager = managerFactory.create(currentServer)
......@@ -69,16 +69,34 @@ class MainPresenter @Inject constructor(
fun toCreateChannel() = navigator.toCreateChannel()
fun loadServerAccounts() {
launchUI(strategy) {
try {
view.setupServerAccountList(getAccountsInteractor.get())
} catch (ex: Exception) {
when (ex) {
is RocketChatAuthException -> logout()
else -> {
Timber.d(ex, "Error loading serve accounts")
ex.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
}
}
}
}
}
fun loadCurrentInfo() {
checkServerInfo(currentServer)
launchUI(strategy) {
try {
val me = retryIO("me") {
client.me()
}
val me = retryIO("me") { client.me() }
val model = navHeaderMapper.mapToUiModel(me)
saveAccount(model)
view.setupNavHeader(model, getAccountsInteractor.get())
view.setupUserAccountInfo(model)
} catch (ex: Exception) {
when (ex) {
is RocketChatAuthException -> {
......@@ -208,8 +226,6 @@ class MainPresenter @Inject constructor(
}
}
private suspend fun updateMyself(myself: Myself) {
val model = navHeaderMapper.mapToUiModel(myself)
view.setupNavHeader(model, getAccountsInteractor.get())
}
private fun updateMyself(myself: Myself) =
view.setupUserAccountInfo(navHeaderMapper.mapToUiModel(myself))
}
\ No newline at end of file
......@@ -16,17 +16,24 @@ interface MainView : MessageView, VersionCheckView {
fun showUserStatus(userStatus: UserStatus)
/**
* Setups the navigation header.
* Setups the user account info (displayed in the nav. header)
*
* @param uiModel The [NavHeaderUiModel].
* @param accounts The list of accounts.
*/
fun setupNavHeader(uiModel: NavHeaderUiModel, accounts: List<Account>)
fun setupUserAccountInfo(uiModel: NavHeaderUiModel)
/**
* Setups the server account list.
*
* @param serverAccountList The list of server accounts.
*/
fun setupServerAccountList(serverAccountList: List<Account>)
fun closeServerSelection()
fun invalidateToken(token: String)
fun showProgress()
fun hideProgress()
}
\ No newline at end of file
......@@ -10,7 +10,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.Gravity
import android.view.MenuItem
import android.view.View
import androidx.annotation.IdRes
import androidx.drawerlayout.widget.DrawerLayout
import chat.rocket.android.BuildConfig
......@@ -56,6 +55,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
private var expanded = false
private val headerLayout by lazy { view_navigation.getHeaderView(0) }
private var chatRoomId: String? = null
private var progressDialog : ProgressDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
......@@ -75,6 +75,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
chatRoomId = intent.getStringExtra(INTENT_CHAT_ROOM_ID)
presenter.connect()
presenter.loadServerAccounts()
presenter.loadCurrentInfo()
setupToolbar()
setupNavigationView()
......@@ -119,8 +120,7 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
}
}
override fun setupNavHeader(uiModel: NavHeaderUiModel, accounts: List<Account>) {
Timber.d("Setting up nav header: $uiModel")
override fun setupUserAccountInfo(uiModel: NavHeaderUiModel) {
with(headerLayout) {
with(uiModel) {
if (userStatus != null) {
......@@ -139,10 +139,43 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
}
text_server_url.text = uiModel.serverUrl
}
setupAccountsList(headerLayout, accounts)
}
}
override fun setupServerAccountList(serverAccountList: List<Account>) {
accounts_list.layoutManager = LinearLayoutManager(this)
accounts_list.adapter = AccountsAdapter(serverAccountList, object : Selector {
override fun onStatusSelected(userStatus: UserStatus) {
presenter.changeDefaultStatus(userStatus)
}
override fun onAccountSelected(serverUrl: String) {
presenter.changeServer(serverUrl)
}
override fun onAddedAccountSelected() {
presenter.addNewServer()
}
})
headerLayout.account_container.setOnClickListener {
it.image_account_expand.rotateBy(180f)
if (expanded) {
accounts_list.fadeOut()
} else {
accounts_list.fadeIn()
}
expanded = !expanded
}
headerLayout.image_avatar.setOnClickListener {
view_navigation.menu.findItem(R.id.action_update_profile).isChecked = true
presenter.toUserProfile()
drawer_layout.closeDrawer(Gravity.START)
}
}
override fun closeServerSelection() {
view_navigation.getHeaderView(0).account_container.performClick()
}
......@@ -174,9 +207,8 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
.show()
}
override fun invalidateToken(token: String) {
override fun invalidateToken(token: String) =
FirebaseInstanceId.getInstance().deleteToken(token, FirebaseMessaging.INSTANCE_ID_SCOPE)
}
override fun showMessage(resId: Int) = showToast(resId)
......@@ -221,57 +253,14 @@ class MainActivity : AppCompatActivity(), MainView, HasActivityInjector,
}
}
private fun setupAccountsList(header: View, accounts: List<Account>) {
accounts_list.layoutManager = LinearLayoutManager(this)
accounts_list.adapter = AccountsAdapter(accounts, object : Selector {
override fun onStatusSelected(userStatus: UserStatus) {
presenter.changeDefaultStatus(userStatus)
}
override fun onAccountSelected(serverUrl: String) {
presenter.changeServer(serverUrl)
}
override fun onAddedAccountSelected() {
presenter.addNewServer()
}
})
header.account_container.setOnClickListener {
header.image_account_expand.rotateBy(180f)
if (expanded) {
accounts_list.fadeOut()
} else {
accounts_list.fadeIn()
}
expanded = !expanded
}
header.image_avatar.setOnClickListener {
view_navigation.menu.findItem(R.id.action_update_profile).isChecked = true
presenter.toUserProfile()
drawer_layout.closeDrawer(Gravity.START)
}
}
fun getDrawerLayout(): DrawerLayout {
return drawer_layout
}
fun getDrawerLayout(): DrawerLayout = drawer_layout
fun openDrawer() {
drawer_layout.openDrawer(Gravity.START)
}
fun openDrawer() = drawer_layout.openDrawer(Gravity.START)
fun closeDrawer() {
drawer_layout.closeDrawer(Gravity.START)
}
fun closeDrawer() = drawer_layout.closeDrawer(Gravity.START)
fun setCheckedNavDrawerItem(@IdRes item: Int) {
view_navigation.setCheckedItem(item)
}
fun setCheckedNavDrawerItem(@IdRes item: Int) = view_navigation.setCheckedItem(item)
private var progressDialog : ProgressDialog? = null
override fun showProgress() {
progressDialog = ProgressDialog.show(this, getString(R.string.app_name), getString(R.string.msg_log_out), true, false)
}
......
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