Skip to content

Commit f7adc6e

Browse files
authored
State fixed while authenticating
State fixed while authenticating
2 parents 6850d9f + 30e13e4 commit f7adc6e

File tree

5 files changed

+84
-23
lines changed

5 files changed

+84
-23
lines changed

app/src/main/java/com/github/code/gambit/ui/fragment/auth/AuthFragment.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
5151
if (it) {
5252
Toast.makeText(requireContext(), "Permission granted", Toast.LENGTH_SHORT).show()
5353
} else {
54-
Toast.makeText(requireContext(), "Permission not granted", Toast.LENGTH_SHORT).show()
54+
Toast.makeText(requireContext(), "Permission not granted", Toast.LENGTH_SHORT)
55+
.show()
5556
}
5657
}
5758

@@ -72,15 +73,15 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
7273
confirmationComponent = ConfirmationComponent.bind(requireContext())
7374

7475
confirmationComponent.getOtp().observe(viewLifecycleOwner) {
75-
it?.let {
76-
confirmSignUp(it)
77-
}
76+
it?.let { confirmSignUp(it) }
7877
}
7978

8079
confirmationComponent.setResendCallback { email ->
8180
viewModel.setEvent(AuthEvent.ResendCode(email))
8281
}
8382

83+
confirmationComponent.setOnCancelCallback { enableInteraction() }
84+
8485
binding.buttonSubmit.setOnClickListener {
8586
disableInteraction()
8687
val fg = (binding.fragmentContainer.adapter as AuthFragmentAdapter).getFragment(
@@ -90,12 +91,16 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
9091
val data = (fg as SignUpFragment).validate()
9192
if (data != null) {
9293
signUp(data)
93-
} else { enableInteraction() }
94+
} else {
95+
enableInteraction()
96+
}
9497
} else {
9598
val data = (fg as LoginFragment).validate()
9699
if (data != null) {
97100
logIn(data)
98-
} else { enableInteraction() }
101+
} else {
102+
enableInteraction()
103+
}
99104
}
100105
}
101106

