1
1
package com.github.code.gambit.ui.fragment.auth
2
2
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
9
3
import android.os.Bundle
10
- import android.view.KeyEvent
11
4
import android.view.View
12
- import android.view.ViewAnimationUtils
13
- import android.widget.TextView
14
5
import android.widget.Toast
15
- import androidx.core.content.ContextCompat
16
6
import androidx.fragment.app.Fragment
17
7
import androidx.fragment.app.viewModels
18
8
import androidx.navigation.fragment.findNavController
19
9
import com.github.code.gambit.R
20
10
import com.github.code.gambit.data.model.User
21
- import com.github.code.gambit.databinding.EmailVerificationLayoutBinding
22
11
import com.github.code.gambit.databinding.FragmentAuthBinding
23
12
import com.github.code.gambit.helper.auth.AuthData
24
13
import com.github.code.gambit.helper.auth.AuthState
14
+ import com.github.code.gambit.ui.fragment.auth.confirmationcomponent.ConfirmationComponent
25
15
import com.github.code.gambit.utility.SystemManager
26
16
import 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
29
18
import com.github.code.gambit.utility.extention.snackbar
30
19
import com.google.android.material.tabs.TabLayoutMediator
31
20
import dagger.hilt.android.AndroidEntryPoint
32
21
import javax.inject.Inject
33
22
import javax.inject.Named
34
- import kotlin.math.hypot
35
23
36
24
@AndroidEntryPoint
37
25
class AuthFragment : Fragment (R .layout.fragment_auth) {
@@ -45,9 +33,7 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
45
33
46
34
private lateinit var authData: AuthData
47
35
48
- private lateinit var mOtpTextView: OtpTextView
49
- private lateinit var dialogView: View
50
- private lateinit var dialog: Dialog
36
+ private lateinit var confirmationComponent: ConfirmationComponent
51
37
52
38
@Inject
53
39
lateinit var permissionManager: SystemManager
@@ -83,6 +69,18 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
83
69
)
84
70
}.attach()
85
71
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
+
86
84
binding.buttonSubmit.setOnClickListener {
87
85
disableInteraction()
88
86
val fg = (binding.fragmentContainer.adapter as AuthFragmentAdapter ).getFragment(
@@ -110,113 +108,38 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
110
108
}
111
109
is AuthState .Confirmation -> {
112
110
binding.progressBar.hide()
113
- showConfirmationDialog( )
111
+ confirmationComponent.show(authData.email )
114
112
}
115
113
is AuthState .Success <User > -> {
116
114
binding.progressBar.hide()
117
115
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() }
120
118
} else {
121
119
navigateToHome()
122
120
}
123
121
}
124
122
is AuthState .Error -> {
125
123
enableInteraction()
126
124
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( )
129
127
}
130
128
binding.root.snackbar(it.reason)
131
129
}
130
+ is AuthState .ResendStatus -> {
131
+ if (it.success) {
132
+ longToast(" Code send on your email" )
133
+ }
134
+ }
132
135
AuthState .CodeMissMatch -> {
133
- mOtpTextView .showError()
136
+ confirmationComponent .showError(" Invalid code " )
134
137
}
135
138
}
136
139
}
137
140
)
138
141
}
139
142
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
-
220
143
private fun signUp (authData : AuthData ) {
221
144
this .authData = authData
222
145
viewModel.setEvent(AuthEvent .SignUpEvent (authData))
0 commit comments