package dev.equestria.delta import android.content.Context import android.content.res.Resources import android.util.Log import com.android.volley.Request import com.android.volley.toolbox.JsonObjectRequest import com.android.volley.toolbox.Volley import com.google.android.material.dialog.MaterialAlertDialogBuilder import dev.equestria.delta.databinding.ActivityMainBinding import org.json.JSONObject import kotlin.system.exitProcess class HTTPRequest { companion object { fun request( url: String, session: String?, context: Context, resources: Resources, activity: MainActivity, initial: Boolean, binding: ActivityMainBinding, positiveCallback: (JSONObject) -> Unit = { }, negativeCallback: (Unit) -> Unit = { } ) { val volleyQueue = Volley.newRequestQueue(context) val data = JSONObject() data.put("session", session) val jsonObjectRequest = JsonObjectRequest(Request.Method.POST, url, data, { response -> Log.i("HTTPRequest", response.toString()) positiveCallback(response) }, { error -> MaterialAlertDialogBuilder(context).setCancelable(false) .setTitle(resources.getString(R.string.offline_title)) .setMessage(resources.getString(R.string.offline_message)) .setPositiveButton(resources.getString(R.string.offline_retry)) { _, _ -> if (initial) { activity.initialCheck() } else { binding.webView.reload() } }.setNegativeButton(resources.getString(R.string.offline_close)) { _, _ -> activity.moveTaskToBack(true) exitProcess(0) }.setNeutralButton(resources.getString(R.string.offline_status)) { _, _ -> negativeCallback(Unit) }.show() Log.e("HTTPRequest", "Request error: ${error.localizedMessage}") }) volleyQueue.add(jsonObjectRequest) } } }