Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.fichaje
- import android.app.Activity
- import android.os.Bundle
- import android.view.View
- import android.widget.*
- import android.content.Context
- import android.content.Intent
- import android.content.SharedPreferences
- import androidx.appcompat.app.ActionBar
- import android.os.AsyncTask
- import android.preference.PreferenceManager
- import android.util.Log
- import android.view.inputmethod.InputMethodManager
- import android.widget.Button
- import android.widget.EditText
- import android.widget.TextView
- import android.widget.Toast
- import androidx.appcompat.app.AppCompatActivity
- import androidx.appcompat.widget.AppCompatButton
- import androidx.appcompat.widget.AppCompatTextView
- import androidx.core.widget.NestedScrollView
- import kotlinx.android.synthetic.main.activity_main.*
- import org.json.JSONException
- import org.json.JSONObject
- import java.io.IOException
- import java.util.HashMap
- import com.google.android.material.snackbar.Snackbar
- import android.widget.ProgressBar
- import androidx.fragment.app.Fragment
- import org.json.JSONArray
- import pl.droidsonroids.gif.GifImageView
- class MainActivity : AppCompatActivity(),View.OnClickListener {
- private val activity = this@MainActivity
- private lateinit var txt_userName: EditText
- //private lateinit var nestedScrollView: NestedScrollView
- private lateinit var textInputEditTextNie: EditText
- private lateinit var textInputEditTextPassword: EditText
- private lateinit var sesioncheckbox: CheckBox
- private lateinit var appCompatButtonLogin: Button
- private lateinit var gif: GifImageView
- private lateinit var errorCompatTextView: AppCompatTextView
- private lateinit var body: LinearLayout
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- //Toast.makeText(this,"hola",Toast.LENGTH_SHORT).show()
- //val id = intent.getStringExtra("id")
- initViews()
- //sesioncheckbox.visibility = View.INVISIBLE
- val sharedPreference:SharedPreference=SharedPreference(this)
- if (sharedPreference.getValueString("user")!=null) {
- //Toast.makeText(this,sharedPreference.getValueString("user"),Toast.LENGTH_SHORT).show()
- textInputEditTextNie.setText(sharedPreference.getValueString("user"))
- textInputEditTextPassword.setText(sharedPreference.getValueString("password"))
- sesioncheckbox.isChecked = true
- verifyFromSQLite()
- //Toast.makeText(this,"si es",Toast.LENGTH_SHORT).show()
- } else {
- initListeners()
- //Toast.makeText(this,sharedPreference.getValueString("user"),Toast.LENGTH_SHORT)
- //Toast.makeText(this,"no es",Toast.LENGTH_SHORT).show()
- }
- }
- private fun initListeners() {
- appCompatButtonLogin!!.setOnClickListener(this)
- body!!.setOnClickListener{
- val inputManager:InputMethodManager =getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
- inputManager.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.SHOW_FORCED)
- }
- }
- private fun initObjects() {
- //inputValidation = InputValidation(activity)
- }
- override fun onClick(v: View) {
- //gifcarga.visibility = View.VISIBLE
- verifyFromSQLite()
- val inputManager:InputMethodManager =getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
- inputManager.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.SHOW_FORCED)
- appCompatButtonLogin.isClickable = false
- }
- /*fun Fragment.hideKeyboard() {
- view?.let { activity?.hideKeyboard(it) }
- }
- fun Activity.hideKeyboard() {
- hideKeyboard(currentFocus ?: View(this))
- }
- fun Context.hideKeyboard(view: View) {
- val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
- inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
- }*/
- private fun verifyFromSQLite() {
- gifcarga.visibility = View.VISIBLE
- val json = JSONObject()
- val sharedPreference:SharedPreference=SharedPreference(this)
- if (sharedPreference.getValueString("user")!=null) {
- json.put("nif", sharedPreference.getValueString("user"))
- json.put("password", sharedPreference.getValueString("password"))
- } else {
- json.put("nif", textInputEditTextNie.text.toString())
- json.put("password", textInputEditTextPassword.text.toString())
- }
- HttpTask(){
- if (it == null) {
- println("connection error")
- return@HttpTask
- }
- println(it)
- val json_res = JSONObject(it)
- if (json_res.getString("status").equals("true")) {
- if (sharedPreference.getValueString("user")==null) {
- if (sesioncheckbox.isChecked){
- sharedPreference.save("user",textInputEditTextNie.text.toString())
- sharedPreference.save("password",textInputEditTextPassword.text.toString())
- }
- }
- var userdata = User()
- var jsonArray = JSONArray(json_res.getString("data"))
- for (i in 0..(jsonArray.length() - 1)) {
- val item = jsonArray.getJSONObject(i)
- userdata.nif = item.getString("nif")
- userdata.nombre = item.getString("nombre")
- userdata.apellidos = item.getString("apellidos")
- userdata.fecha = item.getString("fecha")
- }
- emptyInputEditText()
- val intent = Intent(activity, Bienvenida::class.java)
- intent.putExtra("nif", userdata.nif)
- intent.putExtra("nombre", userdata.nombre)
- intent.putExtra("apellidos", userdata.apellidos)
- intent.putExtra("email", userdata.email)
- intent.putExtra("tlf", userdata.tlf)
- intent.putExtra("fecha", userdata.fecha)
- startActivity(intent)
- Log.d("userdata Data:::::::", userdata.toString())
- } else {
- Log.d("psot Data:::::::", json_res.getString("message"))
- initListeners()
- errorCompatTextView.text = json_res.getString("message")
- appCompatButtonLogin.isClickable = true
- gifcarga.visibility = View.INVISIBLE
- }
- }.execute("POST", "https://.../red/loginapp.php", json.toString())
- }
- private fun emptyInputEditText() {
- textInputEditTextNie!!.text = null
- textInputEditTextPassword!!.text = null
- }
- private fun initViews() {
- //nestedScrollView = findViewById<View>(R.id.nestty) as NestedScrollView
- textInputEditTextNie = findViewById<View>(R.id.etnie) as EditText
- textInputEditTextPassword = findViewById<View>(R.id.etpassword) as EditText
- sesioncheckbox = findViewById<View>(R.id.sesioncheck) as CheckBox
- appCompatButtonLogin = findViewById<View>(R.id.btn_login) as Button
- gif = findViewById<View>(R.id.gifcarga) as GifImageView
- body = findViewById<View>(R.id.ll_main_layout) as LinearLayout
- errorCompatTextView = findViewById<View>(R.id.errortext) as AppCompatTextView
- }
- }
- /*object PreferenceHelper {
- val USER = "USER_ID"
- val USER_PASSWORD = "PASSWORD"
- fun defaultPreference(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
- fun customPreference(context: Context, name: String): SharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)
- inline fun SharedPreferences.editMe(operation: (SharedPreferences.Editor) -> Unit) {
- val editMe = edit()
- operation(editMe)
- editMe.apply()
- }
- var SharedPreferences.user
- get() = getString(USER, "")
- set(value) {
- editMe {
- it.putString(USER, value)
- }
- }
- var SharedPreferences.password
- get() = getString(USER_PASSWORD, "")
- set(value) {
- editMe {
- it.putString(USER_PASSWORD, value)
- }
- }
- var SharedPreferences.clearValues
- get() = { }
- set(value) {
- editMe {
- it.clear()
- }
- }
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement