From 2380499af22068ef1de858d1efd907ba47f9a108 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 17 Jan 2019 18:20:01 +0100 Subject: [PATCH] Login : quickly add homeserver url field --- app/build.gradle | 4 +- .../core/platform/RiotActivity.kt | 13 ++++- .../features/login/LoginActivity.kt | 54 +++++++++++++++---- app/src/main/res/layout/activity_login.xml | 51 +++++++++++++----- 4 files changed, 98 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3e3c16b863..e9f9a3e985 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -55,8 +55,9 @@ dependencies { // rx implementation 'io.reactivex.rxjava2:rxkotlin:2.3.0' - implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' + implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' implementation 'com.jakewharton.rxrelay2:rxrelay:2.1.0' + implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0' implementation("com.airbnb.android:epoxy:$epoxy_version") kapt "com.airbnb.android:epoxy-processor:$epoxy_version" @@ -69,6 +70,7 @@ dependencies { implementation 'com.github.bumptech.glide:glide:4.8.0' kapt 'com.github.bumptech.glide:compiler:4.8.0' implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' + implementation 'com.google.android.material:material:1.1.0-alpha02' // DI implementation "org.koin:koin-android:$koin_version" diff --git a/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt b/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt index 1308a0e12c..9ab99f8304 100644 --- a/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt +++ b/app/src/main/java/im/vector/riotredesign/core/platform/RiotActivity.kt @@ -1,5 +1,16 @@ package im.vector.riotredesign.core.platform import com.airbnb.mvrx.BaseMvRxActivity +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.disposables.Disposable -abstract class RiotActivity : BaseMvRxActivity() \ No newline at end of file +abstract class RiotActivity : BaseMvRxActivity() { + + private val uiDisposables = CompositeDisposable() + + protected fun Disposable.disposeOnDestroy(): Disposable { + uiDisposables.add(this) + return this + } + +} \ No newline at end of file diff --git a/app/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt b/app/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt index f72107dcaf..79f0e3da0f 100644 --- a/app/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt +++ b/app/src/main/java/im/vector/riotredesign/features/login/LoginActivity.kt @@ -5,6 +5,8 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Toast +import arrow.core.Try +import com.jakewharton.rxbinding2.widget.RxTextView import im.vector.matrix.android.api.Matrix import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig @@ -12,8 +14,15 @@ import im.vector.matrix.android.api.session.Session import im.vector.riotredesign.R import im.vector.riotredesign.core.platform.RiotActivity import im.vector.riotredesign.features.home.HomeActivity +import io.reactivex.Observable +import io.reactivex.functions.Function3 +import io.reactivex.rxkotlin.subscribeBy import kotlinx.android.synthetic.main.activity_login.* +private const val DEFAULT_HOME_SERVER_URI = "https://matrix.org" +private const val DEFAULT_IDENTITY_SERVER_URI = "https://vector.im" +private const val DEFAULT_ANTIVIRUS_SERVER_URI = "https://matrix.org" + class LoginActivity : RiotActivity() { private val authenticator = Matrix.getInstance().authenticator() @@ -21,19 +30,21 @@ class LoginActivity : RiotActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) - authenticateButton.setOnClickListener { authenticate() } + setupAuthButton() + homeServerField.setText(DEFAULT_HOME_SERVER_URI) } private fun authenticate() { - val login = loginField.text.trim().toString() - val password = passwordField.text.trim().toString() - progressBar.visibility = View.VISIBLE - val homeServerConnectionConfig = HomeServerConnectionConfig.Builder() - .withHomeServerUri("https://matrix.org/") - .withIdentityServerUri("https://vector.im") - .withAntiVirusServerUri("https://matrix.org/") - .build() + val login = loginField.text?.trim().toString() + val password = passwordField.text?.trim().toString() + buildHomeServerConnectionConfig().fold( + { Toast.makeText(this@LoginActivity, "Authenticate failure: $it", Toast.LENGTH_LONG).show() }, + { authenticateWith(it, login, password) } + ) + } + private fun authenticateWith(homeServerConnectionConfig: HomeServerConnectionConfig, login: String, password: String) { + progressBar.visibility = View.VISIBLE authenticator.authenticate(homeServerConnectionConfig, login, password, object : MatrixCallback { override fun onSuccess(data: Session) { Matrix.getInstance().currentSession = data @@ -48,6 +59,31 @@ class LoginActivity : RiotActivity() { }) } + private fun buildHomeServerConnectionConfig(): Try { + return Try { + val homeServerUri = homeServerField.text?.trim().toString() + HomeServerConnectionConfig.Builder() + .withHomeServerUri(homeServerUri) + .withIdentityServerUri(DEFAULT_IDENTITY_SERVER_URI) + .withAntiVirusServerUri(DEFAULT_ANTIVIRUS_SERVER_URI) + .build() + } + } + + private fun setupAuthButton() { + Observable + .combineLatest( + RxTextView.textChanges(loginField).map { it.trim().isNotEmpty() }, + RxTextView.textChanges(passwordField).map { it.trim().isNotEmpty() }, + RxTextView.textChanges(homeServerField).map { it.trim().isNotEmpty() }, + Function3 { isLoginNotEmpty, isPasswordNotEmpty, isHomeServerNotEmpty -> + isLoginNotEmpty && isPasswordNotEmpty && isHomeServerNotEmpty + } + ) + .subscribeBy { authenticateButton.isEnabled = it } + .disposeOnDestroy() + authenticateButton.setOnClickListener { authenticate() } + } private fun goToHome() { val intent = HomeActivity.newIntent(this) diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 376c0177ea..d533f6802d 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -23,25 +23,50 @@ android:layout_height="100dp" android:src="@drawable/logo_login" /> - + android:layout_marginTop="16dp" + android:hint="Username"> + + + - + android:layout_marginTop="16dp" + android:hint="Password"> + + + + + + + + +