mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-06 07:29:10 +00:00
Handle generic errors
This commit is contained in:
@@ -75,14 +75,16 @@ class CircularLetterFragment :
|
|||||||
circularLetterViewModel.showMessage.observe(
|
circularLetterViewModel.showMessage.observe(
|
||||||
viewLifecycleOwner,
|
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(
|
Snackbar.make(
|
||||||
view,
|
view,
|
||||||
getString(R.string.snackbar_connection_not_available),
|
message,
|
||||||
Snackbar.LENGTH_LONG
|
Snackbar.LENGTH_LONG
|
||||||
).show()
|
).show()
|
||||||
|
|
||||||
circularLetterViewModel.showMessage.postValue(false)
|
circularLetterViewModel.showMessage.postValue(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -91,8 +93,6 @@ class CircularLetterFragment :
|
|||||||
{
|
{
|
||||||
if (it) {
|
if (it) {
|
||||||
binding.circularsRefresh.isRefreshing = false
|
binding.circularsRefresh.isRefreshing = false
|
||||||
|
|
||||||
circularLetterViewModel.showMessage.postValue(false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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 }
|
val circularsUpdated = MutableLiveData<Boolean>().apply { value = false }
|
||||||
private var isNotUpdating = true
|
private var isNotUpdating = true
|
||||||
|
|
||||||
@@ -65,9 +65,8 @@ class CircularLetterViewModel internal constructor(
|
|||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
isNotUpdating = false
|
isNotUpdating = false
|
||||||
|
|
||||||
if (circularRepository.updateCirculars(false).second == -1) {
|
val result = circularRepository.updateCirculars(false).second
|
||||||
showMessage.postValue(true)
|
showMessage.postValue(result)
|
||||||
}
|
|
||||||
|
|
||||||
isNotUpdating = true
|
isNotUpdating = true
|
||||||
circularsUpdated.postValue(true)
|
circularsUpdated.postValue(true)
|
||||||
|
|||||||
@@ -83,7 +83,9 @@ class PollWork(appContext: Context, workerParams: WorkerParameters) :
|
|||||||
val circularRepository = AndroidCircularRepository.getInstance(applicationContext)
|
val circularRepository = AndroidCircularRepository.getInstance(applicationContext)
|
||||||
|
|
||||||
val result = circularRepository.updateCirculars()
|
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()
|
return@coroutineScope Result.retry()
|
||||||
|
|
||||||
val newCirculars = result.first
|
val newCirculars = result.first
|
||||||
|
|||||||
@@ -77,6 +77,7 @@
|
|||||||
<string name="snackbar_circular_downloaded">La circolare sarà salvata nella cartella Download</string>
|
<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_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_view_attachment">Visualizza allegato</string>
|
||||||
<string name="image_share_attachment">Condividi allegato</string>
|
<string name="image_share_attachment">Condividi allegato</string>
|
||||||
|
|||||||
@@ -78,6 +78,7 @@
|
|||||||
<string name="snackbar_circular_downloaded">The circular letter will be saved into the download folder</string>
|
<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_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_view_attachment">View attachment</string>
|
||||||
<string name="image_share_attachment">Share attachment</string>
|
<string name="image_share_attachment">Share attachment</string>
|
||||||
|
|||||||
@@ -45,8 +45,10 @@ class ServerUtils {
|
|||||||
|
|
||||||
var errorCode = 0
|
var errorCode = 0
|
||||||
val result = server.getCircularsFromServer()
|
val result = server.getCircularsFromServer()
|
||||||
if (result.second == ServerAPI.Companion.Result.ERROR)
|
if (result.second == ServerAPI.Companion.Result.GENERIC_ERROR)
|
||||||
return Pair(emptyList(), -1)
|
return Pair(emptyList(), -1)
|
||||||
|
if (result.second == ServerAPI.Companion.Result.NETWORK_ERROR)
|
||||||
|
return Pair(emptyList(), -2)
|
||||||
|
|
||||||
val oldCirculars = circularDao.getCirculars(server.serverID)
|
val oldCirculars = circularDao.getCirculars(server.serverID)
|
||||||
val newCirculars = result.first
|
val newCirculars = result.first
|
||||||
@@ -71,7 +73,7 @@ class ServerUtils {
|
|||||||
private suspend fun checkServer(server: Server) {
|
private suspend fun checkServer(server: Server) {
|
||||||
val newCirculars = updateCirculars(server)
|
val newCirculars = updateCirculars(server)
|
||||||
|
|
||||||
if (newCirculars.second != -1 && newCirculars.first.isNotEmpty()) {
|
if (newCirculars.second >= 0 && newCirculars.first.isNotEmpty()) {
|
||||||
for (circular in newCirculars.first) {
|
for (circular in newCirculars.first) {
|
||||||
PushNotificationUtils.createPushNotification(
|
PushNotificationUtils.createPushNotification(
|
||||||
circular,
|
circular,
|
||||||
|
|||||||
@@ -11,9 +11,13 @@ class CircularRepository(
|
|||||||
|
|
||||||
var errorCode = 0
|
var errorCode = 0
|
||||||
val result = serverAPI.getCircularsFromServer()
|
val result = serverAPI.getCircularsFromServer()
|
||||||
if (result.second == ServerAPI.Companion.Result.ERROR)
|
|
||||||
|
if (result.second == ServerAPI.Companion.Result.GENERIC_ERROR)
|
||||||
return Pair(emptyList(), -1)
|
return Pair(emptyList(), -1)
|
||||||
|
|
||||||
|
if (result.second == ServerAPI.Companion.Result.NETWORK_ERROR)
|
||||||
|
return Pair(emptyList(), -2)
|
||||||
|
|
||||||
val oldCirculars = circularDao.getCirculars(serverAPI.serverID())
|
val oldCirculars = circularDao.getCirculars(serverAPI.serverID())
|
||||||
val newCirculars = result.first
|
val newCirculars = result.first
|
||||||
|
|
||||||
|
|||||||
@@ -38,8 +38,11 @@ class ServerAPI(serverName: Servers) {
|
|||||||
suspend fun getCircularsFromServer(): Pair<List<Circular>, Result> = withContext(PlatformDispatcher.IO) {
|
suspend fun getCircularsFromServer(): Pair<List<Circular>, Result> = withContext(PlatformDispatcher.IO) {
|
||||||
val newCircularsAvailable = server.newCircularsAvailable()
|
val newCircularsAvailable = server.newCircularsAvailable()
|
||||||
|
|
||||||
if (newCircularsAvailable.second == Result.ERROR)
|
if (newCircularsAvailable.second == Result.GENERIC_ERROR)
|
||||||
return@withContext Pair(emptyList(), Result.ERROR)
|
return@withContext Pair(emptyList(), Result.GENERIC_ERROR)
|
||||||
|
|
||||||
|
if (newCircularsAvailable.second == Result.NETWORK_ERROR)
|
||||||
|
return@withContext Pair(emptyList(), Result.NETWORK_ERROR)
|
||||||
|
|
||||||
if (!newCircularsAvailable.first)
|
if (!newCircularsAvailable.first)
|
||||||
return@withContext Pair(emptyList(), Result.SUCCESS)
|
return@withContext Pair(emptyList(), Result.SUCCESS)
|
||||||
@@ -57,7 +60,7 @@ class ServerAPI(serverName: Servers) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum class Result {
|
enum class Result {
|
||||||
SUCCESS, ERROR
|
SUCCESS, NETWORK_ERROR, GENERIC_ERROR
|
||||||
}
|
}
|
||||||
|
|
||||||
val numberOfServers = Servers.values().size
|
val numberOfServers = Servers.values().size
|
||||||
|
|||||||
@@ -22,7 +22,9 @@ class CurieServer(ktorClient: HttpClient) : Server(ktorClient) {
|
|||||||
Pair(list, ServerAPI.Companion.Result.SUCCESS)
|
Pair(list, ServerAPI.Companion.Result.SUCCESS)
|
||||||
}
|
}
|
||||||
} catch (exception: IOException) {
|
} 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,7 +43,9 @@ class PorporatoServer(ktorClient: HttpClient) : Server(ktorClient) {
|
|||||||
|
|
||||||
Pair(list, ServerAPI.Companion.Result.SUCCESS)
|
Pair(list, ServerAPI.Companion.Result.SUCCESS)
|
||||||
} catch (exception: IOException) {
|
} 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user