summaryrefslogtreecommitdiff
path: root/app/src/main/java/dev/equestria/delta/HTTPRequest.kt
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2023-01-03 22:01:57 +0100
committerMinteck <contact@minteck.org>2023-01-03 22:01:57 +0100
commit5d7695aa154f294defa17e151d88df0b12c60098 (patch)
tree727ee7068c20ce15cd9652bbe27a8e7f186979e5 /app/src/main/java/dev/equestria/delta/HTTPRequest.kt
downloaddelta-android-5d7695aa154f294defa17e151d88df0b12c60098.tar.gz
delta-android-5d7695aa154f294defa17e151d88df0b12c60098.tar.bz2
delta-android-5d7695aa154f294defa17e151d88df0b12c60098.zip
Initial commit
Diffstat (limited to 'app/src/main/java/dev/equestria/delta/HTTPRequest.kt')
-rw-r--r--app/src/main/java/dev/equestria/delta/HTTPRequest.kt61
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