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

View File

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

View File

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