Remove searchService from the home activity

This commit is contained in:
davide 2022-08-13 17:34:09 +02:00
parent 554b8df9d0
commit e6c70c66fc
7 changed files with 52 additions and 66 deletions

View File

@ -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.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.service.SearchService
import bou.amine.apps.readerforselfossv2.utils.DateUtils
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
@ -86,7 +84,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private lateinit var dataBase: AndroidDeviceDatabase private lateinit var dataBase: AndroidDeviceDatabase
private lateinit var dbService: AndroidDeviceDatabaseService private lateinit var dbService: AndroidDeviceDatabaseService
private lateinit var searchService: SearchService
private val MENU_PREFERENCES = 12302 private val MENU_PREFERENCES = 12302
private val DRAWER_ID_TAGS = 100101L private val DRAWER_ID_TAGS = 100101L
private val DRAWER_ID_HIDDEN_TAGS = 101100L private val DRAWER_ID_HIDDEN_TAGS = 101100L
@ -186,8 +183,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
customTabActivityHelper = CustomTabActivityHelper() customTabActivityHelper = CustomTabActivityHelper()
dataBase = AndroidDeviceDatabase(applicationContext) dataBase = AndroidDeviceDatabase(applicationContext)
searchService = SearchService(DateUtils(repository.apiMajorVersion)) //dbService = AndroidDeviceDatabaseService(dataBase, searchService)
dbService = AndroidDeviceDatabaseService(dataBase, searchService)
handleBottomBar() handleBottomBar()
handleDrawer() handleDrawer()
@ -370,7 +366,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
userIdentifier = settings.getString("unique_id", "") userIdentifier = settings.getString("unique_id", "")
displayAccountHeader = settings.getBoolean("account_header_displaying", false) displayAccountHeader = settings.getBoolean("account_header_displaying", false)
infiniteScroll = settings.getBoolean("infinite_loading", false) infiniteScroll = settings.getBoolean("infinite_loading", false)
searchService.itemsCaching = settings.getBoolean("items_caching", false)
updateSources = settings.getBoolean("update_sources", true) updateSources = settings.getBoolean("update_sources", true)
markOnScroll = settings.getBoolean("mark_on_scroll", false) markOnScroll = settings.getBoolean("mark_on_scroll", false)
hiddenTags = if (settings.getString("hidden_tags", "").isNotEmpty()) { hiddenTags = if (settings.getString("hidden_tags", "").isNotEmpty()) {
@ -518,9 +513,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
textColor = ColorHolder.fromColor(Color.WHITE) textColor = ColorHolder.fromColor(Color.WHITE)
color = ColorHolder.fromColor(appColors.colorAccent) } color = ColorHolder.fromColor(appColors.colorAccent) }
onDrawerItemClickListener = { _,_,_ -> onDrawerItemClickListener = { _,_,_ ->
searchService.tagFilter = it.tag repository.tagFilter = it
searchService.sourceFilter = null repository.sourceFilter = null
searchService.sourceIDFilter = null
getElementsAccordingToTab() getElementsAccordingToTab()
fetchOnEmptyList() fetchOnEmptyList()
false false
@ -570,9 +564,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
textColor = ColorHolder.fromColor(Color.WHITE) textColor = ColorHolder.fromColor(Color.WHITE)
color = ColorHolder.fromColor(appColors.colorAccent) } color = ColorHolder.fromColor(appColors.colorAccent) }
onDrawerItemClickListener = { _,_,_ -> onDrawerItemClickListener = { _,_,_ ->
searchService.tagFilter = it.tag repository.tagFilter = it
searchService.sourceFilter = null repository.sourceFilter = null
searchService.sourceIDFilter = null
getElementsAccordingToTab() getElementsAccordingToTab()
fetchOnEmptyList() fetchOnEmptyList()
false false
@ -605,9 +598,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
identifier = source.id.toLong() identifier = source.id.toLong()
iconUrl = source.getIcon(repository.baseUrl) iconUrl = source.getIcon(repository.baseUrl)
onDrawerItemClickListener = { _,_,_ -> onDrawerItemClickListener = { _,_,_ ->
searchService.sourceIDFilter = source.id.toLong() repository.sourceFilter = source
searchService.sourceFilter = source.title repository.tagFilter = null
searchService.tagFilter = null
getElementsAccordingToTab() getElementsAccordingToTab()
fetchOnEmptyList() fetchOnEmptyList()
false false
@ -627,9 +619,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
identifier = DRAWER_ID_FILTERS identifier = DRAWER_ID_FILTERS
badgeRes = R.string.drawer_action_clear badgeRes = R.string.drawer_action_clear
onDrawerItemClickListener = { _,_,_ -> onDrawerItemClickListener = { _,_,_ ->
searchService.sourceFilter = null repository.sourceFilter = null
searchService.sourceIDFilter = null repository.tagFilter = null
searchService.tagFilter = null
binding.mainDrawer.setSelectionAtPosition(-1) binding.mainDrawer.setSelectionAtPosition(-1)
getElementsAccordingToTab() getElementsAccordingToTab()
fetchOnEmptyList() fetchOnEmptyList()
@ -920,7 +911,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.selectedType = "unread" repository.displayedItems = "unread"
items = if (appendResults) { items = if (appendResults) {
repository.getNewerItems() repository.getNewerItems()
} else { } else {
@ -934,7 +925,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.selectedType = "all" repository.displayedItems = "all"
items = if (appendResults) { items = if (appendResults) {
repository.getNewerItems() repository.getNewerItems()
} else { } else {
@ -948,7 +939,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.selectedType = "starred" repository.displayedItems = "starred"
items = if (appendResults) { items = if (appendResults) {
repository.getNewerItems() repository.getNewerItems()
} else { } else {
@ -984,8 +975,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
fullHeightCards, fullHeightCards,
appColors, appColors,
userIdentifier, userIdentifier,
config, config
searchService
) { ) {
updateItems(it) updateItems(it)
} }
@ -1000,8 +990,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
articleViewer, articleViewer,
userIdentifier, userIdentifier,
appColors, appColors,
config, config
searchService
) { ) {
updateItems(it) updateItems(it)
} }
@ -1064,7 +1053,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
override fun onQueryTextChange(p0: String?): Boolean { override fun onQueryTextChange(p0: String?): Boolean {
if (p0.isNullOrBlank()) { if (p0.isNullOrBlank()) {
searchService.searchFilter = null repository.searchFilter = null
getElementsAccordingToTab() getElementsAccordingToTab()
fetchOnEmptyList() fetchOnEmptyList()
} }
@ -1072,7 +1061,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
} }
override fun onQueryTextSubmit(p0: String?): Boolean { override fun onQueryTextSubmit(p0: String?): Boolean {
searchService.searchFilter = p0 repository.searchFilter = p0
getElementsAccordingToTab() getElementsAccordingToTab()
fetchOnEmptyList() fetchOnEmptyList()
return false return false
@ -1171,10 +1160,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun maxItemNumber(): Int = private fun maxItemNumber(): Int =
when (elementsShown) { when (elementsShown) {
UNREAD_SHOWN -> searchService.badgeUnread UNREAD_SHOWN -> repository.badgeUnread
READ_SHOWN -> searchService.badgeAll READ_SHOWN -> repository.badgeAll
FAV_SHOWN -> searchService.badgeStarred FAV_SHOWN -> repository.badgeStarred
else -> searchService.badgeUnread // if !elementsShown then unread are fetched. else -> repository.badgeUnread // if !elementsShown then unread are fetched.
} }
private fun updateItems(adapterItems: ArrayList<SelfossModel.Item>) { private fun updateItems(adapterItems: ArrayList<SelfossModel.Item>) {

View File

@ -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.android.utils.network.isNetworkAvailable
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.service.SearchService
import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.DateUtils
import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.TextDrawable
import com.amulyakhare.textdrawable.util.ColorGenerator import com.amulyakhare.textdrawable.util.ColorGenerator
@ -42,7 +41,6 @@ class ItemCardAdapter(
override val appColors: AppColors, override val appColors: AppColors,
override val userIdentifier: String, override val userIdentifier: String,
override val config: Config, override val config: Config,
override val searchService: SearchService,
override val updateItems: (ArrayList<SelfossModel.Item>) -> Unit override val updateItems: (ArrayList<SelfossModel.Item>) -> Unit
) : ItemsAdapter<ItemCardAdapter.ViewHolder>() { ) : ItemsAdapter<ItemCardAdapter.ViewHolder>() {
private val c: Context = app.baseContext private val c: Context = app.baseContext
@ -155,8 +153,7 @@ class ItemCardAdapter(
customTabsIntent, customTabsIntent,
internalBrowser, internalBrowser,
articleViewer, articleViewer,
app, app
searchService
) )
} }
} }

View File

@ -33,7 +33,6 @@ class ItemListAdapter(
override val userIdentifier: String, override val userIdentifier: String,
override val appColors: AppColors, override val appColors: AppColors,
override val config: Config, override val config: Config,
override val searchService: SearchService,
override val updateItems: (ArrayList<SelfossModel.Item>) -> Unit override val updateItems: (ArrayList<SelfossModel.Item>) -> Unit
) : ItemsAdapter<ItemListAdapter.ViewHolder>() { ) : ItemsAdapter<ItemListAdapter.ViewHolder>() {
private val generator: ColorGenerator = ColorGenerator.MATERIAL private val generator: ColorGenerator = ColorGenerator.MATERIAL
@ -100,8 +99,7 @@ class ItemListAdapter(
customTabsIntent, customTabsIntent,
internalBrowser, internalBrowser,
articleViewer, articleViewer,
app, app
searchService
) )
} }
} }

View File

@ -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.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.service.SearchService
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
@ -25,7 +24,6 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
abstract val app: Activity abstract val app: Activity
abstract val appColors: AppColors abstract val appColors: AppColors
abstract val config: Config abstract val config: Config
abstract val searchService: SearchService
abstract val updateItems: (ArrayList<SelfossModel.Item>) -> Unit abstract val updateItems: (ArrayList<SelfossModel.Item>) -> Unit
fun updateAllItems(items: ArrayList<SelfossModel.Item>) { fun updateAllItems(items: ArrayList<SelfossModel.Item>) {

View File

@ -10,18 +10,17 @@ import android.net.Uri
import android.os.Build import android.os.Build
import android.text.Spannable import android.text.Spannable
import android.text.style.ClickableSpan import android.text.style.ClickableSpan
import androidx.browser.customtabs.CustomTabsIntent
import android.util.Patterns import android.util.Patterns
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.browser.customtabs.CustomTabsIntent
import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.R
import bou.amine.apps.readerforselfossv2.android.ReaderActivity import bou.amine.apps.readerforselfossv2.android.ReaderActivity
import bou.amine.apps.readerforselfossv2.android.model.getLinkDecoded import bou.amine.apps.readerforselfossv2.android.model.getLinkDecoded
import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper
import bou.amine.apps.readerforselfossv2.rest.SelfossModel import bou.amine.apps.readerforselfossv2.rest.SelfossModel
import bou.amine.apps.readerforselfossv2.service.SearchService
import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
@ -79,12 +78,10 @@ fun Context.openItemUrlInternally(
linkDecoded: String, linkDecoded: String,
customTabsIntent: CustomTabsIntent, customTabsIntent: CustomTabsIntent,
articleViewer: Boolean, articleViewer: Boolean,
app: Activity, app: Activity
searchService: SearchService
) { ) {
if (articleViewer) { if (articleViewer) {
ReaderActivity.allItems = allItems ReaderActivity.allItems = allItems
searchService.position = currentItem
val intent = Intent(this, ReaderActivity::class.java) val intent = Intent(this, ReaderActivity::class.java)
intent.putExtra("currentItem", currentItem) intent.putExtra("currentItem", currentItem)
app.startActivity(intent) app.startActivity(intent)
@ -123,8 +120,7 @@ fun Context.openItemUrl(
customTabsIntent: CustomTabsIntent, customTabsIntent: CustomTabsIntent,
internalBrowser: Boolean, internalBrowser: Boolean,
articleViewer: Boolean, articleViewer: Boolean,
app: Activity, app: Activity
searchService: SearchService
) { ) {
if (!linkDecoded.isUrlValid()) { if (!linkDecoded.isUrlValid()) {
@ -143,8 +139,7 @@ fun Context.openItemUrl(
linkDecoded, linkDecoded,
customTabsIntent, customTabsIntent,
articleViewer, articleViewer,
app, app
searchService
) )
} else { } else {
this.openItemUrlInternalBrowser( this.openItemUrlInternalBrowser(

View File

@ -8,7 +8,10 @@ interface Repository {
var items: ArrayList<SelfossModel.Item> var items: ArrayList<SelfossModel.Item>
var baseUrl: String var baseUrl: String
var selectedType: String var displayedItems: String
// Settings
var itemsCaching: Boolean
// API // API
var apiMajorVersion: Int var apiMajorVersion: Int
@ -16,6 +19,10 @@ interface Repository {
var badgeAll: Int var badgeAll: Int
var badgeStarred: Int var badgeStarred: Int
var tagFilter: SelfossModel.Tag?
var sourceFilter: SelfossModel.Source?
var searchFilter: String?
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> suspend fun getNewerItems(): ArrayList<SelfossModel.Item>
suspend fun getOlderItems(): ArrayList<SelfossModel.Item> suspend fun getOlderItems(): ArrayList<SelfossModel.Item>
suspend fun reloadBadges(): Boolean suspend fun reloadBadges(): Boolean

View File

@ -19,7 +19,7 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
override var baseUrl = apiDetails.getBaseUrl() override var baseUrl = apiDetails.getBaseUrl()
override var selectedType = "unread" override var displayedItems = "unread"
set(value) { set(value) {
field = when (value) { field = when (value) {
"all" -> "all" "all" -> "all"
@ -30,9 +30,11 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
} }
} }
private var selectedTag: SelfossModel.Tag? = null override var tagFilter: SelfossModel.Tag? = null
private var selectedSource: SelfossModel.Source? = null override var sourceFilter: SelfossModel.Source? = null
private var search: String? = null override var searchFilter: String? = null
override var itemsCaching = settings.getBoolean("items_caching", false)
override var apiMajorVersion = 0 override var apiMajorVersion = 0
override var badgeUnread = 0 override var badgeUnread = 0
@ -52,12 +54,12 @@ 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(displayedItems,
settings.getString("prefer_api_items_number", "200").toInt(), settings.getString("prefer_api_items_number", "200").toInt(),
offset = 0, offset = 0,
selectedTag?.tag, tagFilter?.tag,
selectedSource?.id?.toLong(), sourceFilter?.id?.toLong(),
search) searchFilter)
if (fetchedItems != null) { if (fetchedItems != null) {
items = ArrayList(fetchedItems) items = ArrayList(fetchedItems)
@ -68,12 +70,12 @@ 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(selectedType, val fetchedItems = api.getItems(displayedItems,
settings.getString("prefer_api_items_number", "200").toInt(), settings.getString("prefer_api_items_number", "200").toInt(),
offset, offset,
selectedTag?.tag, tagFilter?.tag,
selectedSource?.id?.toLong(), sourceFilter?.id?.toLong(),
search) searchFilter)
if (fetchedItems != null) { if (fetchedItems != null) {
items = ArrayList(fetchedItems) items = ArrayList(fetchedItems)
@ -98,14 +100,14 @@ 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 = ArrayList(items) val tmpItems = ArrayList(items)
if (selectedType == "unread") { if (displayedItems == "unread") {
tmpItems.removeAll { !it.unread } tmpItems.removeAll { !it.unread }
} else if (selectedType == "starred") { } else if (displayedItems == "starred") {
tmpItems.removeAll { !it.starred } tmpItems.removeAll { !it.starred }
} }
if (selectedTag != null) { if (tagFilter != null) {
tmpItems.removeAll { !it.tags.contains(selectedTag!!.tag) } tmpItems.removeAll { !it.tags.contains(tagFilter!!.tag) }
} }
return tmpItems return tmpItems