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()
}
getElementsAccordingToTab()
binding.swipeRefreshLayout.isRefreshing = false
}
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
binding.swipeRefreshLayout.isRefreshing = true
CoroutineScope(Dispatchers.IO).launch {
repository.displayedItems = itemType
items =
if (appendResults) {
@ -472,8 +468,12 @@ class HomeActivity :
} else {
repository.getNewerItems()
}
binding.swipeRefreshLayout.isRefreshing = false
handleListResult()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
binding.swipeRefreshLayout.isRefreshing = false
handleListResult()
CountingIdlingResourceSingleton.decrement()
}
CountingIdlingResourceSingleton.decrement()
}
}
@ -613,22 +613,26 @@ 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()
if (updatedRemote) {
Toast
.makeText(
this@HomeActivity,
R.string.refresh_success_response,
Toast.LENGTH_LONG,
).show()
} else {
Toast
.makeText(
this@HomeActivity,
R.string.refresh_failer_message,
Toast.LENGTH_SHORT,
).show()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (updatedRemote) {
Toast
.makeText(
this@HomeActivity,
R.string.refresh_success_response,
Toast.LENGTH_LONG,
).show()
} else {
Toast
.makeText(
this@HomeActivity,
R.string.refresh_failer_message,
Toast.LENGTH_SHORT,
).show()
}
CountingIdlingResourceSingleton.decrement()
}
CountingIdlingResourceSingleton.decrement()
}
@ -641,28 +645,31 @@ 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)
if (success) {
Toast
.makeText(
this@HomeActivity,
R.string.all_posts_read,
Toast.LENGTH_SHORT,
).show()
tabNewBadge.removeBadge()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (success) {
Toast
.makeText(
this@HomeActivity,
R.string.all_posts_read,
Toast.LENGTH_SHORT,
).show()
tabNewBadge.removeBadge()
getElementsAccordingToTab()
} else {
Toast
.makeText(
this@HomeActivity,
R.string.all_posts_not_read,
Toast.LENGTH_SHORT,
).show()
getElementsAccordingToTab()
} else {
Toast
.makeText(
this@HomeActivity,
R.string.all_posts_not_read,
Toast.LENGTH_SHORT,
).show()
}
binding.swipeRefreshLayout.isRefreshing = false
CountingIdlingResourceSingleton.decrement()
}
handleListResult()
binding.swipeRefreshLayout.isRefreshing = false
CountingIdlingResourceSingleton.decrement()
}
}

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,34 +163,41 @@ class LoginActivity :
repository.refreshLoginInformation(url, login, password)
CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.IO).launch {
try {
repository.updateApiInformation()
val result = repository.login()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (result) {
val errorFetching = repository.checkIfFetchFails()
if (!errorFetching) {
goToMain()
} else {
preferenceError()
}
} else {
preferenceError()
}
CountingIdlingResourceSingleton.decrement()
}
} 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)
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()
}
val result = repository.login()
if (result) {
val errorFetching = repository.checkIfFetchFails()
if (!errorFetching) {
goToMain()
} else {
preferenceError()
}
} else {
preferenceError()
}
showProgress(false)
CountingIdlingResourceSingleton.decrement()
}
}

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() {
val isStarred = allItems.getOrNull(currentItem)?.starred ?: false
toolbarMenu.findItem(R.id.star)?.icon?.setTint(if (isStarred) Color.RED else Color.WHITE)
if (toolbarMenu != null) {
val isStarred = allItems.getOrNull(currentItem)?.starred ?: false
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,24 +58,28 @@ class SourcesActivity :
binding.recyclerView.layoutManager = mLayoutManager
CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.Main).launch {
CoroutineScope(Dispatchers.IO).launch {
val response = repository.getSourcesDetails()
if (response.isNotEmpty()) {
items = response
val mAdapter =
SourcesListAdapter(
this@SourcesActivity,
items,
)
binding.recyclerView.adapter = mAdapter
mAdapter.notifyDataSetChanged()
} else {
Toast
.makeText(
this@SourcesActivity,
R.string.cant_get_sources,
Toast.LENGTH_SHORT,
).show()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (response.isNotEmpty()) {
items = response
val mAdapter =
SourcesListAdapter(
this@SourcesActivity,
items,
)
binding.recyclerView.adapter = mAdapter
mAdapter.notifyDataSetChanged()
} else {
Toast
.makeText(
this@SourcesActivity,
R.string.cant_get_sources,
Toast.LENGTH_SHORT,
).show()
}
CountingIdlingResourceSingleton.decrement()
}
CountingIdlingResourceSingleton.decrement()
}

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,36 +109,42 @@ class UpsertSourceActivity :
binding.progress.visibility = View.GONE
}
CoroutineScope(Dispatchers.Main).launch {
CountingIdlingResourceSingleton.increment()
CoroutineScope(Dispatchers.IO).launch {
try {
val items = repository.getSpouts()
if (items.isNotEmpty()) {
val itemsStrings = items.map { it.value.name }
for ((key, value) in items) {
spoutsKV[value.name] = key
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (items.isNotEmpty()) {
val itemsStrings = items.map { it.value.name }
for ((key, value) in items) {
spoutsKV[value.name] = key
}
binding.progress.visibility = View.GONE
binding.formContainer.visibility = View.VISIBLE
val spinnerArrayAdapter =
ArrayAdapter(
this@UpsertSourceActivity,
android.R.layout.simple_spinner_item,
itemsStrings,
)
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.spoutsSpinner.adapter = spinnerArrayAdapter
if (existingSource != null) {
initFields(items)
}
} else {
handleSpoutFailure()
}
binding.progress.visibility = View.GONE
binding.formContainer.visibility = View.VISIBLE
val spinnerArrayAdapter =
ArrayAdapter(
this@UpsertSourceActivity,
android.R.layout.simple_spinner_item,
itemsStrings,
)
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.spoutsSpinner.adapter = spinnerArrayAdapter
if (existingSource != null) {
initFields(items)
}
} 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,16 +186,21 @@ class UpsertSourceActivity :
binding.tags.text.toString(),
)
}
if (successfullyAddedSource) {
finish()
} else {
Toast
.makeText(
this@UpsertSourceActivity,
R.string.cant_create_source,
Toast.LENGTH_SHORT,
).show()
CountingIdlingResourceSingleton.increment()
launch(Dispatchers.Main) {
if (successfullyAddedSource) {
finish()
} else {
Toast
.makeText(
this@UpsertSourceActivity,
R.string.cant_create_source,
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 -> {