Skip to content

Instantly share code, notes, and snippets.

@tsherdiwala
Created April 6, 2023 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsherdiwala/abb2e507af3fbbef04322320d75b435f to your computer and use it in GitHub Desktop.
Save tsherdiwala/abb2e507af3fbbef04322320d75b435f to your computer and use it in GitHub Desktop.
package com.cheq.retail.ui.cheqsafe.subfragments
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.cheq.retail.databinding.BottomSheetDialogEnableCheqSafeBinding
import com.cheq.retail.ui.cheqsafe.CheqSafeViewModel
import com.google.android.gms.auth.GoogleAuthUtil
import com.google.android.gms.auth.api.Auth
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.Scopes
import com.google.android.gms.common.api.Scope
import com.google.android.gms.tasks.Task
import com.google.android.gms.wallet.callback.OnCompleteListener
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class IntroductionFragment : BottomSheetDialogFragment() {
private val viewModel by lazy {
ViewModelProvider(parentFragment ?: this)[CheqSafeViewModel::class.java]
}
private lateinit var binding: BottomSheetDialogEnableCheqSafeBinding
private var googleSignInClient: GoogleSignInClient? = null
private val signInWithGoogleLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val intent = result.data
if (intent == null) {
viewModel.notifyEmailLinkingFailed()
return@registerForActivityResult
}
handleGoogleLogin(intent)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = BottomSheetDialogEnableCheqSafeBinding.inflate(
layoutInflater,
container,
false
)
return binding.root
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
(dialog as? BottomSheetDialog)?.behavior?.state = BottomSheetBehavior.STATE_EXPANDED
return dialog
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initGoogleLogin()
setupActions()
setUpObserver()
}
private fun initGoogleLogin() {
val gmailScope = Scope("https://www.googleapis.com/auth/gmail.readonly")
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
// .requestIdToken("143139010454-p91g4i1lgru0tvri08mts7927ojvs21f.apps.googleusercontent.com")
.requestIdToken("143139010454-2bm1njnpn4j5gg6235tpsf65g5j2j8r3.apps.googleusercontent.com")
.requestEmail()
// .requestServerAuthCode("143139010454-p91g4i1lgru0tvri08mts7927ojvs21f.apps.googleusercontent.com")
.requestServerAuthCode("143139010454-2bm1njnpn4j5gg6235tpsf65g5j2j8r3.apps.googleusercontent.com")
.requestScopes(gmailScope)
.build()
googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
}
private fun setupActions() {
binding.btnLinkEmail.setOnClickListener {
googleSignOut()
doGoogleLogin()
}
binding.ivCancel.setOnClickListener {
dialog?.dismiss()
}
/*binding.btnLinkEmail.setOnClickListener {
viewModel.linkEmailAddress(
firstName = "tejas",
lastName = "s",
email = "hello@tejas.com",
token = ""
)
}*/
}
private fun googleSignOut() {
googleSignInClient?.signOut()
?.addOnCompleteListener(object : OnCompleteListener<Void>,
com.google.android.gms.tasks.OnCompleteListener<Void> {
override fun complete(p0: Void) {
}
override fun onComplete(p0: Task<Void>) {
}
})
}
private fun doGoogleLogin() {
signInWithGoogleLauncher.launch(googleSignInClient!!.signInIntent)
}
private fun handleGoogleLogin(intent: Intent) {
val signInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(intent)
if (signInResult == null) {
viewModel.notifyEmailLinkingFailed()
return
}
if (!signInResult.status.isSuccess) {
viewModel.notifyEmailLinkingFailed()
return
}
GoogleSignIn.getSignedInAccountFromIntent(intent)
.addOnSuccessListener {
lifecycleScope.launch(Dispatchers.IO) {
val scope = "oauth2:" + Scopes.EMAIL + " " + Scopes.PROFILE
val accessToken = GoogleAuthUtil.getToken(
requireContext(),
it.account!!, scope, Bundle()
)
viewModel.linkEmailAddress(
it.givenName ?: "",
it.familyName ?: "",
it.email,
accessToken,
it.serverAuthCode
)
}
}
.addOnFailureListener {
viewModel.notifyEmailLinkingFailed()
}
.addOnCanceledListener {
viewModel.notifyEmailLinkingFailed()
}
}
private fun setUpObserver() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment