Skip to content

Commit

Permalink
Give Reason during Login Screen
Browse files Browse the repository at this point in the history
- Also fix Compatibility with Kitkat
- Give multiple Reasons
  • Loading branch information
sounddrill31 committed Aug 16, 2024
1 parent 4aa8549 commit 3f63c0a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
27 changes: 16 additions & 11 deletions app/src/main/java/com/zeusinstitute/upiapp/FirstFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -33,12 +34,14 @@ class FirstFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList
savedInstanceState: Bundle?
): View? {
_binding = FragmentFirstBinding.inflate(inflater, container, false)
qrCodeImageView = binding.qrCodeImageView // Initialize qrCodeImageView
qrCodeImageView = binding.qrCodeImageView

// Make status bar transparent
val window = requireActivity().window
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window.statusBarColor = Color.TRANSPARENT
// Make status bar transparent (conditionally for Lollipop and above)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val window = requireActivity().window
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window.statusBarColor = Color.TRANSPARENT
}

return binding.root
}
Expand Down Expand Up @@ -81,12 +84,14 @@ class FirstFragment : Fragment(), SharedPreferences.OnSharedPreferenceChangeList
override fun onDestroyView() {
super.onDestroyView()
sharedPref.unregisterOnSharedPreferenceChangeListener(this) // Unregister listener
val windowInsetsController =
ViewCompat.getWindowInsetsController(requireActivity().window.decorView)
windowInsetsController?.show(WindowInsetsCompat.Type.systemBars())
val window = requireActivity().window
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window.statusBarColor = Color.TRANSPARENT // or any other color you want
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val windowInsetsController =
ViewCompat.getWindowInsetsController(requireActivity().window.decorView)
windowInsetsController?.show(WindowInsetsCompat.Type.systemBars())
val window = requireActivity().window
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window.statusBarColor = Color.TRANSPARENT // or any other color you want
}
_binding = null
}
}
26 changes: 21 additions & 5 deletions app/src/main/java/com/zeusinstitute/upiapp/Login.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout

class Login : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -30,6 +30,7 @@ class Login : Fragment() {
): View? {
val view = inflater.inflate(R.layout.fragment_login, container, false)

val apikeyLayout = view.findViewById<TextInputLayout>(R.id.apikeyLayout)
val apikey = view.findViewById<TextInputEditText>(R.id.apikey)
val submitButton = view.findViewById<Button>(R.id.submitButton)

Expand All @@ -42,7 +43,7 @@ class Login : Fragment() {
// Set up the listener for the "Enter" key on the keyboard
apikey.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
submitButton.performClick() // Trigger the submit button's click listener
submitButton.performClick()
true
} else {
false
Expand All @@ -52,7 +53,8 @@ class Login : Fragment() {
submitButton.setOnClickListener {
val data = apikey.text.toString()

if (isValidUpiId(data)) {
val invalidReasons = getInvalidUpiIdReasons(data) // Get multiple reasons
if (invalidReasons.isEmpty()) {
// Save data to Shared Preferences
val sharedPref = requireActivity().getPreferences(Context.MODE_PRIVATE) ?: return@setOnClickListener
with (sharedPref.edit()) {
Expand All @@ -63,8 +65,8 @@ class Login : Fragment() {
// Navigate back to FirstFragment
findNavController().navigate(R.id.action_login_to_firstFragment)
} else {
// Show error message if UPI ID is invalid
Toast.makeText(context, "Invalid UPI ID. Please check and try again.", Toast.LENGTH_SHORT).show()
// Show error message directly in the TextInputLayout with all reasons
apikeyLayout.error = "Invalid UPI ID:\n${invalidReasons.joinToString("\n")}"
}
}
return view
Expand All @@ -75,4 +77,18 @@ class Login : Fragment() {
val upiIdRegex = Regex("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+$")
return upiId.contains("@") && !upiId.contains(" ") && upiIdRegex.matches(upiId)
}

private fun getInvalidUpiIdReasons(upiId: String): List<String> {
val reasons = mutableListOf<String>()
if (!upiId.contains("@")) {
reasons.add("Must contain '@'") // Rule 2
}
if (upiId.contains(" ")) {
reasons.add("Should not contain whitespace") // Rule 3
}
if (!Regex("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+$").matches(upiId)) {
reasons.add("Invalid characters used") // Rule 1 (and implicitly covers Rule 4)
}
return reasons
}
}

0 comments on commit 3f63c0a

Please sign in to comment.