Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement remember me option in login screen #2243

Merged
merged 12 commits into from
Jul 5, 2019
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,7 @@ dependencies {
//Shimmer
implementation 'com.facebook.shimmer:shimmer:0.4.0'

//Smart LOck authentication
implementation "com.google.android.gms:play-services-auth:${rootConfiguration.playServiceAuthVersion}"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${rootConfiguration.kotlinVersion}"
}
1 change: 1 addition & 0 deletions app/config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ ext {
customTabsVersion = '28.0.0'
jUnitVersion = '4.12'
mockitoCoreVersion = '1.10.19'
playServiceAuthVersion='16.0.1'
}
2 changes: 2 additions & 0 deletions app/src/main/java/org/fossasia/susi/ai/helper/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ object Constant {
const val SAVED_EMAIL = "saved_email"
const val SAVE_EMAIL = "save_email"

const val SUSI_ACCOUNT = "susi_account"

const val SAVE_DIALOG_STATE = "save_state"
const val SUSI_SERVER = "is_susi_server_selected"
const val CUSTOM_SERVER = "custom_server"
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/org/fossasia/susi/ai/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ArrayAdapter
import android.widget.Toast
import com.google.android.gms.auth.api.credentials.Credential
import kotlinx.android.synthetic.main.activity_login.*
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.ChatActivity
Expand Down Expand Up @@ -72,11 +73,26 @@ class LoginActivity : AppCompatActivity(), ILoginView {
val string = bundle?.getString("email")
if (string != null)
email.editText?.setText(string)

loginPresenter.clientRequest(this)
}

override fun onCredentialRetrieved(credential: Credential?) {

var accountName = credential?.name
if (accountName == Constant.SUSI_ACCOUNT) {
email.editText?.setText(credential?.id.toString())
password.editText?.setText(credential?.password.toString())
}
}

override fun onLoginSuccess(message: String?) {
hideSoftKeyboard(this, window.decorView)
Toast.makeText(this@LoginActivity, message, Toast.LENGTH_SHORT).show()
if (rememberCredential.isChecked) {
loginPresenter.saveCredential(email.editText?.text.toString(), password.editText?.text.toString())
}

val intent = Intent(this@LoginActivity, ChatActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.putExtra(Constant.FIRST_TIME, true)
Expand Down
43 changes: 40 additions & 3 deletions app/src/main/java/org/fossasia/susi/ai/login/LoginPresenter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package org.fossasia.susi.ai.login

import android.content.Context
import android.graphics.Color
import com.google.android.gms.auth.api.credentials.Credentials
import com.google.android.gms.auth.api.credentials.CredentialRequest
import com.google.android.gms.auth.api.credentials.CredentialsClient
import com.google.android.gms.auth.api.credentials.Credential
import com.google.android.gms.auth.api.credentials.CredentialRequestResponse
import com.google.android.gms.tasks.OnCompleteListener
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.data.ForgotPasswordModel
import org.fossasia.susi.ai.data.contract.ILoginModel
Expand Down Expand Up @@ -29,9 +36,9 @@ import java.net.UnknownHostException
* Created by chiragw15 on 4/7/17.
*/
class LoginPresenter(loginActivity: LoginActivity) :
ILoginPresenter,
ILoginModel.OnLoginFinishedListener,
IForgotPasswordModel.OnFinishListener {
ILoginPresenter,
ILoginModel.OnLoginFinishedListener,
IForgotPasswordModel.OnFinishListener {

private var loginModel: LoginModel = LoginModel()
private var utilModel: UtilModel = UtilModel(loginActivity)
Expand All @@ -40,6 +47,9 @@ class LoginPresenter(loginActivity: LoginActivity) :
var forgotPasswordModel: ForgotPasswordModel = ForgotPasswordModel()
lateinit var email: String
lateinit var message: String
private lateinit var credential: Credential
private lateinit var credentialsClient: CredentialsClient
private lateinit var credentialRequest: CredentialRequest

override fun onAttach(loginView: ILoginView) {
this.loginView = loginView
Expand Down Expand Up @@ -240,4 +250,31 @@ class LoginPresenter(loginActivity: LoginActivity) :
override fun cancelSignup() {
forgotPasswordModel.cancelSignup()
}

override fun clientRequest(context: Context) {
credentialsClient = Credentials.getClient(context)
credentialRequest = CredentialRequest.Builder()
.setPasswordLoginSupported(true)
.build()

credentialsClient.request(credentialRequest).addOnCompleteListener(
OnCompleteListener<CredentialRequestResponse> { task ->
if (task.isSuccessful) {
loginView?.onCredentialRetrieved(task.result?.credential)
return@OnCompleteListener
}
})
}

override fun saveCredential(email: String, password: String) {
credential = Credential.Builder(email)
.setPassword(password)
.setName(Constant.SUSI_ACCOUNT)
.build()
credentialsClient.save(credential).addOnCompleteListener({
if (it.isComplete) {
Timber.d("Saved Credentials")
}
})
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.fossasia.susi.ai.login.contract

import android.content.Context

/**
* The interface for Login Presenter
*
Expand All @@ -20,4 +22,8 @@ interface ILoginPresenter {
fun requestPassword(email: String, url: String, isPersonalServerChecked: Boolean)

fun cancelSignup()

fun saveCredential(email: String, password: String)

fun clientRequest(context: Context)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.fossasia.susi.ai.login.contract

import com.google.android.gms.auth.api.credentials.Credential

/**
* The interface for Login view
*
Expand All @@ -24,4 +26,6 @@ interface ILoginView {
fun resetPasswordSuccess()

fun resetPasswordFailure(title: String?, message: String?, button: String?, color: Int)

fun onCredentialRetrieved(credential: Credential?)
}
7 changes: 7 additions & 0 deletions app/src/main/res/layout-land/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
android:layout_marginBottom="5dp"
android:text="@string/custom_server" />

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/rememberCredential"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_bottom"
android:text="@string/remember_me" />

<android.support.design.widget.TextInputLayout
android:id="@+id/inputUrl"
android:layout_width="match_parent"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
android:layout_marginBottom="@dimen/margin_bottom"
android:text="@string/custom_server" />

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/rememberCredential"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_bottom"
android:text="@string/remember_me" />

<android.support.design.widget.TextInputLayout
android:id="@+id/inputUrl"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@
<string name="content_type_static">Content Type: Static</string>

<string name="custom_server">Custom Server</string>
<string name="remember_me">Remember Me</string>

<string name="description">Description</string>
<string name="description_long">Description long</string>
Expand Down