Login : quickly add homeserver url field

This commit is contained in:
ganfra 2019-01-17 18:20:01 +01:00
parent 4a6b28aca1
commit 2380499af2
4 changed files with 98 additions and 24 deletions

View File

@ -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"

View File

@ -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()
abstract class RiotActivity : BaseMvRxActivity() {
private val uiDisposables = CompositeDisposable()
protected fun Disposable.disposeOnDestroy(): Disposable {
uiDisposables.add(this)
return this
}
}

View File

@ -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<Session> {
override fun onSuccess(data: Session) {
Matrix.getInstance().currentSession = data
@ -48,6 +59,31 @@ class LoginActivity : RiotActivity() {
})
}
private fun buildHomeServerConnectionConfig(): Try<HomeServerConnectionConfig> {
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<Boolean, Boolean, Boolean, Boolean> { isLoginNotEmpty, isPasswordNotEmpty, isHomeServerNotEmpty ->
isLoginNotEmpty && isPasswordNotEmpty && isHomeServerNotEmpty
}
)
.subscribeBy { authenticateButton.isEnabled = it }
.disposeOnDestroy()
authenticateButton.setOnClickListener { authenticate() }
}
private fun goToHome() {
val intent = HomeActivity.newIntent(this)

View File

@ -23,25 +23,50 @@
android:layout_height="100dp"
android:src="@drawable/logo_login" />
<EditText
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Username">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/loginField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:hint="Username"
android:inputType="textEmailAddress"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<EditText
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Password">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/passwordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Homeserver url">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/homeServerField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</ScrollView>