Use an enum to represent the selected items

This commit is contained in:
davide 2022-08-15 20:42:14 +02:00
parent bb5c521387
commit 29619e1b2b
2 changed files with 18 additions and 27 deletions

View File

@ -90,9 +90,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private val DRAWER_ID_HIDDEN_TAGS = 101100L private val DRAWER_ID_HIDDEN_TAGS = 101100L
private val DRAWER_ID_SOURCES = 100110L private val DRAWER_ID_SOURCES = 100110L
private val DRAWER_ID_FILTERS = 100111L private val DRAWER_ID_FILTERS = 100111L
private val UNREAD_SHOWN = 1
private val READ_SHOWN = 2
private val FAV_SHOWN = 3
private var items: ArrayList<SelfossModel.Item> = ArrayList() private var items: ArrayList<SelfossModel.Item> = ArrayList()
@ -103,7 +100,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private var displayAllCount = false private var displayAllCount = false
private var fullHeightCards: Boolean = false private var fullHeightCards: Boolean = false
private var itemsNumber: Int = 200 private var itemsNumber: Int = 200
private var elementsShown: Int = 1 private var elementsShown: ItemType = ItemType.UNREAD
private var userIdentifier: String = "" private var userIdentifier: String = ""
private var displayAccountHeader: Boolean = false private var displayAccountHeader: Boolean = false
private var infiniteScroll: Boolean = false private var infiniteScroll: Boolean = false
@ -160,7 +157,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
offlineShortcut = intent.getBooleanExtra("startOffline", false) offlineShortcut = intent.getBooleanExtra("startOffline", false)
if (fromTabShortcut) { if (fromTabShortcut) {
elementsShown = intent.getIntExtra("shortcutTab", UNREAD_SHOWN) elementsShown = ItemType.fromInt(intent.getIntExtra("shortcutTab", ItemType.UNREAD.position))
} }
setContentView(view) setContentView(view)
@ -219,7 +216,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
recyclerView: RecyclerView, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder viewHolder: RecyclerView.ViewHolder
): Int = ): Int =
if (elementsShown == FAV_SHOWN) { if (elementsShown == ItemType.STARRED) {
0 0
} else { } else {
super.getSwipeDirs( super.getSwipeDirs(
@ -316,7 +313,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
binding.bottomBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC) binding.bottomBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
if (fromTabShortcut) { if (fromTabShortcut) {
binding.bottomBar.selectTab(elementsShown - 1) binding.bottomBar.selectTab(elementsShown.position - 1)
} }
} }
@ -830,7 +827,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
offset = 0 offset = 0
lastFetchDone = false lastFetchDone = false
elementsShown = position + 1 elementsShown = ItemType.fromInt(position + 1)
getElementsAccordingToTab() getElementsAccordingToTab()
binding.recyclerView.scrollToPosition(0) binding.recyclerView.scrollToPosition(0)
@ -883,16 +880,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun getElementsAccordingToTab( private fun getElementsAccordingToTab(
appendResults: Boolean = false appendResults: Boolean = false
) { ) {
fun doGetAccordingToTab() {
when (elementsShown) {
// TODO: These three functions are not required, one is enough
UNREAD_SHOWN -> getItems(appendResults, ItemType.UNREAD)
READ_SHOWN -> getItems(appendResults, ItemType.ALL)
FAV_SHOWN -> getItems(appendResults, ItemType.STARRED)
else -> getItems(appendResults, ItemType.UNREAD)
}
}
offset = if (appendResults && items.size > 0) { offset = if (appendResults && items.size > 0) {
items.size - 1 items.size - 1
} else { } else {
@ -900,7 +887,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
} }
firstVisible = if (appendResults) firstVisible else 0 firstVisible = if (appendResults) firstVisible else 0
doGetAccordingToTab() getItems(appendResults, elementsShown)
} }
private fun getItems(appendResults: Boolean, itemType: ItemType) { private fun getItems(appendResults: Boolean, itemType: ItemType) {
@ -1085,7 +1072,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
} }
} }
R.id.readAll -> { R.id.readAll -> {
if (elementsShown == UNREAD_SHOWN) { if (elementsShown == ItemType.UNREAD) {
needsConfirmation(R.string.readAll, R.string.markall_dialog_message) { needsConfirmation(R.string.readAll, R.string.markall_dialog_message) {
binding.swipeRefreshLayout.isRefreshing = true binding.swipeRefreshLayout.isRefreshing = true
@ -1127,9 +1114,9 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun maxItemNumber(): Int = private fun maxItemNumber(): Int =
when (elementsShown) { when (elementsShown) {
UNREAD_SHOWN -> repository.badgeUnread ItemType.UNREAD -> repository.badgeUnread
READ_SHOWN -> repository.badgeAll ItemType.ALL -> repository.badgeAll
FAV_SHOWN -> repository.badgeStarred ItemType.STARRED -> repository.badgeStarred
else -> repository.badgeUnread // if !elementsShown then unread are fetched. else -> repository.badgeUnread // if !elementsShown then unread are fetched.
} }

View File

@ -1,7 +1,11 @@
package bou.amine.apps.readerforselfossv2.utils package bou.amine.apps.readerforselfossv2.utils
enum class ItemType(val type: String) { enum class ItemType(val position: Int, val type: String) {
UNREAD("unread"), UNREAD(1, "unread"),
ALL("all"), ALL(2, "all"),
STARRED("starred") STARRED(3, "starred");
companion object {
fun fromInt(value: Int) = values().first { it.position == value }
}
} }