Commit ada461e5 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add rxKotlin dependency and update LoginPresenter and LoginFragment to

"kotlinized" it better
parent 69bb6a50
...@@ -24,6 +24,7 @@ dependencies { ...@@ -24,6 +24,7 @@ dependencies {
api project(':log-wrapper') api project(':log-wrapper')
implementation extraDependencies.okHTTP implementation extraDependencies.okHTTP
implementation extraDependencies.rxJava implementation extraDependencies.rxJava
implementation extraDependencies.rxKotlin
implementation extraDependencies.boltTask implementation extraDependencies.boltTask
implementation supportDependencies.annotation implementation supportDependencies.annotation
} }
\ No newline at end of file
...@@ -113,6 +113,7 @@ dependencies { ...@@ -113,6 +113,7 @@ dependencies {
api project(':persistence-realm') api project(':persistence-realm')
implementation extraDependencies.okHTTP implementation extraDependencies.okHTTP
implementation extraDependencies.rxJava implementation extraDependencies.rxJava
implementation extraDependencies.rxKotlin
implementation extraDependencies.boltTask implementation extraDependencies.boltTask
implementation supportDependencies.multidex implementation supportDependencies.multidex
implementation supportDependencies.designSupportLibrary implementation supportDependencies.designSupportLibrary
......
...@@ -7,9 +7,6 @@ import android.support.v4.app.Fragment ...@@ -7,9 +7,6 @@ import android.support.v4.app.Fragment
import android.view.View import android.view.View
import android.widget.Button import android.widget.Button
import android.widget.TextView import android.widget.TextView
import java.util.HashMap
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.api.MethodCallHelper import chat.rocket.android.api.MethodCallHelper
import chat.rocket.android.layouthelper.oauth.OAuthProviderInfo import chat.rocket.android.layouthelper.oauth.OAuthProviderInfo
...@@ -17,17 +14,18 @@ import chat.rocket.android.log.RCLog ...@@ -17,17 +14,18 @@ import chat.rocket.android.log.RCLog
import chat.rocket.core.models.LoginServiceConfiguration import chat.rocket.core.models.LoginServiceConfiguration
import chat.rocket.persistence.realm.repositories.RealmLoginServiceConfigurationRepository import chat.rocket.persistence.realm.repositories.RealmLoginServiceConfigurationRepository
import chat.rocket.persistence.realm.repositories.RealmPublicSettingRepository import chat.rocket.persistence.realm.repositories.RealmPublicSettingRepository
import java.util.*
/** /**
* Login screen. * Login screen.
*/ */
class LoginFragment : AbstractServerConfigFragment(), LoginContract.View { class LoginFragment : AbstractServerConfigFragment(), LoginContract.View {
private var presenter: LoginContract.Presenter? = null private lateinit var presenter: LoginContract.Presenter
private var container: ConstraintLayout? = null private lateinit var container: ConstraintLayout
private var waitingView: View? = null private lateinit var waitingView: View
private var txtUsername: TextView? = null private lateinit var txtUsername: TextView
private var txtPasswd: TextView? = null private lateinit var txtPasswd: TextView
override fun getLayout(): Int { override fun getLayout(): Int {
return R.layout.fragment_login return R.layout.fragment_login
...@@ -52,22 +50,22 @@ class LoginFragment : AbstractServerConfigFragment(), LoginContract.View { ...@@ -52,22 +50,22 @@ class LoginFragment : AbstractServerConfigFragment(), LoginContract.View {
txtPasswd = rootView.findViewById(R.id.editor_passwd) txtPasswd = rootView.findViewById(R.id.editor_passwd)
waitingView = rootView.findViewById(R.id.waiting) waitingView = rootView.findViewById(R.id.waiting)
btnEmail.setOnClickListener { view -> presenter!!.login(txtUsername!!.text.toString(), txtPasswd!!.text.toString()) } btnEmail.setOnClickListener { view -> presenter.login(txtUsername.text.toString(), txtPasswd.text.toString()) }
btnUserRegistration.setOnClickListener { view -> btnUserRegistration.setOnClickListener { view ->
UserRegistrationDialogFragment.create(hostname, txtUsername!!.text.toString(), txtPasswd!!.text.toString()) UserRegistrationDialogFragment.create(hostname, txtUsername.text.toString(), txtPasswd.text.toString())
.show(fragmentManager!!, "UserRegistrationDialogFragment") .show(fragmentManager!!, "UserRegistrationDialogFragment")
} }
} }
override fun showLoader() { override fun showLoader() {
container!!.visibility = View.GONE container.visibility = View.GONE
waitingView!!.visibility = View.VISIBLE waitingView.visibility = View.VISIBLE
} }
override fun hideLoader() { override fun hideLoader() {
waitingView!!.visibility = View.GONE waitingView.visibility = View.GONE
container!!.visibility = View.VISIBLE container.visibility = View.VISIBLE
} }
override fun showError(message: String) { override fun showError(message: String) {
...@@ -84,9 +82,9 @@ class LoginFragment : AbstractServerConfigFragment(), LoginContract.View { ...@@ -84,9 +82,9 @@ class LoginFragment : AbstractServerConfigFragment(), LoginContract.View {
for (authProvider in loginServiceList) { for (authProvider in loginServiceList) {
for (info in OAuthProviderInfo.LIST) { for (info in OAuthProviderInfo.LIST) {
if (!supportedMap[info.serviceName] && info.serviceName == authProvider.service) { if (supportedMap[info.serviceName] == false && info.serviceName == authProvider.service) {
supportedMap.put(info.serviceName, true) supportedMap.put(info.serviceName, true)
viewMap[info.serviceName].setOnClickListener { view -> viewMap[info.serviceName]?.setOnClickListener { view ->
var fragment: Fragment? = null var fragment: Fragment? = null
try { try {
fragment = info.fragmentClass.newInstance() fragment = info.fragmentClass.newInstance()
...@@ -94,38 +92,38 @@ class LoginFragment : AbstractServerConfigFragment(), LoginContract.View { ...@@ -94,38 +92,38 @@ class LoginFragment : AbstractServerConfigFragment(), LoginContract.View {
RCLog.w(exception, "failed to build new Fragment") RCLog.w(exception, "failed to build new Fragment")
} }
if (fragment != null) { fragment?.let {
val args = Bundle() val args = Bundle()
args.putString("hostname", hostname) args.putString("hostname", hostname)
fragment.arguments = args fragment.arguments = args
showFragmentWithBackStack(fragment) showFragmentWithBackStack(fragment)
} }
} }
viewMap[info.serviceName].setVisibility(View.VISIBLE) viewMap[info.serviceName]?.setVisibility(View.VISIBLE)
} }
} }
} }
for (info in OAuthProviderInfo.LIST) { for (info in OAuthProviderInfo.LIST) {
if (!supportedMap[info.serviceName]) { if (supportedMap[info.serviceName] == false) {
viewMap[info.serviceName].setVisibility(View.GONE) viewMap[info.serviceName]?.setVisibility(View.GONE)
} }
} }
} }
override fun showTwoStepAuth() { override fun showTwoStepAuth() {
showFragmentWithBackStack(TwoStepAuthFragment.create( showFragmentWithBackStack(TwoStepAuthFragment.create(
hostname, txtUsername!!.text.toString(), txtPasswd!!.text.toString() hostname, txtUsername.text.toString(), txtPasswd.text.toString()
)) ))
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
presenter!!.bindView(this) presenter.bindView(this)
} }
override fun onPause() { override fun onPause() {
presenter!!.release() presenter.release()
super.onPause() super.onPause()
} }
} }
package chat.rocket.android.fragment.server_config package chat.rocket.android.fragment.server_config
import com.hadisatrio.optional.Optional import bolts.Continuation
import bolts.Task import bolts.Task
import chat.rocket.android.BackgroundLooper import chat.rocket.android.BackgroundLooper
import chat.rocket.android.api.MethodCallHelper import chat.rocket.android.api.MethodCallHelper
...@@ -13,7 +12,9 @@ import chat.rocket.core.PublicSettingsConstants ...@@ -13,7 +12,9 @@ import chat.rocket.core.PublicSettingsConstants
import chat.rocket.core.models.PublicSetting import chat.rocket.core.models.PublicSetting
import chat.rocket.core.repositories.LoginServiceConfigurationRepository import chat.rocket.core.repositories.LoginServiceConfigurationRepository
import chat.rocket.core.repositories.PublicSettingRepository import chat.rocket.core.repositories.PublicSettingRepository
import com.hadisatrio.optional.Optional
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.subscribeBy
class LoginPresenter(private val loginServiceConfigurationRepository: LoginServiceConfigurationRepository, class LoginPresenter(private val loginServiceConfigurationRepository: LoginServiceConfigurationRepository,
private val publicSettingRepository: PublicSettingRepository, private val publicSettingRepository: PublicSettingRepository,
...@@ -36,9 +37,9 @@ class LoginPresenter(private val loginServiceConfigurationRepository: LoginServi ...@@ -36,9 +37,9 @@ class LoginPresenter(private val loginServiceConfigurationRepository: LoginServi
publicSettingRepository.getById(PublicSettingsConstants.LDAP.ENABLE) publicSettingRepository.getById(PublicSettingsConstants.LDAP.ENABLE)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribeBy(
{ publicSettingOptional -> doLogin(username, password, publicSettingOptional) }, onSuccess = { publicSettingOptional -> doLogin(username, password, publicSettingOptional) },
Consumer<Throwable> { Logger.report(it) } onError = { Logger.report(it) }
) )
) )
} }
...@@ -48,28 +49,32 @@ class LoginPresenter(private val loginServiceConfigurationRepository: LoginServi ...@@ -48,28 +49,32 @@ class LoginPresenter(private val loginServiceConfigurationRepository: LoginServi
loginServiceConfigurationRepository.all loginServiceConfigurationRepository.all
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribeBy(
{ loginServiceConfigurations -> view.showLoginServices(loginServiceConfigurations) }, onNext = { loginServiceConfigurations -> view.showLoginServices(loginServiceConfigurations) },
Consumer<Throwable> { Logger.report(it) } onError = { Logger.report(it) }
) )
) )
} }
private fun doLogin(username: String, password: String, optional: Optional<PublicSetting>) { private fun doLogin(username: String, password: String, optional: Optional<PublicSetting>) {
call(username, password, optional) call(username, password, optional)
.continueWith<Any>({ task -> .continueWith(object : Continuation<Void, Any?> {
if (task.isFaulted()) { override fun then(task: Task<Void>?): Any? {
view.hideLoader() if (task != null && task.isFaulted()) {
view.hideLoader()
val error = task.getError() val error = task.getError()
if (error is TwoStepAuthException) { error?.let {
view.showTwoStepAuth() if (error is TwoStepAuthException) {
} else { view.showTwoStepAuth()
view.showError(error.message) } else {
view.showError(error.message)
}
}
} }
return null
} }
null
}, Task.UI_THREAD_EXECUTOR) }, Task.UI_THREAD_EXECUTOR)
} }
......
...@@ -6,6 +6,7 @@ ext { ...@@ -6,6 +6,7 @@ ext {
kotlinVersion = "1.2.0" kotlinVersion = "1.2.0"
okHttpVersion = "3.9.0" okHttpVersion = "3.9.0"
rxbindingVersion = '2.0.0' rxbindingVersion = '2.0.0'
rxJavaVersion = "2.1.0"
supportDependencies = [ supportDependencies = [
designSupportLibrary: "com.android.support:design:${supportLibraryVersion}", designSupportLibrary: "com.android.support:design:${supportLibraryVersion}",
...@@ -19,7 +20,8 @@ ext { ...@@ -19,7 +20,8 @@ ext {
extraDependencies = [ extraDependencies = [
okHTTP : "com.squareup.okhttp3:okhttp:${okHttpVersion}", okHTTP : "com.squareup.okhttp3:okhttp:${okHttpVersion}",
rxJava : "io.reactivex.rxjava2:rxjava:2.1.0", rxJava : "io.reactivex.rxjava2:rxjava:${rxJavaVersion}",
rxKotlin : "io.reactivex.rxjava2:rxkotlin:${rxJavaVersion}",
boltTask : "com.parse.bolts:bolts-tasks:1.4.0", boltTask : "com.parse.bolts:bolts-tasks:1.4.0",
rxAndroid : "io.reactivex.rxjava2:rxandroid:2.0.1", rxAndroid : "io.reactivex.rxjava2:rxandroid:2.0.1",
textDrawable : "com.github.rocketchat:textdrawable:1.0.2", textDrawable : "com.github.rocketchat:textdrawable:1.0.2",
......
...@@ -33,6 +33,7 @@ dependencies { ...@@ -33,6 +33,7 @@ dependencies {
api project(':log-wrapper') api project(':log-wrapper')
api project(':rocket-chat-core') api project(':rocket-chat-core')
implementation extraDependencies.rxJava implementation extraDependencies.rxJava
implementation extraDependencies.rxKotlin
implementation extraDependencies.boltTask implementation extraDependencies.boltTask
implementation supportDependencies.annotation implementation supportDependencies.annotation
implementation supportDependencies.designSupportLibrary implementation supportDependencies.designSupportLibrary
......
...@@ -4,6 +4,7 @@ apply plugin: 'java' ...@@ -4,6 +4,7 @@ apply plugin: 'java'
dependencies { dependencies {
compile extraDependencies.rxJava compile extraDependencies.rxJava
compile extraDependencies.rxKotlin
compile extraDependencies.optional compile extraDependencies.optional
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$rootProject.ext.kotlinVersion" compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$rootProject.ext.kotlinVersion"
compile 'com.google.code.findbugs:jsr305:3.0.2' compile 'com.google.code.findbugs:jsr305:3.0.2'
......
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