diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index cce872a..671c20b 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -170,7 +170,7 @@ class ArticleFragment : private fun handleContent() { if (contentText.isEmptyOrNullOrNullString()) { - if (connectivityService.isNetworkAvailable() == true && url.isUrlValid()) { + if (connectivityService.isNetworkAvailable() && url.isUrlValid()) { getContentFromMercury(url!!) } } else { diff --git a/androidApp/src/test/kotlin/bou/amine/apps/readerforselfossv2/tests/repository/RepositoryTest.kt b/androidApp/src/test/kotlin/bou/amine/apps/readerforselfossv2/tests/repository/RepositoryTest.kt index 29d68c7..f68d441 100644 --- a/androidApp/src/test/kotlin/bou/amine/apps/readerforselfossv2/tests/repository/RepositoryTest.kt +++ b/androidApp/src/test/kotlin/bou/amine/apps/readerforselfossv2/tests/repository/RepositoryTest.kt @@ -55,7 +55,7 @@ class RepositoryTest { private val connectivityService = mockk() private lateinit var repository: Repository - private fun initializeRepository(isNetworkAvailable: Boolean? = true) { + private fun initializeRepository(isNetworkAvailable: Boolean = true) { every { connectivityService.isNetworkAvailable() } returns isNetworkAvailable repository = Repository(api, appSettingsService, connectivityService, db) 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 ce717ff..45a2272 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 @@ -63,7 +63,7 @@ class Repository( suspend fun getNewerItems(): ArrayList { var fetchedItems: StatusAndData> = StatusAndData.error() - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { fetchedItems = api.getItems( displayedItems.type, @@ -102,7 +102,7 @@ class Repository( suspend fun getOlderItems(): ArrayList { var fetchedItems: StatusAndData> = StatusAndData.error() - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { val offset = items.size fetchedItems = api.getItems( @@ -122,7 +122,7 @@ class Repository( } private suspend fun getMaxItemsForBackground(itemType: ItemType): List { - return if (connectivityService.isNetworkAvailable() == true) { + return if (connectivityService.isNetworkAvailable()) { val items = api.getItems( itemType.type, @@ -146,7 +146,7 @@ class Repository( @Suppress("detekt:ForbiddenComment") suspend fun reloadBadges(): Boolean { var success = false - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { val response = api.stats() if (response.success && response.data != null) { _badgeUnread.value = response.data.unread ?: 0 @@ -168,7 +168,7 @@ class Repository( suspend fun getTags(): List { val isDatabaseEnabled = appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() - return if (connectivityService.isNetworkAvailable() == true && !fetchedTags) { + return if (connectivityService.isNetworkAvailable() && !fetchedTags) { val apiTags = api.tags() if (apiTags.success && apiTags.data != null && isDatabaseEnabled) { resetDBTagsWithData(apiTags.data) @@ -185,7 +185,7 @@ class Repository( } suspend fun getSpouts(): Map = - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { val spouts = api.spouts() if (spouts.success && spouts.data != null) { spouts.data @@ -201,7 +201,7 @@ class Repository( val isDatabaseEnabled = appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() val shouldFetch = if (!appSettingsService.isUpdateSourcesEnabled()) !fetchedSources else true - if (shouldFetch && connectivityService.isNetworkAvailable() == true) { + if (shouldFetch && connectivityService.isNetworkAvailable()) { if (appSettingsService.getPublicAccess()) { val apiSources = api.sourcesStats() if (apiSources.success && apiSources.data != null) { @@ -223,7 +223,7 @@ class Repository( val isDatabaseEnabled = appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled() val shouldFetch = if (!appSettingsService.isUpdateSourcesEnabled()) !fetchedSources else true - if (shouldFetch && connectivityService.isNetworkAvailable() == true) { + if (shouldFetch && connectivityService.isNetworkAvailable()) { val apiSources = api.sourcesDetailed() if (apiSources.success && apiSources.data != null) { fetchedSources = true @@ -248,7 +248,7 @@ class Repository( } private suspend fun markAsReadById(id: Int): Boolean = - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { api.markAsRead(id.toString()).isSuccess } else { insertDBAction(id.toString(), read = true) @@ -265,7 +265,7 @@ class Repository( } private suspend fun unmarkAsReadById(id: Int): Boolean = - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { api.unmarkAsRead(id.toString()).isSuccess } else { insertDBAction(id.toString(), unread = true) @@ -282,7 +282,7 @@ class Repository( } private suspend fun starrById(id: Int): Boolean = - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { api.starr(id.toString()).isSuccess } else { insertDBAction(id.toString(), starred = true) @@ -299,7 +299,7 @@ class Repository( } private suspend fun unstarrById(id: Int): Boolean = - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { api.unstarr(id.toString()).isSuccess } else { insertDBAction(id.toString(), starred = true) @@ -309,9 +309,7 @@ class Repository( suspend fun markAllAsRead(items: ArrayList): Boolean { var success = false - if (connectivityService.isNetworkAvailable() != null && - connectivityService.isNetworkAvailable()!! && - api.markAllAsRead(items.map { it.id.toString() }).isSuccess + if (connectivityService.isNetworkAvailable() && api.markAllAsRead(items.map { it.id.toString() }).isSuccess ) { success = true for (item in items) { @@ -372,7 +370,7 @@ class Repository( tags: String, ): Boolean { var response = false - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { response = api .createSourceForVersion( title, @@ -393,7 +391,7 @@ class Repository( tags: String, ): Boolean { var response = false - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { response = api.updateSourceForVersion(id, title, url, spout, tags).isSuccess == true } @@ -405,13 +403,13 @@ class Repository( title: String, ): Boolean { var success = false - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { val response = api.deleteSource(id) success = response.isSuccess } // We filter on success or if the network isn't available - if (success || !(connectivityService.isNetworkAvailable() == true)) { + if (success || !connectivityService.isNetworkAvailable()) { items = ArrayList(items.filter { it.sourcetitle != title }) setReaderItems(items) db.itemsQueries.deleteItemsWhereSource(title) @@ -421,7 +419,7 @@ class Repository( } suspend fun updateRemote(): Boolean = - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { api.update().data.equals("finished") } else { false @@ -429,7 +427,7 @@ class Repository( suspend fun login(): Boolean { var result = false - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { try { val response = api.login() result = response.isSuccess == true @@ -442,7 +440,7 @@ class Repository( suspend fun checkIfFetchFails(): Boolean { var fetchFailed = true - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { try { // Trying to fetch one item, and check someone is trying to use the app with // a random rss feed, that would throw a NoTransformationFoundException @@ -456,7 +454,7 @@ class Repository( } suspend fun logout() { - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { try { val response = api.logout() if (!response.isSuccess) { @@ -484,7 +482,7 @@ class Repository( suspend fun updateApiInformation() { val apiMajorVersion = appSettingsService.getApiVersion() - if (connectivityService.isNetworkAvailable() == true) { + if (connectivityService.isNetworkAvailable()) { val fetchedInformation = api.apiInformation() if (fetchedInformation.success && fetchedInformation.data != null) { if (fetchedInformation.data.getApiMajorVersion() != apiMajorVersion) { 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 4f067bc..4ade188 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 @@ -10,7 +10,7 @@ import kotlinx.coroutines.launch class ConnectivityService { private val _networkAvailableProvider = MutableSharedFlow() val networkAvailableProvider = _networkAvailableProvider.asSharedFlow() - private var currentStatus: Boolean? = null + private var currentStatus = true private lateinit var connectivity: Connectivity fun start() { @@ -20,23 +20,27 @@ class ConnectivityService { connectivity.statusUpdates.collect { status -> when (status) { is Connectivity.Status.Connected -> { - currentStatus = true - _networkAvailableProvider.emit(true) + if (!currentStatus) { + currentStatus = true + _networkAvailableProvider.emit(true) + } } is Connectivity.Status.Disconnected -> { - currentStatus = false - _networkAvailableProvider.emit(false) + if (currentStatus) { + currentStatus = false + _networkAvailableProvider.emit(false) + } } } } } } - fun isNetworkAvailable(): Boolean? = currentStatus + fun isNetworkAvailable(): Boolean = currentStatus fun stop() { - currentStatus = null + currentStatus = true connectivity.stop() } }