diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index a00081b..9a409cf 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -48,40 +48,65 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } suspend fun getNewerItems(): ArrayList { - // TODO: Check connectivity, use the updatedSince parameter - val fetchedItems = api.getItems(displayedItems.type, - settings.getString("prefer_api_items_number", "200").toInt(), - offset = 0, - tagFilter?.tag, - sourceFilter?.id?.toLong(), - searchFilter, - null) + // TODO: Use the updatedSince parameter + if (isConnectionAvailable.value) { + val fetchedItems = api.getItems( + displayedItems.type, + settings.getString("prefer_api_items_number", "200").toInt(), + offset = 0, + tagFilter?.tag, + sourceFilter?.id?.toLong(), + searchFilter, + null + ) - if (fetchedItems != null) { - items = ArrayList(fetchedItems) + if (fetchedItems != null) { + items = ArrayList(fetchedItems) + } + } else { + // TODO: Provide an error message if the connection is not available. + // TODO: Get items from the database } return items } suspend fun getOlderItems(): ArrayList { - // TODO: Check connectivity - val offset = items.size - val fetchedItems = api.getItems(displayedItems.type, - settings.getString("prefer_api_items_number", "200").toInt(), - offset, - tagFilter?.tag, - sourceFilter?.id?.toLong(), - searchFilter, - null) + if (isConnectionAvailable.value) { + val offset = items.size + val fetchedItems = api.getItems( + displayedItems.type, + settings.getString("prefer_api_items_number", "200").toInt(), + offset, + tagFilter?.tag, + sourceFilter?.id?.toLong(), + searchFilter, + null + ) - if (fetchedItems != null) { - appendItems(fetchedItems) + if (fetchedItems != null) { + appendItems(fetchedItems) + } + } else { + // TODO: Provide an error message } return items } - suspend fun allItems(itemType: ItemType): List? = - api.getItems(itemType.type, 200, 0, tagFilter?.tag, sourceFilter?.id?.toLong(), searchFilter, null) + suspend fun allItems(itemType: ItemType): List? { + return if (isConnectionAvailable.value) { + api.getItems( + itemType.type, + 200, + 0, + tagFilter?.tag, + sourceFilter?.id?.toLong(), + searchFilter, + null + ) + } else { + null + } + } private fun appendItems(fetchedItems: List) { // TODO: Store in DB if enabled by user @@ -97,82 +122,114 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } suspend fun reloadBadges(): Boolean { - // TODO: Check connectivity, calculate from DB var success = false - val response = api.stats() - if (response != null) { - badgeUnread = response.unread - badgeAll = response.total - badgeStarred = response.starred - success = true + if (isConnectionAvailable.value) { + val response = api.stats() + if (response != null) { + badgeUnread = response.unread + badgeAll = response.total + badgeStarred = response.starred + success = true + } + } else { + // TODO: Compute badges from database } return success } suspend fun getTags(): List? { - // TODO: Check success, store in DB - return api.tags() + // TODO: Store in DB + return if (isConnectionAvailable.value) { + api.tags() + } else { + // TODO: Compute from database + null + } } suspend fun getSpouts(): Map? { - // TODO: Check success, store in DB - return api.spouts() + // TODO: Store in DB + return if (isConnectionAvailable.value) { + api.spouts() + } else { + // TODO: Compute from database + null + } } suspend fun getSources(): ArrayList? { - // TODO: Check success - return api.sources() + // TODO: Store in DB + return if (isConnectionAvailable.value) { + api.sources() + } else { + // TODO: Compute from database + null + } } suspend fun markAsRead(id: Int): Boolean { - // TODO: Check internet connection - val success = api.markAsRead(id.toString())?.isSuccess == true + var success = false - if (success) { - markAsReadLocally(items.first {it.id == id}) + if (isConnectionAvailable.value) { + success = api.markAsRead(id.toString())?.isSuccess == true + + if (success) { + markAsReadLocally(items.first { it.id == id }) + } } return success } suspend fun unmarkAsRead(id: Int): Boolean { - // TODO: Check internet connection - val success = api.unmarkAsRead(id.toString())?.isSuccess == true + var success = false - if (success) { - unmarkAsReadLocally(items.first {it.id == id}) + if (isConnectionAvailable.value) { + success = api.unmarkAsRead(id.toString())?.isSuccess == true + + if (success) { + unmarkAsReadLocally(items.first { it.id == id }) + } } return success } suspend fun starr(id: Int): Boolean { - // TODO: Check success, store in DB - val success = api.starr(id.toString())?.isSuccess == true + var success = false - if (success) { - starrLocally(items.first {it.id == id}) + if (isConnectionAvailable.value) { + success = api.starr(id.toString())?.isSuccess == true + + if (success) { + starrLocally(items.first { it.id == id }) + } } return success } suspend fun unstarr(id: Int): Boolean { - // TODO: Check internet connection - val success = api.unstarr(id.toString())?.isSuccess == true + var success = false - if (success) { - unstarrLocally(items.first {it.id == id}) + if (isConnectionAvailable.value) { + success = api.unstarr(id.toString())?.isSuccess == true + + if (success) { + unstarrLocally(items.first { it.id == id }) + } } return success } suspend fun markAllAsRead(ids: List): Boolean { - // TODO: Check Internet connectivity, store in DB + var success = false - val success = api.markAllAsRead(ids.map { it.toString() })?.isSuccess == true + if (isConnectionAvailable.value) { + success = api.markAllAsRead(ids.map { it.toString() })?.isSuccess == true - if (success) { - val itemsToMark = items.filter { it.id in ids } - for (item in itemsToMark) { - markAsReadLocally(item) + if (success) { + val itemsToMark = items.filter { it.id in ids } + for (item in itemsToMark) { + markAsReadLocally(item) + } } } return success @@ -217,45 +274,51 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails tags: String, filter: String ): Boolean { - // TODO: Check connectivity - val response = api.createSourceForVersion( - title, - url, - spout, - tags, - filter, - apiMajorVersion - ) + var response = false + if (isConnectionAvailable.value) { + response = api.createSourceForVersion( + title, + url, + spout, + tags, + filter, + apiMajorVersion + )?.isSuccess == true + } - return response != null + return response } suspend fun deleteSource(id: Int): Boolean { - // TODO: Check connectivity, store in DB + // TODO: Store in DB var success = false - val response = api.deleteSource(id) - if (response != null) { - success = response.isSuccess + if (isConnectionAvailable.value) { + val response = api.deleteSource(id) + if (response != null) { + success = response.isSuccess + } } return success } suspend fun updateRemote(): Boolean { - // TODO: Handle connectivity issues - val response = api.update() - return response?.isSuccess ?: false + var response = false + if (isConnectionAvailable.value) { + response = api.update()?.isSuccess == true + } + return response } suspend fun login(): Boolean { var result = false - try { - val response = api.login() - if (response != null && response.isSuccess) { - result = true + if (isConnectionAvailable.value) { + try { + val response = api.login() + result = response?.isSuccess == true + } catch (cause: Throwable) { + Napier.e(cause.stackTraceToString(), tag = "RepositoryImpl.updateRemote") } - } catch (cause: Throwable) { - Napier.e(cause.stackTraceToString(),tag = "RepositoryImpl.updateRemote") } return result } @@ -274,13 +337,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } private suspend fun updateApiVersion() { - // TODO: Handle connectivity issues - val fetchedVersion = api.version() - if (fetchedVersion != null) { - apiMajorVersion = fetchedVersion.getApiMajorVersion() - settings.putInt("apiVersionMajor", apiMajorVersion) - } else { - apiMajorVersion = settings.getInt("apiVersionMajor", 0) + apiMajorVersion = settings.getInt("apiVersionMajor", 0) + + if (isConnectionAvailable.value) { + val fetchedVersion = api.version() + if (fetchedVersion != null) { + apiMajorVersion = fetchedVersion.getApiMajorVersion() + settings.putInt("apiVersionMajor", apiMajorVersion) + } } }