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.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<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.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<SelfossModel.Item>) -> Unit
) : ItemsAdapter<ItemCardAdapter.ViewHolder>() {
private val c: Context = app.baseContext
@ -155,8 +153,7 @@ class ItemCardAdapter(
customTabsIntent,
internalBrowser,
articleViewer,
app,
searchService
app
)
}
}

View File

@ -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<SelfossModel.Item>) -> Unit
) : ItemsAdapter<ItemListAdapter.ViewHolder>() {
private val generator: ColorGenerator = ColorGenerator.MATERIAL
@ -100,8 +99,7 @@ class ItemListAdapter(
customTabsIntent,
internalBrowser,
articleViewer,
app,
searchService
app
)
}
}

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

View File

@ -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(

View File

@ -8,7 +8,10 @@ interface Repository {
var items: ArrayList<SelfossModel.Item>
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<SelfossModel.Item>
suspend fun getOlderItems(): ArrayList<SelfossModel.Item>
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 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<SelfossModel.Item> {
// 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<SelfossModel.Item> {
// 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<SelfossModel.Item>): ArrayList<SelfossModel.Item> {
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