Use an enum to represent item type

This commit is contained in:
davide 2022-08-15 15:16:57 +02:00
parent fe59177efb
commit d47cca2f5a
5 changed files with 21 additions and 19 deletions

View File

@ -49,6 +49,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.persistence.toEntity
import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView import bou.amine.apps.readerforselfossv2.android.utils.persistence.toView
import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.utils.ItemType
import bou.amine.apps.readerforselfossv2.utils.longHash import bou.amine.apps.readerforselfossv2.utils.longHash
import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem import com.ashokvarma.bottomnavigation.BottomNavigationItem
@ -905,7 +906,7 @@ 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
repository.displayedItems = "unread" repository.displayedItems = ItemType.UNREAD
items = if (appendResults) { items = if (appendResults) {
repository.getNewerItems() repository.getNewerItems()
} else { } else {
@ -919,7 +920,7 @@ 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
repository.displayedItems = "all" repository.displayedItems = ItemType.ALL
items = if (appendResults) { items = if (appendResults) {
repository.getNewerItems() repository.getNewerItems()
} else { } else {
@ -933,7 +934,7 @@ 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
repository.displayedItems = "starred" repository.displayedItems = ItemType.STARRED
items = if (appendResults) { items = if (appendResults) {
repository.getNewerItems() repository.getNewerItems()
} else { } else {

View File

@ -10,6 +10,7 @@ import bou.amine.apps.readerforselfossv2.android.themes.AppColors
import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.Config
import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.repository.Repository
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.utils.ItemType
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -81,7 +82,7 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
repository.markAsRead(i.id) repository.markAsRead(i.id)
} }
if (repository.displayedItems == "unread") { if (repository.displayedItems == ItemType.UNREAD) {
items.remove(i) items.remove(i)
notifyItemRemoved(position) notifyItemRemoved(position)
updateItems(items) updateItems(items)

View File

@ -2,6 +2,7 @@ package bou.amine.apps.readerforselfossv2.repository
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.DateUtils
import bou.amine.apps.readerforselfossv2.utils.ItemType
interface Repository { interface Repository {
@ -10,7 +11,7 @@ interface Repository {
var baseUrl: String var baseUrl: String
var dateUtils: DateUtils var dateUtils: DateUtils
var displayedItems: String var displayedItems: ItemType
// Settings // Settings
var itemsCaching: Boolean var itemsCaching: Boolean

View File

@ -4,6 +4,7 @@ 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 bou.amine.apps.readerforselfossv2.utils.DateUtils
import bou.amine.apps.readerforselfossv2.utils.ItemType
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
@ -18,16 +19,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
override var baseUrl = apiDetails.getBaseUrl() override var baseUrl = apiDetails.getBaseUrl()
override lateinit var dateUtils: DateUtils override lateinit var dateUtils: DateUtils
override var displayedItems = "unread" override var displayedItems = ItemType.UNREAD
set(value) {
field = when (value) {
"all" -> "all"
"unread" -> "unread"
"read" -> "read"
"starred" -> "starred"
else -> "all"
}
}
override var tagFilter: SelfossModel.Tag? = null override var tagFilter: SelfossModel.Tag? = null
override var sourceFilter: SelfossModel.Source? = null override var sourceFilter: SelfossModel.Source? = null
@ -54,7 +46,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(displayedItems, val fetchedItems = api.getItems(displayedItems.type,
settings.getString("prefer_api_items_number", "200").toInt(), settings.getString("prefer_api_items_number", "200").toInt(),
offset = 0, offset = 0,
tagFilter?.tag, tagFilter?.tag,
@ -70,7 +62,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
override suspend fun getOlderItems(): ArrayList<SelfossModel.Item> { override suspend fun getOlderItems(): ArrayList<SelfossModel.Item> {
// TODO: Check connectivity // TODO: Check connectivity
val offset = items.size val offset = items.size
val fetchedItems = api.getItems(displayedItems, val fetchedItems = api.getItems(displayedItems.type,
settings.getString("prefer_api_items_number", "200").toInt(), settings.getString("prefer_api_items_number", "200").toInt(),
offset, offset,
tagFilter?.tag, tagFilter?.tag,
@ -99,9 +91,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
private fun filterSelectedItems(items: ArrayList<SelfossModel.Item>): ArrayList<SelfossModel.Item> { private fun filterSelectedItems(items: ArrayList<SelfossModel.Item>): ArrayList<SelfossModel.Item> {
val tmpItems = items val tmpItems = items
if (displayedItems == "unread") { if (displayedItems == ItemType.UNREAD) {
tmpItems.removeAll { !it.unread } tmpItems.removeAll { !it.unread }
} else if (displayedItems == "starred") { } else if (displayedItems == ItemType.STARRED) {
tmpItems.removeAll { !it.starred } tmpItems.removeAll { !it.starred }
} }

View File

@ -0,0 +1,7 @@
package bou.amine.apps.readerforselfossv2.utils
enum class ItemType(val type: String) {
UNREAD("unread"),
ALL("all"),
STARRED("starred")
}