package com.project.fplus.fplusapp import android.annotation.SuppressLint import android.app.ActivityOptions import android.content.Intent import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.widget.Button import android.widget.EditText import android.widget.ImageView import android.widget.Toast import com.project.fplus.fplusapp.Segment.ConnectionDetector import android.os.AsyncTask import android.os.Build import android.view.View import kotlinx.android.synthetic.main.activity_login.* import java.lang.ref.WeakReference import org.json.JSONException import android.util.Log import androidx.annotation.RequiresApi import androidx.core.app.ActivityCompat import com.project.fplus.fplusapp.Model.UserManager import kotlinx.android.synthetic.main.activity_splash_screen.* import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.Response import java.io.IOException import okhttp3.MultipartBody import org.json.JSONArray class LoginActivity : AppCompatActivity(){ lateinit var cd: ConnectionDetector var myVariable = 10 private var user_name : String? = null private var name : String? = null private var area : String? = null private val REQUEST_MAIN = 3 @RequiresApi(Build.VERSION_CODES.LOLLIPOP) @SuppressLint("WrongViewCast") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N){ this@LoginActivity.btn_sign_in.background = ActivityCompat.getDrawable(this, R.drawable.bg_edittext) } cd = ConnectionDetector() this@LoginActivity.btn_sign_in.setOnClickListener { if (cd.isConnectingToInternet(this@LoginActivity)) { val task = MyAsyncTask(this) task.execute(0) try { var jsonArray = JSONArray(task.get()) for (jsonIndex in 0..(jsonArray.length() - 1)) { user_name = jsonArray.getJSONObject(jsonIndex).getString("SALE_USERNAME") name = jsonArray.getJSONObject(jsonIndex).getString("SALE_NAME") area = jsonArray.getJSONObject(jsonIndex).getString("SALE_AREA") Toast.makeText(this@LoginActivity, jsonArray.getJSONObject(jsonIndex).getString("SALE_NAME"), Toast.LENGTH_SHORT).show() } var mManager = UserManager(this@LoginActivity) mManager.saveLogin(user_name!!,name!!,area!!) val intent = Intent(this, MainActivity::class.java) val options = ActivityOptions .makeSceneTransitionAnimation( this, btn_sign_in, getString(R.string.transition_activity_login) ) startActivity(intent, options.toBundle()) finish() } catch (e: JSONException) { Toast.makeText(this@LoginActivity,getString(R.string.strReturnNoData), Toast.LENGTH_SHORT).show() } } else { Toast.makeText(applicationContext,getString(R.string.strCheckInterNet), Toast.LENGTH_LONG).show() } } this@LoginActivity.ed_username.addTextChangedListener(object : TextWatcher { override fun afterTextChanged(s: Editable?) = Unit override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { if(count>10){ this@LoginActivity.btn_sign_in.isClickable=false Toast.makeText(this@LoginActivity,getString(R.string.strCheckTextLength),Toast.LENGTH_SHORT).show() } } }) this@LoginActivity.ed_password.addTextChangedListener(object : TextWatcher { override fun afterTextChanged(s: Editable?) = Unit override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { this@LoginActivity.btn_sign_in.isClickable=true if(count>10){ this@LoginActivity.btn_sign_in.isClickable=false Toast.makeText(this@LoginActivity,getString(R.string.strCheckTextLength),Toast.LENGTH_SHORT).show() } } }) } companion object { class MyAsyncTask internal constructor(context: LoginActivity) : AsyncTask() { private var resp: String? = null private val activityReference: WeakReference = WeakReference(context) private val TAG = "TAG" private val DF_PORT = "http://58.181.206.159:9814" private var MAIN_URL = DF_PORT + "/FplusProject/index.php/JsonMobile/getUserLogin" override fun onPreExecute() { val activity = activityReference.get() if (activity == null || activity.isFinishing) return activity.progressBar.visibility = View.VISIBLE } override fun doInBackground(vararg params: Int?): String? { var response: Response? = null val activity = activityReference.get() try { val requestBody = MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("itemUser", activity!!.ed_username.text.toString().trim()) .addFormDataPart("itemPassword", activity!!.ed_password.text.toString().trim()) .build() val client = OkHttpClient() val request = Request.Builder() .url(MAIN_URL) .post(requestBody) .build() response = client.newCall(request).execute() resp = response.body()!!.string() publishProgress(resp) //Toast.makeText(activity,resp, Toast.LENGTH_LONG).show() } catch (e: IOException) { Log.e(TAG, "" + e.localizedMessage) } catch (e: JSONException) { Log.e(TAG, "" + e.localizedMessage) } return resp } override fun onPostExecute(result: String?) { val activity = activityReference.get() if (activity == null || activity.isFinishing) return activity.progressBar.visibility = View.GONE activity.myVariable = 100 /* try { var jsonArray = JSONArray(result) for (jsonIndex in 0..(jsonArray.length() - 1)) { user_name = jsonArray.getJSONObject(jsonIndex).getString("SALE_USERNAME") name = jsonArray.getJSONObject(jsonIndex).getString("SALE_NAME") area = jsonArray.getJSONObject(jsonIndex).getString("SALE_AREA") Toast.makeText(activity, jsonArray.getJSONObject(jsonIndex).getString("SALE_NAME"), Toast.LENGTH_SHORT).show() } var mManager = UserManager(activity) mManager.saveLogin(user_name!!,name!!,area!!) activity.myVariable = 100 } catch (e: JSONException) { Toast.makeText(activity,activity.getString(R.string.strReturnNoData), Toast.LENGTH_SHORT).show() Log.e(TAG, "" + e.localizedMessage) } */ } override fun onProgressUpdate(vararg text: String?) { val activity = activityReference.get() if (activity == null || activity.isFinishing) return //Toast.makeText(activity, text.firstOrNull(), Toast.LENGTH_SHORT).show() } } } override fun onStart() { super.onStart() this@LoginActivity.btn_sign_in.isClickable=false } }