@@ -128,6 +133,7 @@ class AuthFragment : Fragment(R.layout.fragment_auth) {
128133
binding.root.snackbar(it.reason)
129134
}
130135
is AuthState.ResendStatus -> {
136+
confirmationComponent.onResendResult(it.success)
131137
if (it.success) {
132138
longToast("Code send on your email")
133139
}

app/src/main/java/com/github/code/gambit/ui/fragment/auth/confirmationcomponent/ConfirmationComponent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ interface ConfirmationComponent {
1515
fun getOtp(): LiveData<String>
1616
fun setResendCallback(callback: (email: String) -> Unit)
1717
fun showError(message: String = "")
18+
fun setOnCancelCallback(cancelFunction: () -> Unit = {})
19+
fun onResendResult(success: Boolean)
1820
}

app/src/main/java/com/github/code/gambit/ui/fragment/auth/confirmationcomponent/ConfirmationDialog.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.lifecycle.LiveData
1717
import androidx.lifecycle.MutableLiveData
1818
import com.github.code.gambit.R
1919
import com.github.code.gambit.databinding.EmailVerificationLayoutBinding
20+
import com.github.code.gambit.utility.extention.enableAfter
2021
import com.github.code.gambit.utility.extention.setStatusColor
2122
import com.github.code.gambit.utility.extention.snackbar
2223
import kotlin.math.hypot
@@ -30,8 +31,10 @@ class ConfirmationDialog(val context: Context) : ConfirmationComponent {
3031
)
3132
private val binding get() = _binding
3233

34+
private val defaultResendCounter = 50
3335
private val _otp = MutableLiveData<String>()
3436
private var resendCallBack: ((email: String) -> Unit)? = null
37+
private var cancelCallBack: (() -> Unit)? = null
3538

3639
private var userEmail: String = ""
3740

@@ -61,11 +64,14 @@ class ConfirmationDialog(val context: Context) : ConfirmationComponent {
6164
}
6265

6366
binding.cancel.setOnClickListener {
64-
revealShow(b = false, exit = true)
67+
revealShow(b = false, exit = true, exitFunction = cancelCallBack ?: {})
6568
}
6669

67-
binding.resend.setOnClickListener {
68-
resendCallBack?.let { it(userEmail) }
70+
binding.resend.setOnClickListener { v ->
71+
resendCallBack?.let {
72+
v.isEnabled = false
73+
it(userEmail)
74+
}
6975
}
7076

7177
dialog.setOnShowListener { revealShow(true) }
@@ -120,6 +126,7 @@ class ConfirmationDialog(val context: Context) : ConfirmationComponent {
120126

121127
@SuppressLint("SetTextI18n")
122128
override fun show(userEmail: String) {
129+
binding.resend.enableAfter(defaultResendCounter, binding.resendTimer)
123130
this.userEmail = userEmail
124131
binding.infoText.text = "${binding.infoText.text}\n$userEmail"
125132
dialog.show()
@@ -145,4 +152,16 @@ class ConfirmationDialog(val context: Context) : ConfirmationComponent {
145152
snackbar(message)
146153
}
147154
}
155+
156+
override fun setOnCancelCallback(cancelFunction: () -> Unit) {
157+
cancelCallBack = cancelFunction
158+
}
159+
160+
override fun onResendResult(success: Boolean) {
161+
if (success) {
162+
binding.resend.enableAfter(defaultResendCounter, binding.resendTimer)
163+
} else {
164+
binding.resend.enableAfter(0, binding.resendTimer)
165+
}
166+
}
148167
}

app/src/main/java/com/github/code/gambit/utility/extention/view.kt

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import android.app.AlertDialog
55
import android.content.ClipData
66
import android.content.ClipboardManager
77
import android.content.Context
8+
import android.os.Handler
89
import android.view.View
910
import android.view.Window
1011
import android.view.WindowManager
1112
import android.view.animation.AnimationUtils
1213
import android.view.animation.DecelerateInterpolator
1314
import android.view.inputmethod.InputMethodManager
1415
import android.widget.RelativeLayout
16+
import android.widget.TextView
1517
import android.widget.Toast
16-
import androidx.core.content.ContextCompat.getSystemService
1718
import androidx.core.view.isVisible
1819
import androidx.fragment.app.Fragment
1920
import com.github.code.gambit.R
@@ -58,6 +59,27 @@ fun View?.show() {
5859
}
5960
}
6061

62+
fun View?.enableAfter(timeInSeconds: Int, counterView: TextView) {
63+
if (this == null) {
64+
return
65+
}
66+
var count = timeInSeconds
67+
this.isEnabled = false
68+
counterView.visibility = View.VISIBLE
69+
Handler().post(object : Runnable {
70+
override fun run() {
71+
counterView.text = count.toString()
72+
if (count <= 0) {
73+
this@enableAfter.isEnabled = true
74+
counterView.visibility = View.INVISIBLE
75+
return
76+
}
77+
count--
78+
handler.postDelayed(this, 1000)
79+
}
80+
})
81+
}
82+
6183
fun View.toggleVisibility() {
6284
if (isVisible) {
6385
visibility = View.GONE
@@ -66,14 +88,6 @@ fun View.toggleVisibility() {
6688
visibility = View.VISIBLE
6789
}
6890

69-
fun List<View>.hideAll() {
70-
this.forEach { it.hide() }
71-
}
72-
73-
fun List<View>.showAll() {
74-
this.forEach { it.show() }
75-
}
76-
7791
fun View.snackbar(message: String, view: View) {
7892
Snackbar.make(this, message, Snackbar.LENGTH_INDEFINITE)
7993
.setAnchorView(view)
@@ -152,8 +166,13 @@ fun Context.showDefaultAlert(title: String, message: String, positiveButtonPress
152166
.setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() }.create().show()
153167
}
154168

155-
fun Context.showDefaultMaterialAlert(title: String, message: String, positiveButtonPress: () -> Unit) {
169+
fun Context.showDefaultMaterialAlert(
170+
title: String,
171+
message: String,
172+
positiveButtonPress: () -> Unit
173+
) {
156174
MaterialAlertDialogBuilder(this)
157-
.setTitle(title).setMessage(message).setPositiveButton("Yes") { _, _ -> positiveButtonPress() }
175+
.setTitle(title).setMessage(message)
176+
.setPositiveButton("Yes") { _, _ -> positiveButtonPress() }
158177
.setNegativeButton("Cancel") { dialog, _ -> dialog.dismiss() }.create().show()
159178
}

app/src/main/res/layout/email_verification_layout.xml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
55
xmlns:app="http://schemas.android.com/apk/res-auto"
6+
xmlns:tools="http://schemas.android.com/tools"
67
android:background="@color/secondary"
78
android:id="@+id/root">
89

@@ -79,6 +80,18 @@
7980
android:layout_centerHorizontal="true"
8081
android:layout_marginBottom="32dp"/>
8182

83+
<TextView
84+
android:id="@+id/resend_timer"
85+
android:layout_width="wrap_content"
86+
android:layout_height="wrap_content"
87+
tools:text="47"
88+
android:textSize="22sp"
89+
android:fontFamily="sans-serif-light"
90+
android:textColor="@color/raw_black"
91+
android:layout_below="@id/otp_view"
92+
android:layout_centerHorizontal="true"
93+
android:layout_marginTop="@dimen/root_side_margin"/>
94+
8295

8396
<Button
8497
android:id="@+id/resend"
@@ -87,17 +100,19 @@
87100
android:layout_height="36dp"
88101
android:text="@string/resend"
89102
android:textColor="@color/raw_black"
90-
android:layout_below="@id/otp_view"
103+
android:layout_below="@id/resend_timer"
91104
android:layout_centerHorizontal="true"
92-
android:layout_marginTop="@dimen/root_side_margin"/>
105+
android:layout_marginTop="2dp"
106+
android:enabled="false"/>
93107

94108

95109
<com.google.android.material.progressindicator.CircularProgressIndicator
96110
android:id="@+id/progress_bar"
97111
android:layout_width="wrap_content"
98112
android:layout_height="wrap_content"
99113
android:indeterminate="true"
100-
android:layout_centerInParent="true"
114+
android:layout_below="@id/resend"
115+
android:layout_centerHorizontal="true"
101116
android:visibility="gone"
102117
app:indicatorColor="@color/login_signup_button_color"/>
103118

0 commit comments

Comments
 (0)