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