chore: better handling of coroutine dispatchers.
All checks were successful
Check PR code / BuildAndTestAndCoverage (pull_request) Successful in 30m26s

This commit is contained in:
Amine Bouabdallaoui 2025-03-25 00:13:30 +01:00
parent 7c65a63315
commit 1b2e9edc8c
10 changed files with 171 additions and 134 deletions

View File

@ -77,7 +77,6 @@ class `1-LoginActivityTest` : WithANRException() {
@Test @Test
fun `4-connectError`() { fun `4-connectError`() {
performLogin("http://10.0.2.2:8889") performLogin("http://10.0.2.2:8889")
onView(withId(R.id.urlView)).perform(click())
onView(withId(R.id.urlView)).check(matches(withError(R.string.wrong_infos))) onView(withId(R.id.urlView)).check(matches(withError(R.string.wrong_infos)))
} }

View File

@ -104,7 +104,7 @@ class HomeActivity :
if (appSettingsService.isItemCachingEnabled()) { if (appSettingsService.isItemCachingEnabled()) {
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.IO).launch {
repository.tryToCacheItemsAndGetNewOnes() repository.tryToCacheItemsAndGetNewOnes()
CountingIdlingResourceSingleton.decrement() CountingIdlingResourceSingleton.decrement()
} }
@ -120,12 +120,8 @@ class HomeActivity :
binding.swipeRefreshLayout.setOnRefreshListener { binding.swipeRefreshLayout.setOnRefreshListener {
repository.offlineOverride = false repository.offlineOverride = false
lastFetchDone = false lastFetchDone = false
CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch {
getElementsAccordingToTab() getElementsAccordingToTab()
binding.swipeRefreshLayout.isRefreshing = false binding.swipeRefreshLayout.isRefreshing = false
CountingIdlingResourceSingleton.decrement()
}
} }
val swipeDirs = val swipeDirs =
@ -289,7 +285,7 @@ class HomeActivity :
handleRecurringTask() handleRecurringTask()
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.IO).launch {
repository.handleDBActions() repository.handleDBActions()
CountingIdlingResourceSingleton.decrement() CountingIdlingResourceSingleton.decrement()
} }
@ -463,8 +459,8 @@ class HomeActivity :
itemType: ItemType, itemType: ItemType,
) { ) {
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch {
binding.swipeRefreshLayout.isRefreshing = true binding.swipeRefreshLayout.isRefreshing = true
CoroutineScope(Dispatchers.IO).launch {
repository.displayedItems = itemType repository.displayedItems = itemType
items = items =
if (appendResults) { if (appendResults) {
@ -472,10 +468,14 @@ class HomeActivity :
} else { } else {
repository.getNewerItems() repository.getNewerItems()
} }
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
binding.swipeRefreshLayout.isRefreshing = false binding.swipeRefreshLayout.isRefreshing = false
handleListResult() handleListResult()
CountingIdlingResourceSingleton.decrement() CountingIdlingResourceSingleton.decrement()
} }
CountingIdlingResourceSingleton.decrement()
}
} }
private fun handleListResult(appendResults: Boolean = false) { private fun handleListResult(appendResults: Boolean = false) {
@ -613,8 +613,10 @@ class HomeActivity :
needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) { needsConfirmation(R.string.menu_home_refresh, R.string.refresh_dialog_message) {
Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.refresh_in_progress, Toast.LENGTH_SHORT).show()
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.IO).launch {
val updatedRemote = repository.updateRemote() val updatedRemote = repository.updateRemote()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (updatedRemote) { if (updatedRemote) {
Toast Toast
.makeText( .makeText(
@ -632,6 +634,8 @@ class HomeActivity :
} }
CountingIdlingResourceSingleton.decrement() CountingIdlingResourceSingleton.decrement()
} }
CountingIdlingResourceSingleton.decrement()
}
} }
return true return true
} }
@ -639,10 +643,12 @@ class HomeActivity :
R.id.readAll -> { R.id.readAll -> {
if (elementsShown == ItemType.UNREAD) { if (elementsShown == ItemType.UNREAD) {
needsConfirmation(R.string.readAll, R.string.markall_dialog_message) { needsConfirmation(R.string.readAll, R.string.markall_dialog_message) {
binding.swipeRefreshLayout.isRefreshing = true
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch { binding.swipeRefreshLayout.isRefreshing = true
CoroutineScope(Dispatchers.IO).launch {
val success = repository.markAllAsRead(items) val success = repository.markAllAsRead(items)
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (success) { if (success) {
Toast Toast
.makeText( .makeText(
@ -661,10 +667,11 @@ class HomeActivity :
Toast.LENGTH_SHORT, Toast.LENGTH_SHORT,
).show() ).show()
} }
handleListResult()
binding.swipeRefreshLayout.isRefreshing = false binding.swipeRefreshLayout.isRefreshing = false
CountingIdlingResourceSingleton.decrement() CountingIdlingResourceSingleton.decrement()
} }
CountingIdlingResourceSingleton.decrement()
}
} }
} }
return true return true

View File

@ -108,7 +108,7 @@ class LoginActivity :
private fun goToMain() { private fun goToMain() {
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.IO).launch {
repository.updateApiInformation() repository.updateApiInformation()
ACRA.errorReporter.putCustomData( ACRA.errorReporter.putCustomData(
"SELFOSS_API_VERSION", "SELFOSS_API_VERSION",
@ -127,6 +127,9 @@ class LoginActivity :
binding.urlView.error = getString(R.string.wrong_infos) binding.urlView.error = getString(R.string.wrong_infos)
binding.loginView.error = getString(R.string.wrong_infos) binding.loginView.error = getString(R.string.wrong_infos)
binding.passwordView.error = getString(R.string.wrong_infos) binding.passwordView.error = getString(R.string.wrong_infos)
binding.urlView.requestFocus()
showProgress(false)
} }
private fun attemptLogin() { private fun attemptLogin() {
@ -160,22 +163,12 @@ class LoginActivity :
repository.refreshLoginInformation(url, login, password) repository.refreshLoginInformation(url, login, password)
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.IO).launch {
try { try {
repository.updateApiInformation() repository.updateApiInformation()
} catch (e: Exception) {
if (e.message?.startsWith("No transformation found") == true) {
Toast
.makeText(
applicationContext,
R.string.application_selfoss_only,
Toast.LENGTH_LONG,
).show()
preferenceError()
showProgress(false)
}
}
val result = repository.login() val result = repository.login()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (result) { if (result) {
val errorFetching = repository.checkIfFetchFails() val errorFetching = repository.checkIfFetchFails()
if (!errorFetching) { if (!errorFetching) {
@ -186,9 +179,26 @@ class LoginActivity :
} else { } else {
preferenceError() preferenceError()
} }
showProgress(false)
CountingIdlingResourceSingleton.decrement() CountingIdlingResourceSingleton.decrement()
} }
} catch (e: Exception) {
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (e.message?.startsWith("No transformation found") == true) {
Toast
.makeText(
applicationContext,
R.string.application_selfoss_only,
Toast.LENGTH_LONG,
).show()
preferenceError()
}
CountingIdlingResourceSingleton.decrement()
}
} finally {
CountingIdlingResourceSingleton.decrement()
}
}
} }
private fun failLoginDetails( private fun failLoginDetails(

View File

@ -73,7 +73,7 @@ class MyApp :
), ),
) )
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Default).launch {
connectivityService.networkAvailableProvider.collect { networkAvailable -> connectivityService.networkAvailableProvider.collect { networkAvailable ->
val toastMessage = val toastMessage =
if (networkAvailable) { if (networkAvailable) {

View File

@ -27,7 +27,7 @@ class ReaderActivity :
DIAware { DIAware {
private var currentItem: Int = 0 private var currentItem: Int = 0
private lateinit var toolbarMenu: Menu private var toolbarMenu: Menu? = null
private lateinit var binding: ActivityReaderBinding private lateinit var binding: ActivityReaderBinding
@ -90,8 +90,10 @@ class ReaderActivity :
} }
private fun updateStarIcon() { private fun updateStarIcon() {
if (toolbarMenu != null) {
val isStarred = allItems.getOrNull(currentItem)?.starred ?: false val isStarred = allItems.getOrNull(currentItem)?.starred ?: false
toolbarMenu.findItem(R.id.star)?.icon?.setTint(if (isStarred) Color.RED else Color.WHITE) toolbarMenu!!.findItem(R.id.star)?.icon?.setTint(if (isStarred) Color.RED else Color.WHITE)
}
} }
override fun onSaveInstanceState(oldInstanceState: Bundle) { override fun onSaveInstanceState(oldInstanceState: Bundle) {
@ -133,8 +135,10 @@ class ReaderActivity :
private fun alignmentMenu() { private fun alignmentMenu() {
val showJustify = appSettingsService.getActiveAllignment() == AppSettingsService.ALIGN_LEFT val showJustify = appSettingsService.getActiveAllignment() == AppSettingsService.ALIGN_LEFT
toolbarMenu.findItem(R.id.align_left).isVisible = !showJustify if (toolbarMenu != null) {
toolbarMenu.findItem(R.id.align_justify).isVisible = showJustify toolbarMenu!!.findItem(R.id.align_left).isVisible = !showJustify
toolbarMenu!!.findItem(R.id.align_justify).isVisible = showJustify
}
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {

View File

@ -58,8 +58,10 @@ class SourcesActivity :
binding.recyclerView.layoutManager = mLayoutManager binding.recyclerView.layoutManager = mLayoutManager
CountingIdlingResourceSingleton.increment() CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.IO).launch {
val response = repository.getSourcesDetails() val response = repository.getSourcesDetails()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (response.isNotEmpty()) { if (response.isNotEmpty()) {
items = response items = response
val mAdapter = val mAdapter =
@ -79,6 +81,8 @@ class SourcesActivity :
} }
CountingIdlingResourceSingleton.decrement() CountingIdlingResourceSingleton.decrement()
} }
CountingIdlingResourceSingleton.decrement()
}
binding.fab.setOnClickListener { binding.fab.setOnClickListener {
startActivity(Intent(this@SourcesActivity, UpsertSourceActivity::class.java)) startActivity(Intent(this@SourcesActivity, UpsertSourceActivity::class.java))

View File

@ -9,6 +9,7 @@ import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import bou.amine.apps.readerforselfossv2.android.databinding.ActivityUpsertSourceBinding import bou.amine.apps.readerforselfossv2.android.databinding.ActivityUpsertSourceBinding
import bou.amine.apps.readerforselfossv2.android.testing.CountingIdlingResourceSingleton
import bou.amine.apps.readerforselfossv2.model.NetworkUnavailableException import bou.amine.apps.readerforselfossv2.model.NetworkUnavailableException
import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.model.SelfossModel
import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.repository.Repository
@ -108,9 +109,12 @@ class UpsertSourceActivity :
binding.progress.visibility = View.GONE binding.progress.visibility = View.GONE
} }
CoroutineScope(Dispatchers.Main).launch { CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.IO).launch {
try { try {
val items = repository.getSpouts() val items = repository.getSpouts()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (items.isNotEmpty()) { if (items.isNotEmpty()) {
val itemsStrings = items.map { it.value.name } val itemsStrings = items.map { it.value.name }
for ((key, value) in items) { for ((key, value) in items) {
@ -135,9 +139,12 @@ class UpsertSourceActivity :
} else { } else {
handleSpoutFailure() handleSpoutFailure()
} }
CountingIdlingResourceSingleton.decrement()
}
} catch (e: NetworkUnavailableException) { } catch (e: NetworkUnavailableException) {
handleSpoutFailure(networkIssue = true) handleSpoutFailure(networkIssue = true)
} }
CountingIdlingResourceSingleton.decrement()
} }
} }
@ -160,7 +167,8 @@ class UpsertSourceActivity :
} }
else -> { else -> {
CoroutineScope(Dispatchers.Main).launch { CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.IO).launch {
val successfullyAddedSource = val successfullyAddedSource =
if (existingSource != null) { if (existingSource != null) {
repository.updateSource( repository.updateSource(
@ -178,6 +186,8 @@ class UpsertSourceActivity :
binding.tags.text.toString(), binding.tags.text.toString(),
) )
} }
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (successfullyAddedSource) { if (successfullyAddedSource) {
finish() finish()
} else { } else {
@ -188,6 +198,9 @@ class UpsertSourceActivity :
Toast.LENGTH_SHORT, Toast.LENGTH_SHORT,
).show() ).show()
} }
CountingIdlingResourceSingleton.decrement()
}
CountingIdlingResourceSingleton.decrement()
} }
} }
} }

