asfoury/projmag

View on GitHub
app/src/main/java/com/sdp13epfl2021/projmag/activities/SignInActivity.kt

Summary

Maintainability
A
0 mins
Test Coverage
F
35%
package com.sdp13epfl2021.projmag.activities

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import com.sdp13epfl2021.projmag.R
import com.sdp13epfl2021.projmag.video.VideoUtils
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject


/** Sign result key */
private const val RC_SIGN_IN = 120

/**
 * Activity which signs in a user who has not yet signed in.
 */
@AndroidEntryPoint
class SignInActivity : AppCompatActivity() {

    @Inject
    lateinit var mAuth: FirebaseAuth

    lateinit var googleSignInClient: GoogleSignInClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_in)
        val button: Button = findViewById(R.id.signInButton)

        googleSignInClient = GoogleSignIn.getClient(
            this,
            GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build()
        )

        /*
         * Tell the user that app uses caption
         * Propose to change settings
         */
        VideoUtils.showInstructionDialog(this)

        button.setOnClickListener {
            signIn()
        }
    }

    private fun signIn() {
        val signInIntent = googleSignInClient.signInIntent
        startActivityForResult(signInIntent, RC_SIGN_IN)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                // Google Sign In was successful, authenticate with Firebase
                val account = task.getResult(ApiException::class.java)!!
                Log.d("SignInActivity", "firebaseAuthWithGoogle:" + account.id)
                firebaseAuthWithGoogle(account.idToken!!)
            } catch (e: ApiException) {
                Toast.makeText(applicationContext, "Google sign in failed", Toast.LENGTH_SHORT)
                    .show()
                // Google Sign In failed, update UI appropriately
                Log.w("SignInActivity", "Google sign in failed", e)
            }
        }
    }

    private fun firebaseAuthWithGoogle(idToken: String) {
        val credential = GoogleAuthProvider.getCredential(idToken, null)
        mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success, update UI with the signed-in user's information
                    val isNew: Boolean? = task.result?.additionalUserInfo?.isNewUser
                    Log.d("SignInActivity", "signInWithCredential:success")
                    if (isNew == true) {
                        val intent = Intent(this, UserTypeChoice::class.java)
                        startActivity(intent)
                        finish()
                    } else if (isNew == false) {

                        val intent = Intent(this, ProjectsListActivity::class.java)
                        startActivity(intent)
                        finish()
                    } else {
                        Log.w("SignInActivity", "signInWithCredential:failure", task.exception)
                    }
                } else {
                    // If sign in fails, display a message to the user.
                    Log.w("SignInActivity", "signInWithCredential:failure", task.exception)
                }
            }
    }
}