Implement initial Items storage in Repository
This commit is contained in:
parent
c0137ea5e7
commit
e3d35bd653
@ -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<SelfossModel.Item>
|
||||
var selectedItems: List<SelfossModel.Item>
|
||||
var items: ArrayList<SelfossModel.Item>
|
||||
var selectedItems: ArrayList<SelfossModel.Item>
|
||||
var baseUrl: String
|
||||
|
||||
// API
|
||||
var apiMajorVersion: Int
|
||||
|
||||
fun getMoreItems(): List<SelfossModel.Item>
|
||||
suspend fun getNewerItems(): ArrayList<SelfossModel.Item>
|
||||
suspend fun getOlderItems(): ArrayList<SelfossModel.Item>
|
||||
fun stats(): SelfossModel.Stats
|
||||
fun getTags(): List<SelfossModel.Tag>
|
||||
suspend fun getSpouts(): Map<String, SelfossModel.Spout>?
|
||||
|
@ -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<SelfossModel.Item>
|
||||
override lateinit var selectedItems: List<SelfossModel.Item>
|
||||
override var items = ArrayList<SelfossModel.Item>()
|
||||
get() { return ArrayList(field) }
|
||||
set(value) { field = ArrayList(value) }
|
||||
|
||||
override var selectedItems = ArrayList<SelfossModel.Item>()
|
||||
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<SelfossModel.Item> {
|
||||
TODO("Not yet implemented")
|
||||
override suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
|
||||
// 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<SelfossModel.Item> {
|
||||
// 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<SelfossModel.Item>) {
|
||||
// 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<SelfossModel.Item>) {
|
||||
val dateUtils = DateUtils(apiMajorVersion)
|
||||
items.sortBy { dateUtils.parseDate(it.datetime) }
|
||||
}
|
||||
|
||||
override fun stats(): SelfossModel.Stats {
|
||||
|
Loading…
Reference in New Issue
Block a user