chore: new connectivity dep. Closes #84.
All checks were successful
Check PR code / Lint (pull_request) Successful in 1m2s
Check PR code / translations (pull_request) Successful in 1m24s
Check PR code / build (pull_request) Successful in 14m10s

This commit is contained in:
2025-03-10 21:11:39 +01:00
parent ef13e300f0
commit 3bf60f1146
8 changed files with 152 additions and 144 deletions

View File

@ -13,6 +13,7 @@ import bou.amine.apps.readerforselfossv2.model.SelfossModel
import bou.amine.apps.readerforselfossv2.model.StatusAndData
import bou.amine.apps.readerforselfossv2.rest.SelfossApi
import bou.amine.apps.readerforselfossv2.service.AppSettingsService
import bou.amine.apps.readerforselfossv2.service.ConnectivityService
import bou.amine.apps.readerforselfossv2.utils.ItemType
import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded
import bou.amine.apps.readerforselfossv2.utils.toEntity
@ -30,11 +31,10 @@ private const val MAX_ITEMS_NUMBER = 200
class Repository(
private val api: SelfossApi,
private val appSettingsService: AppSettingsService,
val isConnectionAvailable: MutableStateFlow<Boolean>,
private val connectivityService: ConnectivityService,
private val db: ReaderForSelfossDB,
) {
var items = ArrayList<SelfossModel.Item>()
var connectionMonitored = false
var baseUrl = appSettingsService.getBaseUrl()
@ -63,7 +63,7 @@ class Repository(
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
var fetchedItems: StatusAndData<List<SelfossModel.Item>> = StatusAndData.error()
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
fetchedItems =
api.getItems(
displayedItems.type,
@ -102,7 +102,7 @@ class Repository(
suspend fun getOlderItems(): ArrayList<SelfossModel.Item> {
var fetchedItems: StatusAndData<List<SelfossModel.Item>> = StatusAndData.error()
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
val offset = items.size
fetchedItems =
api.getItems(
@ -122,7 +122,7 @@ class Repository(
}
private suspend fun getMaxItemsForBackground(itemType: ItemType): List<SelfossModel.Item> {
return if (isNetworkAvailable()) {
return if (connectivityService.isNetworkAvailable() == true) {
val items =
api.getItems(
itemType.type,
@ -146,7 +146,7 @@ class Repository(
@Suppress("detekt:ForbiddenComment")
suspend fun reloadBadges(): Boolean {
var success = false
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
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<SelfossModel.Tag> {
val isDatabaseEnabled =
appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled()
return if (isNetworkAvailable() && !fetchedTags) {
return if (connectivityService.isNetworkAvailable() == true && !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<String, SelfossModel.Spout> =
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
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 && isNetworkAvailable()) {
if (shouldFetch && connectivityService.isNetworkAvailable() == true) {
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 && isNetworkAvailable()) {
if (shouldFetch && connectivityService.isNetworkAvailable() == true) {
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 (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
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 (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
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 (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
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 (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
api.unstarr(id.toString()).isSuccess
} else {
insertDBAction(id.toString(), starred = true)
@ -309,7 +309,10 @@ class Repository(
suspend fun markAllAsRead(items: ArrayList<SelfossModel.Item>): Boolean {
var success = false
if (isNetworkAvailable() && api.markAllAsRead(items.map { it.id.toString() }).isSuccess) {
if (connectivityService.isNetworkAvailable() != null &&
connectivityService.isNetworkAvailable()!! &&
api.markAllAsRead(items.map { it.id.toString() }).isSuccess
) {
success = true
for (item in items) {
markAsReadLocally(item)
@ -369,7 +372,7 @@ class Repository(
tags: String,
): Boolean {
var response = false
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
response = api
.createSourceForVersion(
title,
@ -390,7 +393,7 @@ class Repository(
tags: String,
): Boolean {
var response = false
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
response = api.updateSourceForVersion(id, title, url, spout, tags).isSuccess == true
}
@ -402,13 +405,13 @@ class Repository(
title: String,
): Boolean {
var success = false
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
val response = api.deleteSource(id)
success = response.isSuccess
}
// We filter on success or if the network isn't available
if (success || !isNetworkAvailable()) {
if (success || !(connectivityService.isNetworkAvailable() == true)) {
items = ArrayList(items.filter { it.sourcetitle != title })
setReaderItems(items)
db.itemsQueries.deleteItemsWhereSource(title)
@ -418,7 +421,7 @@ class Repository(
}
suspend fun updateRemote(): Boolean =
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
api.update().data.equals("finished")
} else {
false
@ -426,7 +429,7 @@ class Repository(
suspend fun login(): Boolean {
var result = false
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
try {
val response = api.login()
result = response.isSuccess == true
@ -439,7 +442,7 @@ class Repository(
suspend fun checkIfFetchFails(): Boolean {
var fetchFailed = true
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
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
@ -453,7 +456,7 @@ class Repository(
}
suspend fun logout() {
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
try {
val response = api.logout()
if (!response.isSuccess) {
@ -481,7 +484,7 @@ class Repository(
suspend fun updateApiInformation() {
val apiMajorVersion = appSettingsService.getApiVersion()
if (isNetworkAvailable()) {
if (connectivityService.isNetworkAvailable() == true) {
val fetchedInformation = api.apiInformation()
if (fetchedInformation.success && fetchedInformation.data != null) {
if (fetchedInformation.data.getApiMajorVersion() != apiMajorVersion) {
@ -500,8 +503,6 @@ class Repository(
}
}
fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride
private fun getDBActions(): List<ACTION> = db.actionsQueries.actions().executeAsList()
private fun deleteDBAction(action: ACTION) = db.actionsQueries.deleteAction(action.id)

View File

@ -0,0 +1,42 @@
package bou.amine.apps.readerforselfossv2.service
import dev.jordond.connectivity.Connectivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
class ConnectivityService {
private val _networkAvailableProvider = MutableSharedFlow<Boolean>()
val networkAvailableProvider = _networkAvailableProvider.asSharedFlow()
private var currentStatus: Boolean? = null
private lateinit var connectivity: Connectivity
fun start() {
connectivity = Connectivity()
connectivity.start()
CoroutineScope(Dispatchers.Main).launch {
connectivity.statusUpdates.collect { status ->
when (status) {
is Connectivity.Status.Connected -> {
currentStatus = true
_networkAvailableProvider.emit(true)
}
is Connectivity.Status.Disconnected -> {
currentStatus = false
_networkAvailableProvider.emit(false)
}
}
}
}
}
fun isNetworkAvailable(): Boolean? = currentStatus
fun stop() {
currentStatus = null
connectivity.stop()
}
}