Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.56 KB | None | 0 0
  1. package com.example.android.fichaje
  2.  
  3. import android.app.Activity
  4. import android.os.Bundle
  5. import android.view.View
  6. import android.widget.*
  7. import android.content.Context
  8. import android.content.Intent
  9. import android.content.SharedPreferences
  10. import androidx.appcompat.app.ActionBar
  11. import android.os.AsyncTask
  12. import android.preference.PreferenceManager
  13. import android.util.Log
  14. import android.view.inputmethod.InputMethodManager
  15. import android.widget.Button
  16. import android.widget.EditText
  17. import android.widget.TextView
  18. import android.widget.Toast
  19. import androidx.appcompat.app.AppCompatActivity
  20. import androidx.appcompat.widget.AppCompatButton
  21. import androidx.appcompat.widget.AppCompatTextView
  22. import androidx.core.widget.NestedScrollView
  23. import kotlinx.android.synthetic.main.activity_main.*
  24. import org.json.JSONException
  25. import org.json.JSONObject
  26. import java.io.IOException
  27. import java.util.HashMap
  28. import com.google.android.material.snackbar.Snackbar
  29. import android.widget.ProgressBar
  30. import androidx.fragment.app.Fragment
  31. import org.json.JSONArray
  32. import pl.droidsonroids.gif.GifImageView
  33.  
  34. class MainActivity : AppCompatActivity(),View.OnClickListener {
  35.  
  36.  
  37. private val activity = this@MainActivity
  38.  
  39. private lateinit var txt_userName: EditText
  40. //private lateinit var nestedScrollView: NestedScrollView
  41. private lateinit var textInputEditTextNie: EditText
  42. private lateinit var textInputEditTextPassword: EditText
  43. private lateinit var sesioncheckbox: CheckBox
  44. private lateinit var appCompatButtonLogin: Button
  45. private lateinit var gif: GifImageView
  46. private lateinit var errorCompatTextView: AppCompatTextView
  47. private lateinit var body: LinearLayout
  48.  
  49.  
  50.  
  51. override fun onCreate(savedInstanceState: Bundle?) {
  52. super.onCreate(savedInstanceState)
  53. setContentView(R.layout.activity_main)
  54.  
  55. //Toast.makeText(this,"hola",Toast.LENGTH_SHORT).show()
  56. //val id = intent.getStringExtra("id")
  57.  
  58. initViews()
  59. //sesioncheckbox.visibility = View.INVISIBLE
  60.  
  61. val sharedPreference:SharedPreference=SharedPreference(this)
  62.  
  63. if (sharedPreference.getValueString("user")!=null) {
  64. //Toast.makeText(this,sharedPreference.getValueString("user"),Toast.LENGTH_SHORT).show()
  65. textInputEditTextNie.setText(sharedPreference.getValueString("user"))
  66. textInputEditTextPassword.setText(sharedPreference.getValueString("password"))
  67. sesioncheckbox.isChecked = true
  68. verifyFromSQLite()
  69. //Toast.makeText(this,"si es",Toast.LENGTH_SHORT).show()
  70. } else {
  71. initListeners()
  72. //Toast.makeText(this,sharedPreference.getValueString("user"),Toast.LENGTH_SHORT)
  73. //Toast.makeText(this,"no es",Toast.LENGTH_SHORT).show()
  74. }
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83. private fun initListeners() {
  84.  
  85. appCompatButtonLogin!!.setOnClickListener(this)
  86. body!!.setOnClickListener{
  87. val inputManager:InputMethodManager =getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  88. inputManager.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.SHOW_FORCED)
  89. }
  90. }
  91.  
  92. private fun initObjects() {
  93.  
  94. //inputValidation = InputValidation(activity)
  95.  
  96. }
  97.  
  98. override fun onClick(v: View) {
  99. //gifcarga.visibility = View.VISIBLE
  100. verifyFromSQLite()
  101. val inputManager:InputMethodManager =getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  102. inputManager.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.SHOW_FORCED)
  103. appCompatButtonLogin.isClickable = false
  104. }
  105.  
  106. /*fun Fragment.hideKeyboard() {
  107. view?.let { activity?.hideKeyboard(it) }
  108. }
  109.  
  110. fun Activity.hideKeyboard() {
  111. hideKeyboard(currentFocus ?: View(this))
  112. }
  113.  
  114. fun Context.hideKeyboard(view: View) {
  115. val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
  116. inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
  117. }*/
  118.  
  119. private fun verifyFromSQLite() {
  120. gifcarga.visibility = View.VISIBLE
  121. val json = JSONObject()
  122. val sharedPreference:SharedPreference=SharedPreference(this)
  123. if (sharedPreference.getValueString("user")!=null) {
  124. json.put("nif", sharedPreference.getValueString("user"))
  125. json.put("password", sharedPreference.getValueString("password"))
  126. } else {
  127. json.put("nif", textInputEditTextNie.text.toString())
  128. json.put("password", textInputEditTextPassword.text.toString())
  129. }
  130.  
  131.  
  132. HttpTask(){
  133. if (it == null) {
  134. println("connection error")
  135. return@HttpTask
  136. }
  137. println(it)
  138. val json_res = JSONObject(it)
  139. if (json_res.getString("status").equals("true")) {
  140.  
  141. if (sharedPreference.getValueString("user")==null) {
  142. if (sesioncheckbox.isChecked){
  143. sharedPreference.save("user",textInputEditTextNie.text.toString())
  144. sharedPreference.save("password",textInputEditTextPassword.text.toString())
  145. }
  146. }
  147.  
  148.  
  149.  
  150. var userdata = User()
  151. var jsonArray = JSONArray(json_res.getString("data"))
  152. for (i in 0..(jsonArray.length() - 1)) {
  153. val item = jsonArray.getJSONObject(i)
  154. userdata.nif = item.getString("nif")
  155. userdata.nombre = item.getString("nombre")
  156. userdata.apellidos = item.getString("apellidos")
  157. userdata.fecha = item.getString("fecha")
  158. }
  159. emptyInputEditText()
  160. val intent = Intent(activity, Bienvenida::class.java)
  161. intent.putExtra("nif", userdata.nif)
  162. intent.putExtra("nombre", userdata.nombre)
  163. intent.putExtra("apellidos", userdata.apellidos)
  164. intent.putExtra("email", userdata.email)
  165.  
  166. intent.putExtra("tlf", userdata.tlf)
  167. intent.putExtra("fecha", userdata.fecha)
  168. startActivity(intent)
  169. Log.d("userdata Data:::::::", userdata.toString())
  170. } else {
  171. Log.d("psot Data:::::::", json_res.getString("message"))
  172. initListeners()
  173. errorCompatTextView.text = json_res.getString("message")
  174. appCompatButtonLogin.isClickable = true
  175. gifcarga.visibility = View.INVISIBLE
  176. }
  177. }.execute("POST", "https://.../red/loginapp.php", json.toString())
  178. }
  179.  
  180. private fun emptyInputEditText() {
  181. textInputEditTextNie!!.text = null
  182. textInputEditTextPassword!!.text = null
  183. }
  184.  
  185. private fun initViews() {
  186. //nestedScrollView = findViewById<View>(R.id.nestty) as NestedScrollView
  187. textInputEditTextNie = findViewById<View>(R.id.etnie) as EditText
  188. textInputEditTextPassword = findViewById<View>(R.id.etpassword) as EditText
  189. sesioncheckbox = findViewById<View>(R.id.sesioncheck) as CheckBox
  190. appCompatButtonLogin = findViewById<View>(R.id.btn_login) as Button
  191. gif = findViewById<View>(R.id.gifcarga) as GifImageView
  192. body = findViewById<View>(R.id.ll_main_layout) as LinearLayout
  193. errorCompatTextView = findViewById<View>(R.id.errortext) as AppCompatTextView
  194. }
  195.  
  196. }
  197.  
  198. /*object PreferenceHelper {
  199.  
  200. val USER = "USER_ID"
  201. val USER_PASSWORD = "PASSWORD"
  202.  
  203. fun defaultPreference(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
  204.  
  205. fun customPreference(context: Context, name: String): SharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE)
  206.  
  207. inline fun SharedPreferences.editMe(operation: (SharedPreferences.Editor) -> Unit) {
  208. val editMe = edit()
  209. operation(editMe)
  210. editMe.apply()
  211. }
  212.  
  213. var SharedPreferences.user
  214. get() = getString(USER, "")
  215. set(value) {
  216. editMe {
  217. it.putString(USER, value)
  218. }
  219. }
  220.  
  221. var SharedPreferences.password
  222. get() = getString(USER_PASSWORD, "")
  223. set(value) {
  224. editMe {
  225. it.putString(USER_PASSWORD, value)
  226. }
  227. }
  228.  
  229. var SharedPreferences.clearValues
  230. get() = { }
  231. set(value) {
  232. editMe {
  233. it.clear()
  234. }
  235. }
  236. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement