From 0f000ea359ac5d9146d7a870971752dc87f60468 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Mon, 26 Sep 2022 15:53:39 +0200 Subject: [PATCH] Only sort if the data is from the DB. This speeds things a little bit. --- .../readerforselfossv2/repository/RepositoryImpl.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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 db9a789..e5dcc2f 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,6 +48,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap suspend fun getNewerItems(): ArrayList { // TODO: Use the updatedSince parameter var fetchedItems: SelfossModel.StatusAndData> = SelfossModel.StatusAndData.error() + var fromDB = false if (isNetworkAvailable()) { fetchedItems = api.getItems( displayedItems.type, @@ -59,6 +60,7 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap ) } else { if (appSettingsService.isItemCachingEnabled()) { + fromDB = true fetchedItems = SelfossModel.StatusAndData.succes( getDBItems().filter { displayedItems == ItemType.ALL || @@ -71,7 +73,9 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap if (fetchedItems.success && fetchedItems.data != null) { items = ArrayList(fetchedItems.data!!) - sortItems() + if (fromDB) { + items.sortByDescending { dateUtils.parseDate(it.datetime) } + } } return items } @@ -92,7 +96,6 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap if (fetchedItems.success && fetchedItems.data != null) { items.addAll(fetchedItems.data!!) - sortItems() } 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 { var success = false if (isNetworkAvailable()) {