Skip to content

Instantly share code, notes, and snippets.

@khan-mujeeb
Created July 27, 2023 19:32
Show Gist options
  • Save khan-mujeeb/b298bd1e13fd6535968919e7bd79faac to your computer and use it in GitHub Desktop.
Save khan-mujeeb/b298bd1e13fd6535968919e7bd79faac to your computer and use it in GitHub Desktop.
package com.example.admindashboard.account
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.text.Editable
import android.text.TextWatcher
import android.widget.Toast
import com.example.admindashboard.MainActivity
import com.example.admindashboard.databinding.ActivityLoginBinding
class LoginActivity : AppCompatActivity() {
lateinit var binding: ActivityLoginBinding
// our machine name
private val stateMachineName = "State Machine 1"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding!!.root)
onEmailFoucus()
onPasswordFoucus()
eyePostionChange()
subscribeOnClickEvents()
}
// when user will click on login
private fun subscribeOnClickEvents() {
binding!!.loginBtn.setOnClickListener {
binding!!.password.clearFocus()
val email = binding!!.email.text.toString()
val password = binding!!.password.text.toString()
// input field validation
if(email.isNotEmpty() && password.isNotEmpty()) {
LoginAccount(email, password)
} else {
Toast.makeText(this, "Enter Id and Password", Toast.LENGTH_SHORT).show()
}
}
}
// user authentication
private fun LoginAccount(email: String, password: String) {
if (email == "admin@gmail.com" && password == "123456"){
Handler(mainLooper).postDelayed({
binding!!.loginCharacter.controller.fireState(stateMachineName, "success")
}, 2000)
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show()
}else{
Handler(mainLooper).postDelayed({
binding!!.loginCharacter.controller.fireState(stateMachineName, "fail")
},2000)
Toast.makeText(this, "Invalid Id or Password", Toast.LENGTH_SHORT).show()
}
}
// functon for look state
private fun eyePostionChange() {
binding!!.email.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
try {
binding!!.loginCharacter.controller.setNumberState(stateMachineName, "Look", 2*p0!!.length.toFloat())
} catch (_: Exception) {
}
}
override fun afterTextChanged(p0: Editable?) {
}
})
}
// function for handsup state
private fun onPasswordFoucus() {
binding!!.password.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
binding!!.loginCharacter.controller.setBooleanState(stateMachineName, "hands_up", true)
} else {
binding!!.loginCharacter.controller.setBooleanState(stateMachineName, "hands_up", false)
}
}
}
// function for check state
private fun onEmailFoucus() {
binding!!.email.setOnFocusChangeListener { v, hasFocus ->
if (hasFocus) {
binding!!.loginCharacter.controller.setBooleanState(
stateMachineName = stateMachineName,
inputName = "Check",
value = true
)
} else {
binding!!.loginCharacter.controller.setBooleanState(
stateMachineName = stateMachineName,
inputName = "Check",
value = false
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment