diff options
Diffstat (limited to 'app/src/main/java/dev/equestria/delta/HTTPRequest.kt')
-rw-r--r-- | app/src/main/java/dev/equestria/delta/HTTPRequest.kt | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/app/src/main/java/dev/equestria/delta/HTTPRequest.kt b/app/src/main/java/dev/equestria/delta/HTTPRequest.kt new file mode 100644 index 0000000..1bebbc0 --- /dev/null +++ b/app/src/main/java/dev/equestria/delta/HTTPRequest.kt @@ -0,0 +1,61 @@ +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) + } + } +}
\ No newline at end of file |