Only sort if the data is from the DB. This speeds things a little bit.

This commit is contained in:
aminecmi 2022-09-26 15:53:39 +02:00
parent fb8f81a4c8
commit 0f000ea359

View File

@ -48,6 +48,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> { suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
// TODO: Use the updatedSince parameter // TODO: Use the updatedSince parameter
var fetchedItems: SelfossModel.StatusAndData<List<SelfossModel.Item>> = SelfossModel.StatusAndData.error() var fetchedItems: SelfossModel.StatusAndData<List<SelfossModel.Item>> = SelfossModel.StatusAndData.error()
var fromDB = false
if (isNetworkAvailable()) { if (isNetworkAvailable()) {
fetchedItems = api.getItems( fetchedItems = api.getItems(
displayedItems.type, displayedItems.type,
@ -59,6 +60,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
) )
} else { } else {
if (appSettingsService.isItemCachingEnabled()) { if (appSettingsService.isItemCachingEnabled()) {
fromDB = true
fetchedItems = SelfossModel.StatusAndData.succes( fetchedItems = SelfossModel.StatusAndData.succes(
getDBItems().filter { getDBItems().filter {
displayedItems == ItemType.ALL || displayedItems == ItemType.ALL ||
@ -71,7 +73,9 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
if (fetchedItems.success && fetchedItems.data != null) { if (fetchedItems.success && fetchedItems.data != null) {
items = ArrayList(fetchedItems.data!!) items = ArrayList(fetchedItems.data!!)
sortItems() if (fromDB) {
items.sortByDescending { dateUtils.parseDate(it.datetime) }
}
} }
return items return items
} }
@ -92,7 +96,6 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
if (fetchedItems.success && fetchedItems.data != null) { if (fetchedItems.success && fetchedItems.data != null) {
items.addAll(fetchedItems.data!!) items.addAll(fetchedItems.data!!)
sortItems()
} }
return items return items
} }
@ -118,10 +121,6 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
} }
} }
private fun sortItems() {
items.sortByDescending { dateUtils.parseDate(it.datetime) }
}
suspend fun reloadBadges(): Boolean { suspend fun reloadBadges(): Boolean {
var success = false var success = false
if (isNetworkAvailable()) { if (isNetworkAvailable()) {