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

This commit is contained in:
Amine Bouabdallaoui 2025-03-25 00:13:30 +01:00
parent 7c65a63315
commit bb901b64fa
10 changed files with 170 additions and 133 deletions

View File

@ -77,7 +77,6 @@ class `1-LoginActivityTest` : WithANRException() {
@Test
fun `4-connectError`() {
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)))
}

View File

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

View File

@ -108,7 +108,7 @@ class LoginActivity :
private fun goToMain() {
CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.IO).launch {
repository.updateApiInformation()
ACRA.errorReporter.putCustomData(
"SELFOSS_API_VERSION",
@ -127,6 +127,9 @@ class LoginActivity :
binding.urlView.error = getString(R.string.wrong_infos)
binding.loginView.error = getString(R.string.wrong_infos)
binding.passwordView.error = getString(R.string.wrong_infos)
binding.urlView.requestFocus()
showProgress(false)
}
private fun attemptLogin() {
@ -160,22 +163,12 @@ class LoginActivity :
repository.refreshLoginInformation(url, login, password)
CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.IO).launch {
try {
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()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (result) {
val errorFetching = repository.checkIfFetchFails()
if (!errorFetching) {
@ -186,9 +179,26 @@ class LoginActivity :
} else {
preferenceError()
}
showProgress(false)
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(

View File

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

View File

@ -27,7 +27,7 @@ class ReaderActivity :
DIAware {
private var currentItem: Int = 0
private lateinit var toolbarMenu: Menu
private var toolbarMenu: Menu? = null
private lateinit var binding: ActivityReaderBinding
@ -90,8 +90,10 @@ class ReaderActivity :
}
private fun updateStarIcon() {
if (toolbarMenu != null) {
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) {
@ -133,8 +135,10 @@ class ReaderActivity :
private fun alignmentMenu() {
val showJustify = appSettingsService.getActiveAllignment() == AppSettingsService.ALIGN_LEFT
toolbarMenu.findItem(R.id.align_left).isVisible = !showJustify
toolbarMenu.findItem(R.id.align_justify).isVisible = showJustify
if (toolbarMenu != null) {
toolbarMenu!!.findItem(R.id.align_left).isVisible = !showJustify
toolbarMenu!!.findItem(R.id.align_justify).isVisible = showJustify
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {

View File

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

View File

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

View File

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

View File

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

View File

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