Bimba.git

commit 2919df3a7321d4e904d6ed9cdd1c3c8c32a5ef6c

Author: Adam <git@apiote.xyz>

process errors in requests

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt b/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
index c1ae81a02ba8a03d5b51abaadeb161af3041b940..76f2a4096a43a1b61b786cf62b0330a38245b88c 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/api/Api.kt
@@ -1,5 +1,6 @@
 package ml.adamsprogs.bimba.api
 
+import android.util.Log
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.withContext
 import java.io.InputStream
@@ -7,15 +8,15 @@ import java.net.HttpURLConnection
 import java.net.URL
 import java.net.URLEncoder
 
-suspend fun queryItems(query: String): InputStream {
+suspend fun queryItems(query: String): InputStream? {
 	return request("https://bimba.apiote.xyz", "poznan_ztm", "items", mapOf("q" to query))
 }
 
-suspend fun locateItems(plusCode: String): InputStream {
+suspend fun locateItems(plusCode: String): InputStream? {
 	return request("https://bimba.apiote.xyz", "poznan_ztm", "items", mapOf("near" to plusCode))
 }
 
-suspend fun getDepartures(stop: String, line: String?): InputStream {
+suspend fun getDepartures(stop: String, line: String?): InputStream? {
 	return request("https://bimba.apiote.xyz", "poznan_ztm", "departures", mapOf("code" to stop))
 }
 
@@ -25,7 +26,7 @@ 	host: String,
 	feed: String,
 	resource: String,
 	params: Map<String, String>
-): InputStream {
+): InputStream? {
 	return withContext(Dispatchers.IO) {
 		val url = URL(
 			"$host/api/$feed/$resource${
@@ -42,7 +43,11 @@ 		)
 		val c = (url.openConnection() as HttpURLConnection).apply {
 			setRequestProperty("X-Bimba-Token", "ef0179272e7270e1a2da1710a8ba24e1")
 		}
-		// todo handle errors
-		c.inputStream
+		try {
+			c.inputStream
+		} catch (e: Exception) {
+			Log.e("request", e.stackTraceToString())
+			null
+		}
 	}
 }
\ No newline at end of file




diff --git a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt
index b4127b6668c4abbb97446356ec8637d671f1db04..38855e9f151ec3819b86e03416efd65de74c9048 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/dashboard/ui/home/HomeViewModel.kt
@@ -18,7 +18,11 @@
 	fun getItems(query: String) {
 		viewModelScope.launch {
 			val itemsStream = queryItems(query)
-			mutableItems.value = unmarshallItemResponse(itemsStream)
+			if (itemsStream == null) {
+				// todo Toast.makeText(context, "Couldn't get response", Toast.LENGTH_SHORT).show()
+			} else {
+				mutableItems.value = unmarshallItemResponse(itemsStream)
+			}
 		}
 	}
 




diff --git a/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt
index 9a152b387a0476eada01ddaf1c9051f225f5ca5e..e83f90388aabcd30705aebeee750078f347af26e 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt
@@ -1,9 +1,11 @@
 package ml.adamsprogs.bimba.departures
 
+import android.content.Context
 import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
 import android.util.Log
 import android.view.View
+import android.widget.Toast
 import androidx.core.view.WindowCompat
 import androidx.recyclerview.widget.LinearLayoutManager
 import kotlinx.coroutines.Dispatchers
@@ -43,7 +45,12 @@
 		MainScope().launch {
 			intent?.extras?.getString("code")?.let {
 				val departuresStream = getDepartures(it, null)
-				updateItems(unmarshallDepartureResponse(departuresStream))
+				if (departuresStream == null) {
+					// todo show empty state
+					Toast.makeText(this@DeparturesActivity as Context, "Couldn't get response", Toast.LENGTH_SHORT).show()
+				} else {
+					updateItems(unmarshallDepartureResponse(departuresStream))
+				}
 			}
 		}
 	}




diff --git a/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt b/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
index bbf6efd30df91934f5cf3b6949a531fe257d290f..09164011bdd3a00a8bb7a32d8a8428619fd93deb 100644
--- a/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
+++ b/app/src/main/java/ml/adamsprogs/bimba/search/ResultsActivity.kt
@@ -95,7 +95,11 @@
 	private fun getItemsByQuery(query: String) {
 		MainScope().launch {
 			val itemsStream = queryItems(query)
-			updateItems(unmarshallItemResponse(itemsStream))
+			if (itemsStream == null) {
+				// todo show empty state
+			} else {
+				updateItems(unmarshallItemResponse(itemsStream))
+			}
 			Log.v("RESPONSE", "getItemsByQuery")
 		}
 	}
@@ -104,7 +108,11 @@ 	private fun getItemsByLocation(plusCode: String) {
 		Log.v("RESPONSE", "getting ItemsByLocation")
 		MainScope().launch {
 			val itemsStream = locateItems(plusCode)
-			updateItems(unmarshallItemResponse(itemsStream))
+			if (itemsStream == null) {
+				// todo show empty state
+			} else {
+				updateItems(unmarshallItemResponse(itemsStream))
+			}
 			Log.v("RESPONSE", "getItemsByLocation")
 		}
 	}