Commit 3d265b00 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update presenters

Add generic error, remove unneeded parammeters , add AuthenticationNavigator properties.
parent a122b55a
......@@ -8,7 +8,6 @@ import chat.rocket.common.RocketChatException
import chat.rocket.common.RocketChatTwoFactorException
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login
import timber.log.Timber
import javax.inject.Inject
class LoginPresenter @Inject constructor(private val view: LoginView,
......@@ -28,22 +27,24 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) {
view.showLoading()
try {
val token = client.login(usernameOrEmail, password)
// TODO Salve token?
client.login(usernameOrEmail, password) // TODO This function returns a user token so should we save it?
navigator.toChatList()
} catch (rocketChatException: RocketChatException) {
if (rocketChatException is RocketChatTwoFactorException) {
navigator.toTwoFA(navigator.currentServer!!, usernameOrEmail, password)
} catch (exception: RocketChatException) {
if (exception is RocketChatTwoFactorException) {
navigator.toTwoFA(usernameOrEmail, password)
} else {
val errorMessage = rocketChatException.message
if (errorMessage != null) {
view.showMessage(errorMessage)
val message = exception.message
if (message != null) {
view.showMessage(message)
} else {
view.showGenericErrorMessage()
}
}
} finally {
view.hideLoading()
}
view.hideLoading()
} else {
view.showNoInternetConnection()
}
......@@ -53,6 +54,6 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
}
fun signup() {
navigator.toSignUp(navigator.currentServer!!)
navigator.toSignUp()
}
}
\ No newline at end of file
......@@ -12,37 +12,39 @@ import chat.rocket.android.util.addFragmentBackStack
import chat.rocket.android.webview.webViewIntent
class AuthenticationNavigator(internal val activity: AuthenticationActivity, internal val context: Context) {
var currentServer: String? = null
lateinit var server: String
lateinit var usernameOrEmail: String
lateinit var password: String
fun toLogin(server: String) {
currentServer = server
this.server = server
activity.addFragmentBackStack("loginFragment", R.id.fragment_container) {
LoginFragment.newInstance(server)
LoginFragment.newInstance()
}
}
fun toTwoFA(server: String, username: String, password: String) {
currentServer = server
fun toTwoFA(usernameOrEmail: String, password: String) {
this.usernameOrEmail = usernameOrEmail
this.password = password
activity.addFragmentBackStack("twoFAFragment", R.id.fragment_container) {
TwoFAFragment.newInstance(server, username, password)
TwoFAFragment.newInstance()
}
}
fun toSignUp(server: String) {
currentServer = server
fun toSignUp() {
activity.addFragmentBackStack("signupFragment", R.id.fragment_container) {
SignupFragment.newInstance(server)
SignupFragment.newInstance()
}
}
fun toTermsOfService() {
val webPageUrl = currentServer + "/terms-of-service"
val webPageUrl = server + "/terms-of-service" // TODO Move to UrlHelper
activity.startActivity(context.webViewIntent(webPageUrl))
activity.overridePendingTransition(R.anim.slide_up, R.anim.hold)
}
fun toPrivacyPolicy() {
val webPageUrl = currentServer + "/privacy-policy"
val webPageUrl = server + "/privacy-policy" // TODO Move to UrlHelper
activity.startActivity(context.webViewIntent(webPageUrl))
activity.overridePendingTransition(R.anim.slide_up, R.anim.hold)
}
......
......@@ -18,10 +18,10 @@ class ServerPresenter @Inject constructor(private val view: ServerView,
view.showLoading()
// TODO - validate server URL and get server settings and info before going to Login screen
// client.connect(server)
//client.connect(server)
navigator.toLogin(server)
view.hideLoading()
navigator.toLogin(server)
} else {
view.showNoInternetConnection()
}
......
......@@ -8,7 +8,6 @@ import chat.rocket.common.RocketChatException
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login
import chat.rocket.core.internal.rest.signup
import timber.log.Timber
import javax.inject.Inject
class SignupPresenter @Inject constructor(private val view: SignupView,
......@@ -34,20 +33,21 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) {
view.showLoading()
try {
val user = client.signup(email, name, username, password)
// TODO Salve user?
val token = client.login(username, password)
// TODO Salve token?
client.signup(email, name, username, password) // TODO This function returns a user so should we save it?
client.login(username, password) // TODO This function returns a user token so should we save it?
navigator.toChatList()
} catch (rocketChatException: RocketChatException) {
val errorMessage = rocketChatException.message
} catch (exception: RocketChatException) {
val errorMessage = exception.message
if (errorMessage != null) {
view.showMessage(errorMessage)
} else {
view.showGenericErrorMessage()
}
} finally {
view.hideLoading()
}
view.hideLoading()
} else {
view.showNoInternetConnection()
}
......
......@@ -15,30 +15,31 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
private val navigator: AuthenticationNavigator) {
@Inject lateinit var client: RocketChatClient
// TODO: If the usernameOrEmail and password was informed by the user on the previous screen, then we should pass only the pin, like this: fun authenticate(pin: EditText)
fun authenticate(usernameOrEmail: String, password: String, twoFactorAuthenticationCode: String) {
fun authenticate(twoFactorAuthenticationCode: String) {
if (twoFactorAuthenticationCode.isBlank()) {
view.alertBlankTwoFactorAuthenticationCode()
} else {
launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) {
view.showLoading()
try {
val token = client.login(usernameOrEmail, password, twoFactorAuthenticationCode)
// TODO Salve token?
client.login(navigator.usernameOrEmail, navigator.password, twoFactorAuthenticationCode) // TODO This function returns a user token so should we save it?
navigator.toChatList()
} catch (rocketChatException: RocketChatException) {
if (rocketChatException is RocketChatAuthException) {
} catch (exception: RocketChatException) {
if (exception is RocketChatAuthException) {
view.alertInvalidTwoFactorAuthenticationCode()
} else {
val errorMessage = rocketChatException.message
if (errorMessage != null) {
view.showMessage(errorMessage)
val message = exception.message
if (message != null) {
view.showMessage(message)
} else {
view.showGenericErrorMessage()
}
}
} finally {
view.hideLoading()
}
view.hideLoading()
} else {
view.showNoInternetConnection()
}
......@@ -47,6 +48,6 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
}
fun signup() {
navigator.toSignUp(navigator.currentServer!!)
navigator.toSignUp()
}
}
\ 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