Do not fail polling/refresh if one topics fails, closes binwiederhier/ntfy#27

This commit is contained in:
Philipp Heckel 2021-12-11 19:40:32 -05:00
parent 425ab45e84
commit 459e1ff84b
3 changed files with 31 additions and 33 deletions

View file

@ -7,10 +7,7 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.*
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
@ -309,10 +306,12 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
private fun refreshAllSubscriptions() {
lifecycleScope.launch(Dispatchers.IO) {
try {
Log.d(TAG, "Polling for new notifications")
var newNotificationsCount = 0
repository.getSubscriptions().forEach { subscription ->
Log.d(TAG, "Polling for new notifications")
var errors = 0
var errorMessage = "" // First error
var newNotificationsCount = 0
repository.getSubscriptions().forEach { subscription ->
try {
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic)
val newNotifications = repository.onlyNewNotifications(subscription.id, notifications)
newNotifications.forEach { notification ->
@ -326,24 +325,24 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
broadcaster?.send(subscription, notification, result.muted)
}
}
}
val toastMessage = if (newNotificationsCount == 0) {
getString(R.string.refresh_message_no_results)
} else {
getString(R.string.refresh_message_result, newNotificationsCount)
}
runOnUiThread {
Toast.makeText(this@MainActivity, toastMessage, Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
}
Log.d(TAG, "Finished polling for new notifications")
} catch (e: Exception) {
Log.e(TAG, "Polling failed: ${e.message}", e)
runOnUiThread {
Toast.makeText(this@MainActivity, getString(R.string.refresh_message_error, e.message), Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
} catch (e: Exception) {
val topic = topicShortUrl(subscription.baseUrl, subscription.topic)
if (errorMessage == "") errorMessage = "$topic: ${e.message}"
errors++
}
}
val toastMessage = if (errors > 0) {
getString(R.string.refresh_message_error, errors, errorMessage)
} else if (newNotificationsCount == 0) {
getString(R.string.refresh_message_no_results)
} else {
getString(R.string.refresh_message_result, newNotificationsCount)
}
runOnUiThread {
Toast.makeText(this@MainActivity, toastMessage, Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
}
Log.d(TAG, "Finished polling for new notifications")
}
}

View file

@ -29,8 +29,8 @@ class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx,
val broadcaster = BroadcastService(applicationContext)
val api = ApiService()
try {
repository.getSubscriptions().forEach{ subscription ->
repository.getSubscriptions().forEach{ subscription ->
try {
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic)
val newNotifications = repository
.onlyNewNotifications(subscription.id, notifications)
@ -44,18 +44,17 @@ class PollWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx,
broadcaster.send(subscription, notification, result.muted)
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed checking messages: ${e.message}", e)
}
Log.d(TAG, "Finished polling for new notifications")
return@withContext Result.success()
} catch (e: Exception) {
Log.e(TAG, "Failed checking messages: ${e.message}", e)
return@withContext Result.failure()
}
Log.d(TAG, "Finished polling for new notifications")
return@withContext Result.success()
}
}
companion object {
const val VERSION = BuildConfig.VERSION_CODE
const val VERSION = BuildConfig.VERSION_CODE
const val TAG = "NtfyPollWorker"
const val WORK_NAME_PERIODIC = "NtfyPollWorkerPeriodic"
}

View file

@ -23,7 +23,7 @@
<!-- Common refresh toasts -->
<string name="refresh_message_result">%1$d notification(s) received</string>
<string name="refresh_message_no_results">Everything is up-to-date</string>
<string name="refresh_message_error">Could not refresh topic: %1$s</string>
<string name="refresh_message_error">%1$d subscription(s) could not be refreshed\n\n%2$s</string>
<!-- Main activity: Action bar -->
<string name="main_action_bar_title">Subscribed topics</string>