View File

@ -334,7 +334,7 @@ class Repository(
_badgeUnread.value -= 1 _badgeUnread.value -= 1
} }
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Default).launch {
updateDBItem(item) updateDBItem(item)
} }
} }
@ -345,7 +345,7 @@ class Repository(
_badgeUnread.value += 1 _badgeUnread.value += 1
} }
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Default).launch {
updateDBItem(item) updateDBItem(item)
} }
} }
@ -356,7 +356,7 @@ class Repository(
_badgeStarred.value += 1 _badgeStarred.value += 1
} }
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Default).launch {
updateDBItem(item) updateDBItem(item)
} }
} }
@ -367,7 +367,7 @@ class Repository(
_badgeStarred.value -= 1 _badgeStarred.value -= 1
} }
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Default).launch {
updateDBItem(item) updateDBItem(item)
} }
} }

View File

@ -82,7 +82,7 @@ class SelfossApi(
} }
modifyRequest { modifyRequest {
Napier.i("Will modify", tag = "HttpSend") Napier.i("Will modify", tag = "HttpSend")
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.IO).launch {
Napier.i("Will login", tag = "HttpSend") Napier.i("Will login", tag = "HttpSend")
login() login()
Napier.i("Did login", tag = "HttpSend") Napier.i("Did login", tag = "HttpSend")

View File

@ -16,7 +16,7 @@ class ConnectivityService {
fun start() { fun start() {
connectivity = Connectivity() connectivity = Connectivity()
connectivity.start() connectivity.start()
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Default).launch {
connectivity.statusUpdates.collect { status -> connectivity.statusUpdates.collect { status ->
when (status) { when (status) {
is Connectivity.Status.Connected -> { is Connectivity.Status.Connected -> {