Perform network connectivity checks in the repository

This commit is contained in:
davidoskky 2022-08-17 15:14:58 +02:00
parent 728eb747c3
commit b7b4ff1485

View File

@ -48,40 +48,65 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> { suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
// TODO: Check connectivity, use the updatedSince parameter // TODO: Use the updatedSince parameter
val fetchedItems = api.getItems(displayedItems.type, if (isConnectionAvailable.value) {
val fetchedItems = api.getItems(
displayedItems.type,
settings.getString("prefer_api_items_number", "200").toInt(), settings.getString("prefer_api_items_number", "200").toInt(),
offset = 0, offset = 0,
tagFilter?.tag, tagFilter?.tag,
sourceFilter?.id?.toLong(), sourceFilter?.id?.toLong(),
searchFilter, searchFilter,
null) null
)
if (fetchedItems != null) { if (fetchedItems != null) {
items = ArrayList(fetchedItems) items = ArrayList(fetchedItems)
} }
} else {
// TODO: Provide an error message if the connection is not available.
// TODO: Get items from the database
}
return items return items
} }
suspend fun getOlderItems(): ArrayList<SelfossModel.Item> { suspend fun getOlderItems(): ArrayList<SelfossModel.Item> {
// TODO: Check connectivity if (isConnectionAvailable.value) {
val offset = items.size val offset = items.size
val fetchedItems = api.getItems(displayedItems.type, val fetchedItems = api.getItems(
displayedItems.type,
settings.getString("prefer_api_items_number", "200").toInt(), settings.getString("prefer_api_items_number", "200").toInt(),
offset, offset,
tagFilter?.tag, tagFilter?.tag,
sourceFilter?.id?.toLong(), sourceFilter?.id?.toLong(),
searchFilter, searchFilter,
null) null
)
if (fetchedItems != null) { if (fetchedItems != null) {
appendItems(fetchedItems) appendItems(fetchedItems)
} }
} else {
// TODO: Provide an error message
}
return items return items
} }
suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>? = suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>? {
api.getItems(itemType.type, 200, 0, tagFilter?.tag, sourceFilter?.id?.toLong(), searchFilter, null) return if (isConnectionAvailable.value) {
api.getItems(
itemType.type,
200,
0,
tagFilter?.tag,
sourceFilter?.id?.toLong(),
searchFilter,
null
)
} else {
null
}
}
private fun appendItems(fetchedItems: List<SelfossModel.Item>) { private fun appendItems(fetchedItems: List<SelfossModel.Item>) {
// TODO: Store in DB if enabled by user // TODO: Store in DB if enabled by user
@ -97,8 +122,8 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
suspend fun reloadBadges(): Boolean { suspend fun reloadBadges(): Boolean {
// TODO: Check connectivity, calculate from DB
var success = false var success = false
if (isConnectionAvailable.value) {
val response = api.stats() val response = api.stats()
if (response != null) { if (response != null) {
badgeUnread = response.unread badgeUnread = response.unread
@ -106,68 +131,99 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
badgeStarred = response.starred badgeStarred = response.starred
success = true success = true
} }
} else {
// TODO: Compute badges from database
}
return success return success
} }
suspend fun getTags(): List<SelfossModel.Tag>? { suspend fun getTags(): List<SelfossModel.Tag>? {
// TODO: Check success, store in DB // TODO: Store in DB
return api.tags() return if (isConnectionAvailable.value) {
api.tags()
} else {
// TODO: Compute from database
null
}
} }
suspend fun getSpouts(): Map<String, SelfossModel.Spout>? { suspend fun getSpouts(): Map<String, SelfossModel.Spout>? {
// TODO: Check success, store in DB // TODO: Store in DB
return api.spouts() return if (isConnectionAvailable.value) {
api.spouts()
} else {
// TODO: Compute from database
null
}
} }
suspend fun getSources(): ArrayList<SelfossModel.Source>? { suspend fun getSources(): ArrayList<SelfossModel.Source>? {
// TODO: Check success // TODO: Store in DB
return api.sources() return if (isConnectionAvailable.value) {
api.sources()
} else {
// TODO: Compute from database
null
}
} }
suspend fun markAsRead(id: Int): Boolean { suspend fun markAsRead(id: Int): Boolean {
// TODO: Check internet connection var success = false
val success = api.markAsRead(id.toString())?.isSuccess == true
if (isConnectionAvailable.value) {
success = api.markAsRead(id.toString())?.isSuccess == true
if (success) { if (success) {
markAsReadLocally(items.first {it.id == id}) markAsReadLocally(items.first { it.id == id })
}
} }
return success return success
} }
suspend fun unmarkAsRead(id: Int): Boolean { suspend fun unmarkAsRead(id: Int): Boolean {
// TODO: Check internet connection var success = false
val success = api.unmarkAsRead(id.toString())?.isSuccess == true
if (isConnectionAvailable.value) {
success = api.unmarkAsRead(id.toString())?.isSuccess == true
if (success) { if (success) {
unmarkAsReadLocally(items.first {it.id == id}) unmarkAsReadLocally(items.first { it.id == id })
}
} }
return success return success
} }
suspend fun starr(id: Int): Boolean { suspend fun starr(id: Int): Boolean {
// TODO: Check success, store in DB var success = false
val success = api.starr(id.toString())?.isSuccess == true
if (isConnectionAvailable.value) {
success = api.starr(id.toString())?.isSuccess == true
if (success) { if (success) {
starrLocally(items.first {it.id == id}) starrLocally(items.first { it.id == id })
}
} }
return success return success
} }
suspend fun unstarr(id: Int): Boolean { suspend fun unstarr(id: Int): Boolean {
// TODO: Check internet connection var success = false
val success = api.unstarr(id.toString())?.isSuccess == true
if (isConnectionAvailable.value) {
success = api.unstarr(id.toString())?.isSuccess == true
if (success) { if (success) {
unstarrLocally(items.first {it.id == id}) unstarrLocally(items.first { it.id == id })
}
} }
return success return success
} }
suspend fun markAllAsRead(ids: List<Int>): Boolean { suspend fun markAllAsRead(ids: List<Int>): 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) { if (success) {
val itemsToMark = items.filter { it.id in ids } val itemsToMark = items.filter { it.id in ids }
@ -175,6 +231,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
markAsReadLocally(item) markAsReadLocally(item)
} }
} }
}
return success return success
} }
@ -217,45 +274,51 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
tags: String, tags: String,
filter: String filter: String
): Boolean { ): Boolean {
// TODO: Check connectivity var response = false
val response = api.createSourceForVersion( if (isConnectionAvailable.value) {
response = api.createSourceForVersion(
title, title,
url, url,
spout, spout,
tags, tags,
filter, filter,
apiMajorVersion apiMajorVersion
) )?.isSuccess == true
}
return response != null return response
} }
suspend fun deleteSource(id: Int): Boolean { suspend fun deleteSource(id: Int): Boolean {
// TODO: Check connectivity, store in DB // TODO: Store in DB
var success = false var success = false
if (isConnectionAvailable.value) {
val response = api.deleteSource(id) val response = api.deleteSource(id)
if (response != null) { if (response != null) {
success = response.isSuccess success = response.isSuccess
} }
}
return success return success
} }
suspend fun updateRemote(): Boolean { suspend fun updateRemote(): Boolean {
// TODO: Handle connectivity issues var response = false
val response = api.update() if (isConnectionAvailable.value) {
return response?.isSuccess ?: false response = api.update()?.isSuccess == true
}
return response
} }
suspend fun login(): Boolean { suspend fun login(): Boolean {
var result = false var result = false
if (isConnectionAvailable.value) {
try { try {
val response = api.login() val response = api.login()
if (response != null && response.isSuccess) { result = response?.isSuccess == true
result = true
}
} catch (cause: Throwable) { } catch (cause: Throwable) {
Napier.e(cause.stackTraceToString(),tag = "RepositoryImpl.updateRemote") Napier.e(cause.stackTraceToString(), tag = "RepositoryImpl.updateRemote")
}
} }
return result return result
} }
@ -274,13 +337,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
} }
private suspend fun updateApiVersion() { private suspend fun updateApiVersion() {
// TODO: Handle connectivity issues apiMajorVersion = settings.getInt("apiVersionMajor", 0)
if (isConnectionAvailable.value) {
val fetchedVersion = api.version() val fetchedVersion = api.version()
if (fetchedVersion != null) { if (fetchedVersion != null) {
apiMajorVersion = fetchedVersion.getApiMajorVersion() apiMajorVersion = fetchedVersion.getApiMajorVersion()
settings.putInt("apiVersionMajor", apiMajorVersion) settings.putInt("apiVersionMajor", apiMajorVersion)
} else { }
apiMajorVersion = settings.getInt("apiVersionMajor", 0)
} }
} }