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