Handle generic errors

This commit is contained in:
2021-01-15 11:00:52 +01:00
parent 45034b294c
commit 5550de5836
10 changed files with 34 additions and 18 deletions

View File

@@ -75,14 +75,16 @@ class CircularLetterFragment :
circularLetterViewModel.showMessage.observe(
viewLifecycleOwner,
{
if (it) activity?.findViewById<ConstraintLayout>(R.id.container)?.let { view ->
if (it < 0) activity?.findViewById<ConstraintLayout>(R.id.container)?.let { view ->
val message = if (it == -1) getString(R.string.snackbar_generic_error) else getString(R.string.snackbar_connection_not_available)
Snackbar.make(
view,
getString(R.string.snackbar_connection_not_available),
message,
Snackbar.LENGTH_LONG
).show()
circularLetterViewModel.showMessage.postValue(false)
circularLetterViewModel.showMessage.postValue(0)
}
}
)
@@ -91,8 +93,6 @@ class CircularLetterFragment :
{
if (it) {
binding.circularsRefresh.isRefreshing = false
circularLetterViewModel.showMessage.postValue(false)
}
}
)

View File

@@ -49,7 +49,7 @@ class CircularLetterViewModel internal constructor(
}
}
val showMessage = MutableLiveData<Boolean>().apply { value = false }
val showMessage = MutableLiveData<Int>().apply { value = 0 }
val circularsUpdated = MutableLiveData<Boolean>().apply { value = false }
private var isNotUpdating = true
@@ -65,9 +65,8 @@ class CircularLetterViewModel internal constructor(
viewModelScope.launch {
isNotUpdating = false
if (circularRepository.updateCirculars(false).second == -1) {
showMessage.postValue(true)
}
val result = circularRepository.updateCirculars(false).second
showMessage.postValue(result)
isNotUpdating = true
circularsUpdated.postValue(true)

View File

@@ -83,7 +83,9 @@ class PollWork(appContext: Context, workerParams: WorkerParameters) :
val circularRepository = AndroidCircularRepository.getInstance(applicationContext)
val result = circularRepository.updateCirculars()
if (result.second == -1)
// Retry only if it's a network error, otherwise there is probably an issue with the parser code which can only be solved with an update
if (result.second == -2)
return@coroutineScope Result.retry()
val newCirculars = result.first

View File

@@ -77,6 +77,7 @@
<string name="snackbar_circular_downloaded">La circolare sarà salvata nella cartella Download</string>
<string name="snackbar_connection_not_available">Rete non disponibile. I risultati potrebbero non essere aggiornati</string>
<string name="snackbar_generic_error">Si è verificato un errore</string>
<string name="image_view_attachment">Visualizza allegato</string>
<string name="image_share_attachment">Condividi allegato</string>

View File

@@ -78,6 +78,7 @@
<string name="snackbar_circular_downloaded">The circular letter will be saved into the download folder</string>
<string name="snackbar_connection_not_available">Network not available. Results may be outdated</string>
<string name="snackbar_generic_error">An error has occurred</string>
<string name="image_view_attachment">View attachment</string>
<string name="image_share_attachment">Share attachment</string>

View File

@@ -45,8 +45,10 @@ class ServerUtils {
var errorCode = 0
val result = server.getCircularsFromServer()
if (result.second == ServerAPI.Companion.Result.ERROR)
if (result.second == ServerAPI.Companion.Result.GENERIC_ERROR)
return Pair(emptyList(), -1)
if (result.second == ServerAPI.Companion.Result.NETWORK_ERROR)
return Pair(emptyList(), -2)
val oldCirculars = circularDao.getCirculars(server.serverID)
val newCirculars = result.first
@@ -71,7 +73,7 @@ class ServerUtils {
private suspend fun checkServer(server: Server) {
val newCirculars = updateCirculars(server)
if (newCirculars.second != -1 && newCirculars.first.isNotEmpty()) {
if (newCirculars.second >= 0 && newCirculars.first.isNotEmpty()) {
for (circular in newCirculars.first) {
PushNotificationUtils.createPushNotification(
circular,

View File

@@ -11,9 +11,13 @@ class CircularRepository(
var errorCode = 0
val result = serverAPI.getCircularsFromServer()
if (result.second == ServerAPI.Companion.Result.ERROR)
if (result.second == ServerAPI.Companion.Result.GENERIC_ERROR)
return Pair(emptyList(), -1)
if (result.second == ServerAPI.Companion.Result.NETWORK_ERROR)
return Pair(emptyList(), -2)
val oldCirculars = circularDao.getCirculars(serverAPI.serverID())
val newCirculars = result.first

View File

@@ -38,8 +38,11 @@ class ServerAPI(serverName: Servers) {
suspend fun getCircularsFromServer(): Pair<List<Circular>, Result> = withContext(PlatformDispatcher.IO) {
val newCircularsAvailable = server.newCircularsAvailable()
if (newCircularsAvailable.second == Result.ERROR)
return@withContext Pair(emptyList(), Result.ERROR)
if (newCircularsAvailable.second == Result.GENERIC_ERROR)
return@withContext Pair(emptyList(), Result.GENERIC_ERROR)
if (newCircularsAvailable.second == Result.NETWORK_ERROR)
return@withContext Pair(emptyList(), Result.NETWORK_ERROR)
if (!newCircularsAvailable.first)
return@withContext Pair(emptyList(), Result.SUCCESS)
@@ -57,7 +60,7 @@ class ServerAPI(serverName: Servers) {
}
enum class Result {
SUCCESS, ERROR
SUCCESS, NETWORK_ERROR, GENERIC_ERROR
}
val numberOfServers = Servers.values().size

View File

@@ -22,7 +22,9 @@ class CurieServer(ktorClient: HttpClient) : Server(ktorClient) {
Pair(list, ServerAPI.Companion.Result.SUCCESS)
}
} catch (exception: IOException) {
Pair(emptyList(), ServerAPI.Companion.Result.ERROR)
Pair(emptyList(), ServerAPI.Companion.Result.NETWORK_ERROR)
} catch (exception: Exception) {
Pair(emptyList(), ServerAPI.Companion.Result.GENERIC_ERROR)
}
}

View File

@@ -43,7 +43,9 @@ class PorporatoServer(ktorClient: HttpClient) : Server(ktorClient) {
Pair(list, ServerAPI.Companion.Result.SUCCESS)
} catch (exception: IOException) {
Pair(emptyList(), ServerAPI.Companion.Result.ERROR)
Pair(emptyList(), ServerAPI.Companion.Result.NETWORK_ERROR)
} catch (exception: Exception) {
Pair(emptyList(), ServerAPI.Companion.Result.GENERIC_ERROR)
}
}