From e6c70c66fc488c5cd5b9468ffb2012e4e671f406 Mon Sep 17 00:00:00 2001 From: davide Date: Sat, 13 Aug 2022 17:34:09 +0200 Subject: [PATCH] Remove searchService from the home activity --- .../android/HomeActivity.kt | 51 ++++++++----------- .../android/adapters/ItemCardAdapter.kt | 5 +- .../android/adapters/ItemListAdapter.kt | 4 +- .../android/adapters/ItemsAdapter.kt | 2 - .../android/utils/LinksUtils.kt | 13 ++--- .../repository/Repository.kt | 9 +++- .../repository/RepositoryImpl.kt | 34 +++++++------ 7 files changed, 52 insertions(+), 66 deletions(-) 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 7aa54df..133e622 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,8 +49,6 @@ 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.service.SearchService -import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.longHash import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationItem @@ -86,7 +84,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private lateinit var dataBase: AndroidDeviceDatabase private lateinit var dbService: AndroidDeviceDatabaseService - private lateinit var searchService: SearchService private val MENU_PREFERENCES = 12302 private val DRAWER_ID_TAGS = 100101L private val DRAWER_ID_HIDDEN_TAGS = 101100L @@ -186,8 +183,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar customTabActivityHelper = CustomTabActivityHelper() dataBase = AndroidDeviceDatabase(applicationContext) - searchService = SearchService(DateUtils(repository.apiMajorVersion)) - dbService = AndroidDeviceDatabaseService(dataBase, searchService) + //dbService = AndroidDeviceDatabaseService(dataBase, searchService) handleBottomBar() handleDrawer() @@ -370,7 +366,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar userIdentifier = settings.getString("unique_id", "") displayAccountHeader = settings.getBoolean("account_header_displaying", false) infiniteScroll = settings.getBoolean("infinite_loading", false) - searchService.itemsCaching = settings.getBoolean("items_caching", false) updateSources = settings.getBoolean("update_sources", true) markOnScroll = settings.getBoolean("mark_on_scroll", false) hiddenTags = if (settings.getString("hidden_tags", "").isNotEmpty()) { @@ -518,9 +513,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar textColor = ColorHolder.fromColor(Color.WHITE) color = ColorHolder.fromColor(appColors.colorAccent) } onDrawerItemClickListener = { _,_,_ -> - searchService.tagFilter = it.tag - searchService.sourceFilter = null - searchService.sourceIDFilter = null + repository.tagFilter = it + repository.sourceFilter = null getElementsAccordingToTab() fetchOnEmptyList() false @@ -570,9 +564,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar textColor = ColorHolder.fromColor(Color.WHITE) color = ColorHolder.fromColor(appColors.colorAccent) } onDrawerItemClickListener = { _,_,_ -> - searchService.tagFilter = it.tag - searchService.sourceFilter = null - searchService.sourceIDFilter = null + repository.tagFilter = it + repository.sourceFilter = null getElementsAccordingToTab() fetchOnEmptyList() false @@ -605,9 +598,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar identifier = source.id.toLong() iconUrl = source.getIcon(repository.baseUrl) onDrawerItemClickListener = { _,_,_ -> - searchService.sourceIDFilter = source.id.toLong() - searchService.sourceFilter = source.title - searchService.tagFilter = null + repository.sourceFilter = source + repository.tagFilter = null getElementsAccordingToTab() fetchOnEmptyList() false @@ -627,9 +619,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar identifier = DRAWER_ID_FILTERS badgeRes = R.string.drawer_action_clear onDrawerItemClickListener = { _,_,_ -> - searchService.sourceFilter = null - searchService.sourceIDFilter = null - searchService.tagFilter = null + repository.sourceFilter = null + repository.tagFilter = null binding.mainDrawer.setSelectionAtPosition(-1) getElementsAccordingToTab() fetchOnEmptyList() @@ -920,7 +911,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun getUnRead(appendResults: Boolean = false) { CoroutineScope(Dispatchers.Main).launch { binding.swipeRefreshLayout.isRefreshing = true - repository.selectedType = "unread" + repository.displayedItems = "unread" items = if (appendResults) { repository.getNewerItems() } else { @@ -934,7 +925,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun getRead(appendResults: Boolean = false) { CoroutineScope(Dispatchers.Main).launch { binding.swipeRefreshLayout.isRefreshing = true - repository.selectedType = "all" + repository.displayedItems = "all" items = if (appendResults) { repository.getNewerItems() } else { @@ -948,7 +939,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun getStarred(appendResults: Boolean = false) { CoroutineScope(Dispatchers.Main).launch { binding.swipeRefreshLayout.isRefreshing = true - repository.selectedType = "starred" + repository.displayedItems = "starred" items = if (appendResults) { repository.getNewerItems() } else { @@ -984,8 +975,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar fullHeightCards, appColors, userIdentifier, - config, - searchService + config ) { updateItems(it) } @@ -1000,8 +990,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar articleViewer, userIdentifier, appColors, - config, - searchService + config ) { updateItems(it) } @@ -1064,7 +1053,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar override fun onQueryTextChange(p0: String?): Boolean { if (p0.isNullOrBlank()) { - searchService.searchFilter = null + repository.searchFilter = null getElementsAccordingToTab() fetchOnEmptyList() } @@ -1072,7 +1061,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } override fun onQueryTextSubmit(p0: String?): Boolean { - searchService.searchFilter = p0 + repository.searchFilter = p0 getElementsAccordingToTab() fetchOnEmptyList() return false @@ -1171,10 +1160,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private fun maxItemNumber(): Int = when (elementsShown) { - UNREAD_SHOWN -> searchService.badgeUnread - READ_SHOWN -> searchService.badgeAll - FAV_SHOWN -> searchService.badgeStarred - else -> searchService.badgeUnread // if !elementsShown then unread are fetched. + UNREAD_SHOWN -> repository.badgeUnread + READ_SHOWN -> repository.badgeAll + FAV_SHOWN -> repository.badgeStarred + else -> repository.badgeUnread // if !elementsShown then unread are fetched. } private fun updateItems(adapterItems: ArrayList) { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt index 146df82..0e9bf0f 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt @@ -19,7 +19,6 @@ import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawa import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAvailable import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.rest.SelfossModel -import bou.amine.apps.readerforselfossv2.service.SearchService import bou.amine.apps.readerforselfossv2.utils.DateUtils import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator @@ -42,7 +41,6 @@ class ItemCardAdapter( override val appColors: AppColors, override val userIdentifier: String, override val config: Config, - override val searchService: SearchService, override val updateItems: (ArrayList) -> Unit ) : ItemsAdapter() { private val c: Context = app.baseContext @@ -155,8 +153,7 @@ class ItemCardAdapter( customTabsIntent, internalBrowser, articleViewer, - app, - searchService + app ) } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt index f6358bd..94fa9dc 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt @@ -33,7 +33,6 @@ class ItemListAdapter( override val userIdentifier: String, override val appColors: AppColors, override val config: Config, - override val searchService: SearchService, override val updateItems: (ArrayList) -> Unit ) : ItemsAdapter() { private val generator: ColorGenerator = ColorGenerator.MATERIAL @@ -100,8 +99,7 @@ class ItemListAdapter( customTabsIntent, internalBrowser, articleViewer, - app, - searchService + app ) } } 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 6ec5237..c905b38 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,7 +10,6 @@ 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.service.SearchService import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -25,7 +24,6 @@ abstract class ItemsAdapter : RecyclerView.Adapte abstract val app: Activity abstract val appColors: AppColors abstract val config: Config - abstract val searchService: SearchService abstract val updateItems: (ArrayList) -> Unit fun updateAllItems(items: ArrayList) { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt index b642c70..7e0200a 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt @@ -10,18 +10,17 @@ import android.net.Uri import android.os.Build import android.text.Spannable import android.text.style.ClickableSpan -import androidx.browser.customtabs.CustomTabsIntent import android.util.Patterns import android.view.MotionEvent import android.view.View import android.widget.TextView import android.widget.Toast +import androidx.browser.customtabs.CustomTabsIntent import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.ReaderActivity import bou.amine.apps.readerforselfossv2.android.model.getLinkDecoded import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.rest.SelfossModel -import bou.amine.apps.readerforselfossv2.service.SearchService import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp import okhttp3.HttpUrl.Companion.toHttpUrlOrNull @@ -79,12 +78,10 @@ fun Context.openItemUrlInternally( linkDecoded: String, customTabsIntent: CustomTabsIntent, articleViewer: Boolean, - app: Activity, - searchService: SearchService + app: Activity ) { if (articleViewer) { ReaderActivity.allItems = allItems - searchService.position = currentItem val intent = Intent(this, ReaderActivity::class.java) intent.putExtra("currentItem", currentItem) app.startActivity(intent) @@ -123,8 +120,7 @@ fun Context.openItemUrl( customTabsIntent: CustomTabsIntent, internalBrowser: Boolean, articleViewer: Boolean, - app: Activity, - searchService: SearchService + app: Activity ) { if (!linkDecoded.isUrlValid()) { @@ -143,8 +139,7 @@ fun Context.openItemUrl( linkDecoded, customTabsIntent, articleViewer, - app, - searchService + app ) } else { this.openItemUrlInternalBrowser( 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 2df1995..be225f8 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 @@ -8,7 +8,10 @@ interface Repository { var items: ArrayList var baseUrl: String - var selectedType: String + var displayedItems: String + + // Settings + var itemsCaching: Boolean // API var apiMajorVersion: Int @@ -16,6 +19,10 @@ interface Repository { var badgeAll: Int var badgeStarred: Int + var tagFilter: SelfossModel.Tag? + var sourceFilter: SelfossModel.Source? + var searchFilter: String? + suspend fun getNewerItems(): ArrayList suspend fun getOlderItems(): ArrayList suspend fun reloadBadges(): 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 e933ff6..07752d8 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 @@ -19,7 +19,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet override var baseUrl = apiDetails.getBaseUrl() - override var selectedType = "unread" + override var displayedItems = "unread" set(value) { field = when (value) { "all" -> "all" @@ -30,9 +30,11 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet } } - private var selectedTag: SelfossModel.Tag? = null - private var selectedSource: SelfossModel.Source? = null - private var search: String? = null + override var tagFilter: SelfossModel.Tag? = null + override var sourceFilter: SelfossModel.Source? = null + override var searchFilter: String? = null + + override var itemsCaching = settings.getBoolean("items_caching", false) override var apiMajorVersion = 0 override var badgeUnread = 0 @@ -52,12 +54,12 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet override suspend fun getNewerItems(): ArrayList { // TODO: Check connectivity - val fetchedItems = api.getItems(selectedType, + val fetchedItems = api.getItems(displayedItems, settings.getString("prefer_api_items_number", "200").toInt(), offset = 0, - selectedTag?.tag, - selectedSource?.id?.toLong(), - search) + tagFilter?.tag, + sourceFilter?.id?.toLong(), + searchFilter) if (fetchedItems != null) { items = ArrayList(fetchedItems) @@ -68,12 +70,12 @@ 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(selectedType, + val fetchedItems = api.getItems(displayedItems, settings.getString("prefer_api_items_number", "200").toInt(), offset, - selectedTag?.tag, - selectedSource?.id?.toLong(), - search) + tagFilter?.tag, + sourceFilter?.id?.toLong(), + searchFilter) if (fetchedItems != null) { items = ArrayList(fetchedItems) @@ -98,14 +100,14 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet private fun filterSelectedItems(items: ArrayList): ArrayList { val tmpItems = ArrayList(items) - if (selectedType == "unread") { + if (displayedItems == "unread") { tmpItems.removeAll { !it.unread } - } else if (selectedType == "starred") { + } else if (displayedItems == "starred") { tmpItems.removeAll { !it.starred } } - if (selectedTag != null) { - tmpItems.removeAll { !it.tags.contains(selectedTag!!.tag) } + if (tagFilter != null) { + tmpItems.removeAll { !it.tags.contains(tagFilter!!.tag) } } return tmpItems