Commit 2b88395e authored by Lucio Maciel's avatar Lucio Maciel

Signup support

parent 69989fb6
package chat.rocket.android.authentication.di package chat.rocket.android.authentication.di
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.android.authentication.presentation.AuthenticationNavigator import chat.rocket.android.authentication.presentation.AuthenticationNavigator
import chat.rocket.android.authentication.ui.AuthenticationActivity import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.dagger.scope.PerActivity import chat.rocket.android.dagger.scope.PerActivity
...@@ -12,4 +13,10 @@ class AuthenticationModule { ...@@ -12,4 +13,10 @@ class AuthenticationModule {
@Provides @Provides
@PerActivity @PerActivity
fun provideAuthenticationNavigator(activity: AuthenticationActivity) = AuthenticationNavigator(activity) fun provideAuthenticationNavigator(activity: AuthenticationActivity) = AuthenticationNavigator(activity)
@Provides
@PerActivity
fun provideAuthTokenRepository(): AuthTokenRepository {
return AuthTokenRepository()
}
} }
package chat.rocket.android.authentication.di
import chat.rocket.android.authentication.presentation.SignupView
import chat.rocket.android.authentication.ui.SignupFragment
import dagger.Module
import dagger.Provides
@Module
class SignupFragmentModule {
@Provides
fun signupView(frag: SignupFragment): SignupView {
return frag
}
}
package chat.rocket.android.authentication.di
import chat.rocket.android.authentication.ui.SignupFragment
import dagger.Module
import dagger.android.ContributesAndroidInjector
@Module abstract class SignupFragmentProvider {
@ContributesAndroidInjector(modules = arrayOf(SignupFragmentModule::class))
abstract fun provideSignupFragment(): SignupFragment
}
package chat.rocket.android.authentication.infraestructure
import chat.rocket.common.model.Token
import chat.rocket.core.TokenRepository
class AuthTokenRepository : TokenRepository {
var savedToken: Token? = null
override fun get(): Token? {
return savedToken
}
override fun save(token: Token) {
savedToken = token
}
}
\ No newline at end of file
...@@ -5,7 +5,7 @@ import chat.rocket.android.R ...@@ -5,7 +5,7 @@ import chat.rocket.android.R
import chat.rocket.android.app.MainActivity import chat.rocket.android.app.MainActivity
import chat.rocket.android.authentication.ui.AuthenticationActivity import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.authentication.ui.LoginFragment import chat.rocket.android.authentication.ui.LoginFragment
import chat.rocket.android.authentication.ui.SignUpFragment import chat.rocket.android.authentication.ui.SignupFragment
import chat.rocket.android.util.addFragmentBackStack import chat.rocket.android.util.addFragmentBackStack
class AuthenticationNavigator(internal val activity: AuthenticationActivity) { class AuthenticationNavigator(internal val activity: AuthenticationActivity) {
...@@ -18,7 +18,7 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity) { ...@@ -18,7 +18,7 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity) {
} }
} }
fun toChatRoom() { fun toChatList() {
val chatRoom = Intent(activity, MainActivity::class.java).apply { val chatRoom = Intent(activity, MainActivity::class.java).apply {
//TODO any parameter to pass //TODO any parameter to pass
} }
...@@ -26,9 +26,9 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity) { ...@@ -26,9 +26,9 @@ class AuthenticationNavigator(internal val activity: AuthenticationActivity) {
activity.finish() activity.finish()
} }
fun toSignUp() { fun toSignUp(server: String) {
activity.addFragmentBackStack("signupFragment", R.id.fragment_container) { activity.addFragmentBackStack("signupFragment", R.id.fragment_container) {
SignUpFragment.newInstance() SignupFragment.newInstance(server)
} }
} }
} }
package chat.rocket.android.authentication.presentation package chat.rocket.android.authentication.presentation
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.Token
import chat.rocket.common.util.PlatformLogger import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient import chat.rocket.core.RocketChatClient
import chat.rocket.core.TokenRepository
import chat.rocket.core.internal.rest.login import chat.rocket.core.internal.rest.login
import kotlinx.coroutines.experimental.Job import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.android.UI
...@@ -16,18 +15,18 @@ import javax.inject.Inject ...@@ -16,18 +15,18 @@ import javax.inject.Inject
class LoginPresenter @Inject constructor(private val view: LoginView, class LoginPresenter @Inject constructor(private val view: LoginView,
private val navigator: AuthenticationNavigator, private val navigator: AuthenticationNavigator,
private val okHttpClient: OkHttpClient, private val okHttpClient: OkHttpClient,
private val logger: PlatformLogger) { private val logger: PlatformLogger,
private val repository: AuthTokenRepository) {
var job: Job? = null var job: Job? = null
val client: RocketChatClient = RocketChatClient.create { val client: RocketChatClient = RocketChatClient.create {
httpClient = okHttpClient httpClient = okHttpClient
restUrl = HttpUrl.parse(navigator.currentServer)!! restUrl = HttpUrl.parse(navigator.currentServer)!!
websocketUrl = navigator.currentServer!! websocketUrl = navigator.currentServer!!
tokenRepository = SimpleTokenProvider() tokenRepository = repository
platformLogger = logger platformLogger = logger
} }
fun authenticate(username: String, password: String) { fun authenticate(username: String, password: String) {
// TODO - validate input // TODO - validate input
...@@ -37,7 +36,7 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -37,7 +36,7 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
val token = client.login(username, password) val token = client.login(username, password)
view.hideProgress() view.hideProgress()
navigator.toChatRoom() navigator.toChatList()
} catch (ex: RocketChatException) { } catch (ex: RocketChatException) {
view.hideProgress() view.hideProgress()
view.onLoginError(ex.message) view.onLoginError(ex.message)
...@@ -53,17 +52,6 @@ class LoginPresenter @Inject constructor(private val view: LoginView, ...@@ -53,17 +52,6 @@ class LoginPresenter @Inject constructor(private val view: LoginView,
} }
fun signup() { fun signup() {
navigator.toSignUp() navigator.toSignUp(navigator.currentServer!!)
}
}
class SimpleTokenProvider : TokenRepository {
var savedToken: Token? = null
override fun get(): Token? {
return savedToken
}
override fun save(token: Token) {
savedToken = token
} }
} }
\ No newline at end of file
package chat.rocket.android.authentication.presentation
import chat.rocket.android.authentication.infraestructure.AuthTokenRepository
import chat.rocket.common.RocketChatException
import chat.rocket.common.util.PlatformLogger
import chat.rocket.core.RocketChatClient
import chat.rocket.core.internal.rest.login
import chat.rocket.core.internal.rest.signup
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import timber.log.Timber
import javax.inject.Inject
class SignupPresenter @Inject constructor(private val view: SignupView,
private val navigator: AuthenticationNavigator,
private val okHttpClient: OkHttpClient,
private val logger: PlatformLogger,
private val repository: AuthTokenRepository) {
var job: Job? = null
val client: RocketChatClient = RocketChatClient.create {
httpClient = okHttpClient
restUrl = HttpUrl.parse(navigator.currentServer)!!
websocketUrl = navigator.currentServer!!
tokenRepository = repository
platformLogger = logger
}
fun signup(email: String, name: String, username: String, password: String) {
// TODO - validate input
job = launch(UI) {
view.showProgress()
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")
view.hideProgress()
navigator.toChatList()
} catch (ex: RocketChatException) {
view.hideProgress()
view.onSignupError(ex.message)
}
}
}
fun unbind() {
job?.let {
it.cancel()
}.also { null }
}
}
\ No newline at end of file
package chat.rocket.android.authentication.presentation
interface SignupView {
fun showProgress()
fun hideProgress()
fun onSignupError(message: String? = "Unknown error")
}
\ No newline at end of file
package chat.rocket.android.authentication.ui package chat.rocket.android.authentication.ui
import DrawableHelper import DrawableHelper
import android.app.ProgressDialog
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.view.* import android.view.*
import android.widget.Toast
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.app.KeyboardHelper import chat.rocket.android.app.KeyboardHelper
import chat.rocket.android.authentication.presentation.SignupPresenter
import chat.rocket.android.authentication.presentation.SignupView
import chat.rocket.android.util.content
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_authentication_sign_up.* import kotlinx.android.synthetic.main.fragment_authentication_sign_up.*
import javax.inject.Inject
class SignUpFragment : Fragment() { class SignupFragment : Fragment(), SignupView {
companion object { companion object {
fun newInstance() = SignUpFragment() private const val SERVER_URL = "server_url"
fun newInstance(url: String) = SignupFragment().apply {
arguments = Bundle(1).apply {
putString(SERVER_URL, url)
}
}
}
@Inject
lateinit var presenter: SignupPresenter
var progress: ProgressDialog? = null
lateinit var serverUrl: String
override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState)
// TODO - research a better way to initialize parameters on fragments.
serverUrl = arguments?.getString(SERVER_URL) ?: "https://open.rocket.chat"
}
override fun onDestroy() {
presenter.unbind()
super.onDestroy()
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_authentication_sign_up, container, false) override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_authentication_sign_up, container, false)
...@@ -27,6 +58,15 @@ class SignUpFragment : Fragment() { ...@@ -27,6 +58,15 @@ class SignUpFragment : Fragment() {
} }
setupGlobalLayoutListener() setupGlobalLayoutListener()
button_sign_up.setOnClickListener {
val email = text_email.content
val name = text_name.content
val username = text_username.content
val password = text_password.content
presenter.signup(email, name, username, password)
}
} }
override fun onDestroyView() { override fun onDestroyView() {
...@@ -59,4 +99,22 @@ class SignUpFragment : Fragment() { ...@@ -59,4 +99,22 @@ class SignUpFragment : Fragment() {
text_new_user_agreement.visibility = View.VISIBLE text_new_user_agreement.visibility = View.VISIBLE
} }
} }
override fun showProgress() {
// TODO - change for a proper progress indicator
progress = ProgressDialog.show(activity, "Authenticating", "Registering user")
}
override fun hideProgress() {
progress?.apply {
cancel()
}
progress = null
}
override fun onSignupError(message: String?) {
// TODO - show a proper error message
Toast.makeText(activity, message, Toast.LENGTH_LONG).show()
}
} }
\ No newline at end of file
package chat.rocket.android.dagger.module package chat.rocket.android.dagger.module
import chat.rocket.android.app.MainActivity import chat.rocket.android.app.MainActivity
import chat.rocket.android.authentication.di.LoginFragmentProvider
import chat.rocket.android.authentication.di.AuthenticationModule import chat.rocket.android.authentication.di.AuthenticationModule
import chat.rocket.android.authentication.di.LoginFragmentProvider
import chat.rocket.android.authentication.di.ServerFragmentProvider import chat.rocket.android.authentication.di.ServerFragmentProvider
import chat.rocket.android.authentication.di.SignupFragmentProvider
import chat.rocket.android.authentication.ui.AuthenticationActivity import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.dagger.scope.PerActivity import chat.rocket.android.dagger.scope.PerActivity
import dagger.Module import dagger.Module
...@@ -16,7 +17,8 @@ abstract class ActivityBuilder { ...@@ -16,7 +17,8 @@ abstract class ActivityBuilder {
@ContributesAndroidInjector(modules = arrayOf( @ContributesAndroidInjector(modules = arrayOf(
AuthenticationModule::class, AuthenticationModule::class,
LoginFragmentProvider::class, LoginFragmentProvider::class,
ServerFragmentProvider::class ServerFragmentProvider::class,
SignupFragmentProvider::class
)) ))
abstract fun bindAuthenticationActivity(): AuthenticationActivity abstract fun bindAuthenticationActivity(): AuthenticationActivity
......
package chat.rocket.android.util package chat.rocket.android.util
import android.widget.TextView
fun String.ifEmpty(value: String): String { fun String.ifEmpty(value: String): String {
if (isEmpty()) { if (isEmpty()) {
return value return value
} }
return this return this
} }
\ No newline at end of file
var TextView.content: String
get() = this.text.toString()
set(value) { this.text = value }
\ 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