Fetch items through the repository

Items are stored and filtered locally in the repository
This commit is contained in:
davide 2022-07-24 03:32:58 +02:00
parent e3d35bd653
commit e37eae8d16
3 changed files with 36 additions and 18 deletions

View File

@ -937,6 +937,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
) {
fun doGetAccordingToTab() {
when (elementsShown) {
// TODO: These three functions are not required, one is enough
UNREAD_SHOWN -> getUnRead(appendResults)
READ_SHOWN -> getRead(appendResults)
FAV_SHOWN -> getStarred(appendResults)
@ -957,11 +958,11 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun getUnRead(appendResults: Boolean = false) {
CoroutineScope(Dispatchers.Main).launch {
binding.swipeRefreshLayout.isRefreshing = true
val apiItems = service.getUnreadItems(itemsNumber, offset, applicationContext.isNetworkAvailable())
if (appendResults) {
apiItems?.let { items.addAll(it) }
repository.selectedType = "unread"
items = if (appendResults) {
repository.getNewerItems()
} else {
items = apiItems.orEmpty() as ArrayList<SelfossModel.Item>
repository.getOlderItems()
}
binding.swipeRefreshLayout.isRefreshing = false
handleListResult()
@ -971,11 +972,11 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun getRead(appendResults: Boolean = false) {
CoroutineScope(Dispatchers.Main).launch {
binding.swipeRefreshLayout.isRefreshing = true
val apiItems = service.getReadItems(itemsNumber, offset, applicationContext.isNetworkAvailable())
if (appendResults) {
apiItems?.let { items.addAll(it) }
repository.selectedType = "all"
items = if (appendResults) {
repository.getNewerItems()
} else {
items = apiItems.orEmpty() as ArrayList<SelfossModel.Item>
repository.getOlderItems()
}
binding.swipeRefreshLayout.isRefreshing = false
handleListResult()
@ -985,11 +986,11 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun getStarred(appendResults: Boolean = false) {
CoroutineScope(Dispatchers.Main).launch {
binding.swipeRefreshLayout.isRefreshing = true
val apiItems = service.getStarredItems(itemsNumber, offset, applicationContext.isNetworkAvailable())
if (appendResults) {
apiItems?.let { items.addAll(it) }
repository.selectedType = "starred"
items = if (appendResults) {
repository.getNewerItems()
} else {
items = apiItems.orEmpty() as ArrayList<SelfossModel.Item>
repository.getOlderItems()
}
binding.swipeRefreshLayout.isRefreshing = false
handleListResult()

View File

@ -9,6 +9,8 @@ interface Repository {
var selectedItems: ArrayList<SelfossModel.Item>
var baseUrl: String
var selectedType: String
// API
var apiMajorVersion: Int

View File

@ -24,7 +24,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
override var baseUrl = apiDetails.getBaseUrl()
// TODO: Validate the string in the setter
private var selectedType = "new"
override var selectedType = "read"
private var selectedTag: SelfossModel.Tag? = null
private var selectedSource: SelfossModel.Source? = null
private var search: String? = null
@ -41,7 +41,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
override suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
// TODO: Check connectivity
val fetchedItems = api.getItems(selectedType,
settings.getInt("prefer_api_items_number", 200),
settings.getString("prefer_api_items_number", "200").toInt(),
offset = 0,
selectedTag?.tag,
selectedSource?.id?.toLong(),
@ -50,14 +50,14 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
if (fetchedItems != null) {
storeItems(fetchedItems)
}
return selectedItems
return filterSelectedItems(items)
}
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),
settings.getString("prefer_api_items_number", "200").toInt(),
offset,
selectedTag?.tag,
selectedSource?.id?.toLong(),
@ -66,7 +66,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
if (fetchedItems != null) {
storeItems(fetchedItems)
}
return selectedItems
return filterSelectedItems(items)
}
private fun storeItems(fetchedItems: List<SelfossModel.Item>) {
@ -81,7 +81,22 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
private fun sortItems(items: ArrayList<SelfossModel.Item>) {
val dateUtils = DateUtils(apiMajorVersion)
items.sortBy { dateUtils.parseDate(it.datetime) }
items.sortByDescending { dateUtils.parseDate(it.datetime) }
}
private fun filterSelectedItems(items: ArrayList<SelfossModel.Item>): ArrayList<SelfossModel.Item> {
val tmpItems = ArrayList(items)
if (selectedType == "unread") {
tmpItems.removeAll { !it.unread }
} else if (selectedType == "starred") {
tmpItems.removeAll { !it.starred }
}
if (selectedTag != null) {
tmpItems.removeAll { !it.tags.contains(selectedTag!!.tag) }
}
return tmpItems
}
override fun stats(): SelfossModel.Stats {