From d47cca2f5a1e2e99185cbd2bb9b8ffc45b745986 Mon Sep 17 00:00:00 2001 From: davide Date: Mon, 15 Aug 2022 15:16:57 +0200 Subject: [PATCH] Use an enum to represent item type --- .../android/HomeActivity.kt | 7 ++++--- .../android/adapters/ItemsAdapter.kt | 3 ++- .../repository/Repository.kt | 3 ++- .../repository/RepositoryImpl.kt | 20 ++++++------------- .../apps/readerforselfossv2/utils/Enums.kt | 7 +++++++ 5 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index 2b68e9b..39bf335 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -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.repository.Repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.utils.ItemType import bou.amine.apps.readerforselfossv2.utils.longHash import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationItem @@ -905,7 +906,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun getUnRead(appendResults: Boolean = false) { CoroutineScope(Dispatchers.Main).launch { binding.swipeRefreshLayout.isRefreshing = true - repository.displayedItems = "unread" + repository.displayedItems = ItemType.UNREAD items = if (appendResults) { repository.getNewerItems() } else { @@ -919,7 +920,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun getRead(appendResults: Boolean = false) { CoroutineScope(Dispatchers.Main).launch { binding.swipeRefreshLayout.isRefreshing = true - repository.displayedItems = "all" + repository.displayedItems = ItemType.ALL items = if (appendResults) { repository.getNewerItems() } else { @@ -933,7 +934,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun getStarred(appendResults: Boolean = false) { CoroutineScope(Dispatchers.Main).launch { binding.swipeRefreshLayout.isRefreshing = true - repository.displayedItems = "starred" + repository.displayedItems = ItemType.STARRED items = if (appendResults) { repository.getNewerItems() } else { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt index 5ba7463..ff24461 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemsAdapter.kt @@ -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.repository.Repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel +import bou.amine.apps.readerforselfossv2.utils.ItemType import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -81,7 +82,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte CoroutineScope(Dispatchers.IO).launch { repository.markAsRead(i.id) } - if (repository.displayedItems == "unread") { + if (repository.displayedItems == ItemType.UNREAD) { items.remove(i) notifyItemRemoved(position) updateItems(items) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt index c1dc7b1..a64393e 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/Repository.kt @@ -2,6 +2,7 @@ package bou.amine.apps.readerforselfossv2.repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.utils.DateUtils +import bou.amine.apps.readerforselfossv2.utils.ItemType interface Repository { @@ -10,7 +11,7 @@ interface Repository { var baseUrl: String var dateUtils: DateUtils - var displayedItems: String + var displayedItems: ItemType // Settings var itemsCaching: Boolean diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index 2832e23..2208fb6 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -4,6 +4,7 @@ import bou.amine.apps.readerforselfossv2.rest.SelfossApi import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.service.ApiDetailsService import bou.amine.apps.readerforselfossv2.utils.DateUtils +import bou.amine.apps.readerforselfossv2.utils.ItemType import com.russhwolf.settings.Settings import io.github.aakira.napier.Napier import kotlinx.coroutines.CoroutineScope @@ -18,16 +19,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet override var baseUrl = apiDetails.getBaseUrl() override lateinit var dateUtils: DateUtils - override var displayedItems = "unread" - set(value) { - field = when (value) { - "all" -> "all" - "unread" -> "unread" - "read" -> "read" - "starred" -> "starred" - else -> "all" - } - } + override var displayedItems = ItemType.UNREAD override var tagFilter: SelfossModel.Tag? = 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 { // TODO: Check connectivity - val fetchedItems = api.getItems(displayedItems, + val fetchedItems = api.getItems(displayedItems.type, settings.getString("prefer_api_items_number", "200").toInt(), offset = 0, tagFilter?.tag, @@ -70,7 +62,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet override suspend fun getOlderItems(): ArrayList { // TODO: Check connectivity val offset = items.size - val fetchedItems = api.getItems(displayedItems, + val fetchedItems = api.getItems(displayedItems.type, settings.getString("prefer_api_items_number", "200").toInt(), offset, tagFilter?.tag, @@ -99,9 +91,9 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet private fun filterSelectedItems(items: ArrayList): ArrayList { val tmpItems = items - if (displayedItems == "unread") { + if (displayedItems == ItemType.UNREAD) { tmpItems.removeAll { !it.unread } - } else if (displayedItems == "starred") { + } else if (displayedItems == ItemType.STARRED) { tmpItems.removeAll { !it.starred } } diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt new file mode 100644 index 0000000..d8390cc --- /dev/null +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/Enums.kt @@ -0,0 +1,7 @@ +package bou.amine.apps.readerforselfossv2.utils + +enum class ItemType(val type: String) { + UNREAD("unread"), + ALL("all"), + STARRED("starred") +} \ No newline at end of file