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