11package com.github.code.gambit.ui.fragment.auth
22
3- import `in`.aabhasjindal.otptextview.OtpTextView
4- import android.animation.Animator
5- import android.animation.AnimatorListenerAdapter
6- import android.app.Dialog
7- import android.content.DialogInterface
8- import android.graphics.drawable.ColorDrawable
93import android.os.Bundle
10- import android.view.KeyEvent
114import android.view.View
12- import android.view.ViewAnimationUtils
13- import android.widget.TextView
145import android.widget.Toast
15- import androidx.core.content.ContextCompat
166import androidx.fragment.app.Fragment
177import androidx.fragment.app.viewModels
188import androidx.navigation.fragment.findNavController
199import com.github.code.gambit.R
2010import com.github.code.gambit.data.model.User
21- import com.github.code.gambit.databinding.EmailVerificationLayoutBinding
2211import com.github.code.gambit.databinding.FragmentAuthBinding
2312import com.github.code.gambit.helper.auth.AuthData
2413import com.github.code.gambit.helper.auth.AuthState
14+ import com.github.code.gambit.ui.fragment.auth.confirmationcomponent.ConfirmationComponent
2515import com.github.code.gambit.utility.SystemManager
2616import com.github.code.gambit.utility.extention.exitFullscreen
27- import com.github.code.gambit.utility.extention.setStatusColor
28- import com.github.code.gambit.utility.extention.show
17+ import com.github.code.gambit.utility.extention.longToast
2918import com.github.code.gambit.utility.extention.snackbar
3019import com.google.android.material.tabs.TabLayoutMediator
3120import dagger.hilt.android.AndroidEntryPoint
3221import javax.inject.Inject
3322import javax.inject.Named
34- import kotlin.math.hypot
3523
3624@AndroidEntryPoint
3725class AuthFragment : Fragment (R .layout.fragment_auth) {
@@ -45,9 +33,7 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
4533
4634 private lateinit var authData: AuthData
4735
48- private lateinit var mOtpTextView: OtpTextView
49- private lateinit var dialogView: View
50- private lateinit var dialog: Dialog
36+ private lateinit var confirmationComponent: ConfirmationComponent
5137
5238 @Inject
5339 lateinit var permissionManager: SystemManager
@@ -83,6 +69,18 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
8369 )
8470 }.attach()
8571
72+ confirmationComponent = ConfirmationComponent .bind(requireContext())
73+
74+ confirmationComponent.getOtp().observe(viewLifecycleOwner) {
75+ it?.let {
76+ confirmSignUp(it)
77+ }
78+ }
79+
80+ confirmationComponent.setResendCallback { email ->
81+ viewModel.setEvent(AuthEvent .ResendCode (email))
82+ }
83+
8684 binding.buttonSubmit.setOnClickListener {
8785 disableInteraction()
8886 val fg = (binding.fragmentContainer.adapter as AuthFragmentAdapter ).getFragment(
@@ -110,113 +108,38 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
110108 }
111109 is AuthState .Confirmation -> {
112110 binding.progressBar.hide()
113- showConfirmationDialog( )
111+ confirmationComponent.show(authData.email )
114112 }
115113 is AuthState .Success <User > -> {
116114 binding.progressBar.hide()
117115 binding.root.snackbar(" Welcome ${it.data.name} " )
118- if (this ::dialog .isInitialized && dialog .isShowing) {
119- revealShow( false , exit = true ) { navigateToHome() }
116+ if (this ::confirmationComponent .isInitialized && confirmationComponent .isShowing() ) {
117+ confirmationComponent. exit { navigateToHome() }
120118 } else {
121119 navigateToHome()
122120 }
123121 }
124122 is AuthState .Error -> {
125123 enableInteraction()
126124 binding.progressBar.hide()
127- if (this ::dialog .isInitialized && dialog .isShowing) {
128- revealShow( false , exit = true )
125+ if (this ::confirmationComponent .isInitialized && confirmationComponent .isShowing() ) {
126+ confirmationComponent. exit( )
129127 }
130128 binding.root.snackbar(it.reason)
131129 }
130+ is AuthState .ResendStatus -> {
131+ if (it.success) {
132+ longToast(" Code send on your email" )
133+ }
134+ }
132135 AuthState .CodeMissMatch -> {
133- mOtpTextView .showError()
136+ confirmationComponent .showError(" Invalid code " )
134137 }
135138 }
136139 }
137140 )
138141 }
139142
140- private fun showConfirmationDialog () {
141- dialog = Dialog (requireContext(), R .style.Theme_VTransfer )
142- EmailVerificationLayoutBinding .inflate(layoutInflater)
143- dialog.setContentView(R .layout.email_verification_layout)
144- dialog.window?.setStatusColor(
145- ContextCompat .getColor(
146- requireContext(),
147- R .color.secondary
148- )
149- )
150- dialog.window?.setBackgroundDrawable(ColorDrawable (android.graphics.Color .TRANSPARENT ))
151- val otpTextView: OtpTextView = dialog.findViewById(R .id.otp_view)
152- val progressBar: View = dialog.findViewById(R .id.progress_bar)
153- mOtpTextView = otpTextView
154- val validate: TextView = dialog.findViewById(R .id.validate)
155- dialogView = dialog.findViewById(R .id.root)
156-
157- validate.setOnClickListener {
158- val otp = otpTextView.otp
159- when {
160- otp == null -> {
161- dialogView.snackbar(" Code can't be empty" )
162- }
163- otp.length != 6 -> {
164- dialogView.snackbar(" Invalid code length" )
165- }
166- else -> {
167- progressBar.show()
168- validate.isClickable = false
169- confirmSignUp(otp)
170- }
171- }
172- }
173-
174- dialog.setOnShowListener { revealShow(true ) }
175-
176- dialog.setOnKeyListener(
177- DialogInterface .OnKeyListener { _, i, _ ->
178- if (i == KeyEvent .KEYCODE_BACK ) {
179- revealShow(false )
180- return @OnKeyListener true
181- }
182- false
183- }
184- )
185-
186- dialog.show()
187- }
188-
189- private fun revealShow (b : Boolean , exit : Boolean = false, exitFunction : () -> Unit = {}) {
190- val view = dialogView.findViewById<View >(R .id.root)
191- val w = view.width
192- val h = view.height
193- val endRadius = hypot(w.toDouble(), h.toDouble()).toInt()
194- val cx = (view.width / 2 )
195- val cy = 0
196- if (b) {
197- val revealAnimator =
198- ViewAnimationUtils .createCircularReveal(view, cx, cy, 0f , endRadius.toFloat())
199- view.visibility = View .VISIBLE
200- revealAnimator.duration = 700
201- revealAnimator.start()
202- } else {
203- val anim =
204- ViewAnimationUtils .createCircularReveal(view, cx, cy, endRadius.toFloat(), 0f )
205- anim.addListener(object : AnimatorListenerAdapter () {
206- override fun onAnimationEnd (animation : Animator ) {
207- super .onAnimationEnd(animation)
208- dialog.dismiss()
209- view.visibility = View .INVISIBLE
210- if (exit) {
211- exitFunction()
212- }
213- }
214- })
215- anim.duration = 700
216- anim.start()
217- }
218- }
219-
220143 private fun signUp (authData : AuthData ) {
221144 this .authData = authData
222145 viewModel.setEvent(AuthEvent .SignUpEvent (authData))
0 commit comments