diff --git a/androidApp/src/androidTest/kotlin/bou/amine/apps/readerforselfossv2/android/1-LoginActivityTest.kt b/androidApp/src/androidTest/kotlin/bou/amine/apps/readerforselfossv2/android/1-LoginActivityTest.kt index 5352be0..2dea4a4 100644 --- a/androidApp/src/androidTest/kotlin/bou/amine/apps/readerforselfossv2/android/1-LoginActivityTest.kt +++ b/androidApp/src/androidTest/kotlin/bou/amine/apps/readerforselfossv2/android/1-LoginActivityTest.kt @@ -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))) } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index c31c857..77df999 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -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() } @@ -639,30 +643,33 @@ class HomeActivity : R.id.readAll -> { if (elementsShown == ItemType.UNREAD) { needsConfirmation(R.string.readAll, R.string.markall_dialog_message) { - binding.swipeRefreshLayout.isRefreshing = true CountingIdlingResourceSingleton.increment() - CoroutineScope(Dispatchers.Main).launch { + binding.swipeRefreshLayout.isRefreshing = true + 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() } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt index 7aa4a47..1564c63 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt @@ -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() } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt index ee3b85c..b9bb9f2 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt @@ -73,7 +73,7 @@ class MyApp : ), ) - CoroutineScope(Dispatchers.Main).launch { + CoroutineScope(Dispatchers.Default).launch { connectivityService.networkAvailableProvider.collect { networkAvailable -> val toastMessage = if (networkAvailable) { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt index 35677d2..650e6dc 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt @@ -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 { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt index 70745e3..a2c8926 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/SourcesActivity.kt @@ -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() } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/UpsertSourceActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/UpsertSourceActivity.kt index f952bdb..690eab6 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/UpsertSourceActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/UpsertSourceActivity.kt @@ -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() } } } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt index d373968..753c7f7 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt @@ -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) } } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt index 9bab495..ab212a6 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt @@ -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") diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/service/ConnectivityService.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/service/ConnectivityService.kt index 4ade188..d83ae51 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/service/ConnectivityService.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/service/ConnectivityService.kt @@ -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 -> {