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 {
|
interface Repository {
|
||||||
|
|
||||||
// TODO: remove the items variables in favor of storing everything in the database
|
// TODO: remove the items variables in favor of storing everything in the database
|
||||||
var items: List<SelfossModel.Item>
|
var items: ArrayList<SelfossModel.Item>
|
||||||
var selectedItems: List<SelfossModel.Item>
|
var selectedItems: ArrayList<SelfossModel.Item>
|
||||||
var baseUrl: String
|
var baseUrl: String
|
||||||
|
|
||||||
// API
|
// API
|
||||||
var apiMajorVersion: Int
|
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 stats(): SelfossModel.Stats
|
||||||
fun getTags(): List<SelfossModel.Tag>
|
fun getTags(): List<SelfossModel.Tag>
|
||||||
suspend fun getSpouts(): Map<String, SelfossModel.Spout>?
|
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.SelfossApi
|
||||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||||
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
|
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
|
||||||
|
import bou.amine.apps.readerforselfossv2.utils.DateUtils
|
||||||
import com.russhwolf.settings.Settings
|
import com.russhwolf.settings.Settings
|
||||||
import io.github.aakira.napier.Napier
|
import io.github.aakira.napier.Napier
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@ -12,10 +13,22 @@ import kotlinx.coroutines.launch
|
|||||||
class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDetailsService) : Repository {
|
class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDetailsService) : Repository {
|
||||||
val settings = Settings()
|
val settings = Settings()
|
||||||
|
|
||||||
override lateinit var items: List<SelfossModel.Item>
|
override var items = ArrayList<SelfossModel.Item>()
|
||||||
override lateinit var selectedItems: List<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()
|
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
|
override var apiMajorVersion = 0
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@ -25,8 +38,50 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getMoreItems(): List<SelfossModel.Item> {
|
override suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
|
||||||
TODO("Not yet implemented")
|
// 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 {
|
override fun stats(): SelfossModel.Stats {
|
||||||
|
Loading…
Reference in New Issue
Block a user