Commit b5e6e327 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Remove context from presenter and create InternetView interface

parent f5a13b4e
...@@ -2,6 +2,7 @@ package chat.rocket.android.authentication.login.presentation ...@@ -2,6 +2,7 @@ package chat.rocket.android.authentication.login.presentation
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.RocketChatTwoFactorException import chat.rocket.common.RocketChatTwoFactorException
...@@ -26,24 +27,28 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -26,24 +27,28 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
else -> { else -> {
launchUI(strategy) { launchUI(strategy) {
view.showLoading() view.showLoading()
try { if (NetworkHelper.hasInternetAccess()) {
val token = client.login(usernameOrEmail, password) try {
Timber.d("Created token: $token") val token = client.login(usernameOrEmail, password)
navigator.toChatList() Timber.d("Created token: $token")
} catch (rocketChatException: RocketChatException) { navigator.toChatList()
when (rocketChatException) { } catch (rocketChatException: RocketChatException) {
is RocketChatTwoFactorException -> { when (rocketChatException) {
navigator.toTwoFA(navigator.currentServer!!, usernameOrEmail, password) is RocketChatTwoFactorException -> {
} navigator.toTwoFA(navigator.currentServer!!, usernameOrEmail, password)
else -> { }
val errorMessage = rocketChatException.message else -> {
if (errorMessage != null) { val errorMessage = rocketChatException.message
view.showMessage(errorMessage) if (errorMessage != null) {
view.showMessage(errorMessage)
}
} }
} }
} finally {
view.hideLoading()
} }
} finally { } else {
view.hideLoading() view.showNoInternetConnection()
} }
} }
} }
......
package chat.rocket.android.authentication.login.presentation package chat.rocket.android.authentication.login.presentation
import chat.rocket.android.core.behaviours.InternetView
import chat.rocket.android.core.behaviours.LoadingView import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView import chat.rocket.android.core.behaviours.MessageView
interface LoginView : LoadingView, MessageView { interface LoginView : LoadingView, MessageView, InternetView {
/** /**
* Shows the oauth view if the server settings allow the login via social accounts. * Shows the oauth view if the server settings allow the login via social accounts.
......
...@@ -182,6 +182,10 @@ class LoginFragment : Fragment(), LoginView { ...@@ -182,6 +182,10 @@ class LoginFragment : Fragment(), LoginView {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show() Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
} }
override fun showNoInternetConnection() {
Toast.makeText(activity, getString(R.string.msg_no_internet_connection), Toast.LENGTH_SHORT).show()
}
private fun tintEditTextDrawableStart() { private fun tintEditTextDrawableStart() {
activity?.applicationContext?.apply { activity?.applicationContext?.apply {
val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_assignment_ind_black_24dp, this) val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_assignment_ind_black_24dp, this)
......
package chat.rocket.android.authentication.server.presentation package chat.rocket.android.authentication.server.presentation
import android.content.Context
import chat.rocket.android.R
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper import chat.rocket.android.helper.NetworkHelper
...@@ -14,7 +12,7 @@ class ServerPresenter @Inject constructor(private val view: ServerView, ...@@ -14,7 +12,7 @@ class ServerPresenter @Inject constructor(private val view: ServerView,
private val navigator: AuthenticationNavigator) { private val navigator: AuthenticationNavigator) {
@Inject lateinit var client: RocketChatClient @Inject lateinit var client: RocketChatClient
fun connect(context: Context, server: String) { fun connect(server: String) {
launchUI(strategy) { launchUI(strategy) {
if (NetworkHelper.hasInternetAccess()) { if (NetworkHelper.hasInternetAccess()) {
view.showLoading() view.showLoading()
...@@ -25,7 +23,7 @@ class ServerPresenter @Inject constructor(private val view: ServerView, ...@@ -25,7 +23,7 @@ class ServerPresenter @Inject constructor(private val view: ServerView,
view.hideLoading() view.hideLoading()
navigator.toLogin(server) navigator.toLogin(server)
} else { } else {
view.showMessage(context.getString(R.string.msg_no_internet_connection)) view.showNoInternetConnection()
} }
} }
} }
......
package chat.rocket.android.authentication.server.presentation package chat.rocket.android.authentication.server.presentation
import chat.rocket.android.core.behaviours.InternetView
import chat.rocket.android.core.behaviours.LoadingView import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView import chat.rocket.android.core.behaviours.MessageView
interface ServerView : LoadingView, MessageView interface ServerView : LoadingView, MessageView, InternetView
\ No newline at end of file \ No newline at end of file
...@@ -43,7 +43,7 @@ class ServerFragment : Fragment(), ServerView { ...@@ -43,7 +43,7 @@ class ServerFragment : Fragment(), ServerView {
activity?.applicationContext?.apply { activity?.applicationContext?.apply {
button_connect.setOnClickListener { button_connect.setOnClickListener {
val url = text_server_url.textContent.ifEmpty(text_server_url.hintContent) val url = text_server_url.textContent.ifEmpty(text_server_url.hintContent)
presenter.connect(this, text_server_protocol.textContent + url) presenter.connect(text_server_protocol.textContent + url)
} }
} }
} }
...@@ -68,4 +68,8 @@ class ServerFragment : Fragment(), ServerView { ...@@ -68,4 +68,8 @@ class ServerFragment : Fragment(), ServerView {
override fun showMessage(message: String) { override fun showMessage(message: String) {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show() Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
} }
override fun showNoInternetConnection() {
Toast.makeText(activity, getString(R.string.msg_no_internet_connection), Toast.LENGTH_SHORT).show()
}
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ package chat.rocket.android.authentication.signup.presentation ...@@ -2,6 +2,7 @@ package chat.rocket.android.authentication.signup.presentation
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.core.lifecycle.CancelStrategy import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.NetworkHelper
import chat.rocket.android.util.launchUI import chat.rocket.android.util.launchUI
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
...@@ -31,22 +32,26 @@ class SignupPresenter @Inject constructor(private val view: SignupView, ...@@ -31,22 +32,26 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
} }
else -> { else -> {
launchUI(strategy) { launchUI(strategy) {
view.showLoading() if (NetworkHelper.hasInternetAccess()) {
try { view.showLoading()
val user = client.signup(email, name, username, password) try {
Timber.d("Created user: $user") val user = client.signup(email, name, username, password)
Timber.d("Created user: $user")
val token = client.login(username, password) val token = client.login(username, password)
Timber.d("Logged in. Token: $token") Timber.d("Logged in. Token: $token")
navigator.toChatList() navigator.toChatList()
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
val errorMessage = ex.message val errorMessage = ex.message
if (errorMessage != null) { if (errorMessage != null) {
view.showMessage(errorMessage) view.showMessage(errorMessage)
}
} finally {
view.hideLoading()
} }
} finally { } else {
view.hideLoading() view.showNoInternetConnection()
} }
} }
} }
......
package chat.rocket.android.authentication.signup.presentation package chat.rocket.android.authentication.signup.presentation
import chat.rocket.android.core.behaviours.InternetView
import chat.rocket.android.core.behaviours.LoadingView import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView import chat.rocket.android.core.behaviours.MessageView
interface SignupView : LoadingView, MessageView { interface SignupView : LoadingView, MessageView, InternetView {
/** /**
* Alerts the user about a blank name. * Alerts the user about a blank name.
......
...@@ -116,6 +116,10 @@ class SignupFragment : Fragment(), SignupView { ...@@ -116,6 +116,10 @@ class SignupFragment : Fragment(), SignupView {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show() Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
} }
override fun showNoInternetConnection() {
Toast.makeText(activity, getString(R.string.msg_no_internet_connection), Toast.LENGTH_SHORT).show()
}
private fun tintEditTextDrawableStart() { private fun tintEditTextDrawableStart() {
val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_person_black_24dp, appContext) val personDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_person_black_24dp, appContext)
val atDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_at_black_24dp, appContext) val atDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_at_black_24dp, appContext)
......
package chat.rocket.android.authentication.twofactor.presentation package chat.rocket.android.authentication.twofactor.presentation
import chat.rocket.android.core.behaviours.InternetView
import chat.rocket.android.core.behaviours.LoadingView import chat.rocket.android.core.behaviours.LoadingView
import chat.rocket.android.core.behaviours.MessageView import chat.rocket.android.core.behaviours.MessageView
interface TwoFAView : LoadingView, MessageView { interface TwoFAView : LoadingView, MessageView, InternetView {
/** /**
* Alerts the user about a blank two factor authentication code. * Alerts the user about a blank two factor authentication code.
......
...@@ -84,6 +84,10 @@ class TwoFAFragment : Fragment(), TwoFAView { ...@@ -84,6 +84,10 @@ class TwoFAFragment : Fragment(), TwoFAView {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show() Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
} }
override fun showNoInternetConnection() {
Toast.makeText(activity, getString(R.string.msg_no_internet_connection), Toast.LENGTH_SHORT).show()
}
private fun tintEditTextDrawableStart() { private fun tintEditTextDrawableStart() {
activity?.applicationContext?.apply { activity?.applicationContext?.apply {
val lockDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_vpn_key_black_24dp, this) val lockDrawable = DrawableHelper.getDrawableFromId(R.drawable.ic_vpn_key_black_24dp, this)
......
package chat.rocket.android.core.behaviours
interface InternetView {
fun showNoInternetConnection()
}
\ No newline at end of file
package chat.rocket.android.core.behaviours package chat.rocket.android.core.behaviours
interface LoadingView { interface LoadingView {
fun showLoading() fun showLoading()
fun hideLoading() fun hideLoading()
} }
\ No newline at end of file
package chat.rocket.android.core.behaviours package chat.rocket.android.core.behaviours
interface MessageView { interface MessageView {
fun showMessage(message: String) fun showMessage(message: String)
} }
\ 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