From e3d35bd65332011b097fc95ffc4f8d929168b940 Mon Sep 17 00:00:00 2001 From: davide Date: Sat, 23 Jul 2022 19:09:43 +0200 Subject: [PATCH] Implement initial Items storage in Repository --- .../repository/Repository.kt | 7 ++- .../repository/RepositoryImpl.kt | 63 +++++++++++++++++-- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt index acfb4f6..51abb07 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt @@ -5,14 +5,15 @@ import bou.amine.apps.readerforselfossv2.rest.SelfossModel interface Repository { // TODO: remove the items variables in favor of storing everything in the database - var items: List - var selectedItems: List + var items: ArrayList + var selectedItems: ArrayList var baseUrl: String // API var apiMajorVersion: Int - fun getMoreItems(): List + suspend fun getNewerItems(): ArrayList + suspend fun getOlderItems(): ArrayList fun stats(): SelfossModel.Stats fun getTags(): List suspend fun getSpouts(): Map? 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 52fcfcd..a4d9030 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 @@ -3,6 +3,7 @@ package bou.amine.apps.readerforselfossv2.repository import bou.amine.apps.readerforselfossv2.rest.SelfossApi import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.service.ApiDetailsService +import bou.amine.apps.readerforselfossv2.utils.DateUtils import com.russhwolf.settings.Settings import io.github.aakira.napier.Napier import kotlinx.coroutines.CoroutineScope @@ -12,10 +13,22 @@ import kotlinx.coroutines.launch class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDetailsService) : Repository { val settings = Settings() - override lateinit var items: List - override lateinit var selectedItems: List + override var items = ArrayList() + get() { return ArrayList(field) } + set(value) { field = ArrayList(value) } + + override var selectedItems = ArrayList() + get() { return ArrayList(field) } + set(value) { field = ArrayList(value) } + override var baseUrl = apiDetails.getBaseUrl() + // TODO: Validate the string in the setter + private var selectedType = "new" + private var selectedTag: SelfossModel.Tag? = null + private var selectedSource: SelfossModel.Source? = null + private var search: String? = null + override var apiMajorVersion = 0 init { @@ -25,8 +38,50 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet } } - override fun getMoreItems(): List { - TODO("Not yet implemented") + override suspend fun getNewerItems(): ArrayList { + // TODO: Check connectivity + val fetchedItems = api.getItems(selectedType, + settings.getInt("prefer_api_items_number", 200), + offset = 0, + selectedTag?.tag, + selectedSource?.id?.toLong(), + search) + + if (fetchedItems != null) { + storeItems(fetchedItems) + } + return selectedItems + } + + override suspend fun getOlderItems(): ArrayList { + // TODO: Check connectivity + val offset = selectedItems.size + val fetchedItems = api.getItems(selectedType, + settings.getInt("prefer_api_items_number", 200), + offset, + selectedTag?.tag, + selectedSource?.id?.toLong(), + search) + + if (fetchedItems != null) { + storeItems(fetchedItems) + } + return selectedItems + } + + private fun storeItems(fetchedItems: List) { + // TODO: Store in DB + val fetchedIDS = fetchedItems.map { it.id } + val tmpItems = ArrayList(items) + tmpItems.removeAll{ it.id in fetchedIDS } + tmpItems.addAll(fetchedItems) + sortItems(tmpItems) + items = tmpItems + } + + private fun sortItems(items: ArrayList) { + val dateUtils = DateUtils(apiMajorVersion) + items.sortBy { dateUtils.parseDate(it.datetime) } } override fun stats(): SelfossModel.Stats {