Author: Adam <git@apiote.xyz>
fix network on main thread
%!v(PANIC=String method: strings: negative Repeat count)
diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt index e787df190a83d292dcc5616c2ad47d3d3f010b28..08bac2980eae007f18460cb14f3bbf26b76a4320 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.kt @@ -11,7 +11,9 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import xyz.apiote.bimba.czwek.api.ErrorResponse import xyz.apiote.bimba.czwek.api.QueryableV1 import xyz.apiote.bimba.czwek.api.QueryablesResponse @@ -30,14 +32,14 @@ val result = queryQueryables(cm, server, query, limit = 12) if (result.error != null) { // xxx intentionally no error showing in suggestions if (result.stream != null) { - val response = ErrorResponse.unmarshal(result.stream) + val response = withContext(Dispatchers.IO) {ErrorResponse.unmarshal(result.stream)} Log.e("Suggestion", "${result.error.statusCode}, ${response.message}") } else { Log.e("Suggestion", "${result.error.statusCode}") } } else { mutableQueryables.value = - when (val response = QueryablesResponse.unmarshal(result.stream!!)) { + when (val response = withContext(Dispatchers.IO) {QueryablesResponse.unmarshal(result.stream!!)}) { is QueryablesResponseDev -> response.queryables is QueryablesResponseV1 -> response.queryables else -> null diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/map/MapViewModel.kt b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/map/MapViewModel.kt index 8fdef9187436c93e89356801645d296208a733d3..1c3111a2e39e2dc4a0661829ae6c59847c3660e4 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/map/MapViewModel.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/map/MapViewModel.kt @@ -18,7 +18,9 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.android.material.bottomsheet.BottomSheetDialogFragment +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.api.ErrorLocatable import xyz.apiote.bimba.czwek.api.ErrorResponse @@ -45,15 +47,14 @@ _locatables.value = listOf(ErrorLocatable(result.error.stringResource)) if (result.stream != null) { Log.w( "Map", - "${result.error.statusCode}, ${ErrorResponse.unmarshal(result.stream).message}" + "${result.error.statusCode}, ${withContext(Dispatchers.IO) {ErrorResponse.unmarshal(result.stream).message}}" ) } else { Log.w("Map", "${result.error.statusCode}") } return@launch } else { - // fixme NetworkOnMainThreadException - _locatables.value = when (val response = LocatablesResponse.unmarshal(result.stream!!)) { + _locatables.value = when (val response = withContext(Dispatchers.IO) {LocatablesResponse.unmarshal(result.stream!!)}) { is LocatablesResponseDev -> response.locatables is LocatablesResponseV1 -> response.locatables else -> null @@ -168,6 +169,7 @@ when (locatable) { is VehicleV1 -> { showVehicle(this, locatable) } + is StopV1 -> { showStop(this, locatable) } diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt index a8813189a98bdebfaccbc5705bd3c0a1863d8049..456c3dee749cc91e33105568ca1243c2946787fb 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt @@ -91,7 +91,7 @@ if (result.error != null) { showError(result.error) if (result.stream != null) { - val response = ErrorResponse.unmarshal(result.stream) + val response = withContext(Dispatchers.IO) {ErrorResponse.unmarshal(result.stream)} Log.w("Departures", "${result.error.statusCode}, ${response.message}") } else { Log.w( @@ -101,7 +101,7 @@ ) } return@launch } - val (departures, stop) = when (val response = DeparturesResponse.unmarshal(result.stream!!)) { + val (departures, stop) = when (val response = withContext(Dispatchers.IO) {DeparturesResponse.unmarshal(result.stream!!)}) { is DeparturesResponseDev -> Pair(response.departures, response.stop) is DeparturesResponseV1 -> Pair(response.departures, response.stop) else -> Pair(null, null) diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt index 7a1fd06fc1660a786c460a43c62f0c99380809be..ce654214b2ecfb8a9bc0093bdf6ccdd6e5ac5ab9 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/search/ResultsActivity.kt @@ -132,7 +132,7 @@ MainScope().launch { val result = queryQueryables(cm, server, query) if (result.error != null) { if (result.stream != null) { - val response = ErrorResponse.unmarshal(result.stream) + val response = withContext(Dispatchers.IO) {ErrorResponse.unmarshal(result.stream)} Log.w("Results", "${result.error.statusCode}, ${response.message}") } else { Log.w( @@ -195,7 +195,7 @@ } private suspend fun unmarshallQueryablesResponse(stream: InputStream): List<QueryableV1>? { return withContext(Dispatchers.IO) { - when (val response = QueryablesResponse.unmarshal(stream)) { + when (val response = withContext(Dispatchers.IO) {QueryablesResponse.unmarshal(stream)}) { is QueryablesResponseDev -> response.queryables is QueryablesResponseV1 -> response.queryables else -> null diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt index c7ee64b4a4e8c7615ec942ab0725ade256e0551f..853636cd3f3c7b1ca23a8259fc578a2c3d00bf60 100644 --- a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt +++ b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedChooserActivity.kt @@ -10,9 +10,17 @@ import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.content.res.AppCompatResources import androidx.core.content.edit import androidx.recyclerview.widget.LinearLayoutManager +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch -import xyz.apiote.bimba.czwek.api.* +import kotlinx.coroutines.withContext +import xyz.apiote.bimba.czwek.api.ErrorResponse +import xyz.apiote.bimba.czwek.api.FeedInfoV1 +import xyz.apiote.bimba.czwek.api.FeedsResponse +import xyz.apiote.bimba.czwek.api.FeedsResponseDev +import xyz.apiote.bimba.czwek.api.FeedsResponseV1 +import xyz.apiote.bimba.czwek.api.Server +import xyz.apiote.bimba.czwek.api.getFeeds import xyz.apiote.bimba.czwek.dashboard.MainActivity import xyz.apiote.bimba.czwek.databinding.ActivityFeedChooserBinding @@ -54,7 +62,7 @@ val result = getFeeds(cm, Server.get(this@FeedChooserActivity)) if (result.error != null) { showError(result.error.imageResource, result.error.stringResource) if (result.stream != null) { - val response = ErrorResponse.unmarshal(result.stream) + val response = withContext(Dispatchers.IO) {ErrorResponse.unmarshal(result.stream)} Log.w("FeedChooser", "${result.error.statusCode}, ${response.message}") } else { Log.w( @@ -64,7 +72,7 @@ ) } return@launch } - val feeds = when (val response = FeedsResponse.unmarshal(result.stream!!)) { + val feeds = when (val response = withContext(Dispatchers.IO) {FeedsResponse.unmarshal(result.stream!!)}) { is FeedsResponseDev -> response.feeds is FeedsResponseV1 -> response.feeds else -> null