Remove database access from the Home
This commit is contained in:
parent
f7055626d9
commit
b5b820c64b
@ -15,9 +15,6 @@ import androidx.activity.result.contract.ActivityResultContracts
|
|||||||
import androidx.appcompat.app.ActionBarDrawerToggle
|
import androidx.appcompat.app.ActionBarDrawerToggle
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
|
||||||
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO
|
|
||||||
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES
|
|
||||||
import androidx.appcompat.widget.SearchView
|
import androidx.appcompat.widget.SearchView
|
||||||
import androidx.core.view.doOnNextLayout
|
import androidx.core.view.doOnNextLayout
|
||||||
import androidx.drawerlayout.widget.DrawerLayout
|
import androidx.drawerlayout.widget.DrawerLayout
|
||||||
@ -98,7 +95,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
|||||||
private val repository : Repository by instance()
|
private val repository : Repository by instance()
|
||||||
private val appSettingsService : AppSettingsService by instance()
|
private val appSettingsService : AppSettingsService by instance()
|
||||||
|
|
||||||
data class DrawerData(val tags: List<SelfossModel.Tag>?, val sources: List<SelfossModel.Source>?)
|
data class DrawerData(val tags: List<SelfossModel.Tag>, val sources: List<SelfossModel.Source>)
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -352,28 +349,14 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
|||||||
)
|
)
|
||||||
|
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
val drawerData = DrawerData(repository.getDBTags().map { it.toView() },
|
val drawerData = DrawerData(repository.getTags(), repository.getSources())
|
||||||
repository.getDBSources().map { it.toView() })
|
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
// TODO: All this logic should be handled by the repository, simplify and remove direct DB access
|
handleDrawerData(drawerData)
|
||||||
// Only refresh if there is no data in the DB, or if the `UpdateSources` setting is enabled
|
|
||||||
if (drawerData.sources?.isEmpty() == true || appSettingsService.isUpdateSourcesEnabled()) {
|
|
||||||
drawerApiCalls(drawerData)
|
|
||||||
} else {
|
|
||||||
handleDrawerData(drawerData, loadedFromCache = true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun drawerApiCalls(drawerData: DrawerData) {
|
private fun handleDrawerData(drawerData: DrawerData) {
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
|
||||||
val apiDrawerData = DrawerData(repository.getTags(), repository.getSources())
|
|
||||||
handleDrawerData(if (drawerData != apiDrawerData) apiDrawerData else drawerData)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun handleDrawerData(drawerData: DrawerData, loadedFromCache: Boolean = false) {
|
|
||||||
binding.mainDrawer.itemAdapter.clear()
|
binding.mainDrawer.itemAdapter.clear()
|
||||||
|
|
||||||
// Filters title with clear action
|
// Filters title with clear action
|
||||||
@ -387,7 +370,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Hidden tags
|
// Hidden tags
|
||||||
if (drawerData.tags != null && drawerData.tags.isNotEmpty() && appSettingsService.getHiddenTags().isNotEmpty()) {
|
if (drawerData.tags.isNotEmpty() && appSettingsService.getHiddenTags().isNotEmpty()) {
|
||||||
secondaryItem(
|
secondaryItem(
|
||||||
withDivider = true,
|
withDivider = true,
|
||||||
R.string.drawer_item_hidden_tags,
|
R.string.drawer_item_hidden_tags,
|
||||||
@ -398,13 +381,13 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
|||||||
|
|
||||||
// Tags
|
// Tags
|
||||||
secondaryItem(withDivider = true, R.string.drawer_item_tags, DRAWER_ID_TAGS)
|
secondaryItem(withDivider = true, R.string.drawer_item_tags, DRAWER_ID_TAGS)
|
||||||
if (drawerData.tags == null && !loadedFromCache) {
|
if (drawerData.tags.isEmpty()) {
|
||||||
binding.mainDrawer.itemAdapter.add(
|
binding.mainDrawer.itemAdapter.add(
|
||||||
SecondaryDrawerItem()
|
SecondaryDrawerItem()
|
||||||
.apply { nameRes = R.string.drawer_error_loading_tags; isSelectable = false }
|
.apply { nameRes = R.string.drawer_error_loading_tags; isSelectable = false }
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
handleTags(drawerData.tags!!)
|
handleTags(drawerData.tags)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sources
|
// Sources
|
||||||
@ -412,7 +395,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
|||||||
startActivity(Intent(v!!.context, SourcesActivity::class.java))
|
startActivity(Intent(v!!.context, SourcesActivity::class.java))
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
if (drawerData.sources == null && !loadedFromCache) {
|
if (drawerData.sources.isEmpty()) {
|
||||||
binding.mainDrawer.itemAdapter.add(
|
binding.mainDrawer.itemAdapter.add(
|
||||||
SecondaryDrawerItem().apply {
|
SecondaryDrawerItem().apply {
|
||||||
nameRes = R.string.drawer_error_loading_tags
|
nameRes = R.string.drawer_error_loading_tags
|
||||||
@ -420,7 +403,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
handleSources(drawerData.sources!!)
|
handleSources(drawerData.sources)
|
||||||
}
|
}
|
||||||
|
|
||||||
// About action
|
// About action
|
||||||
|
@ -410,11 +410,9 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
|
|||||||
private fun deleteDBAction(action: ACTION) =
|
private fun deleteDBAction(action: ACTION) =
|
||||||
db.actionsQueries.deleteAction(action.id)
|
db.actionsQueries.deleteAction(action.id)
|
||||||
|
|
||||||
// TODO: This function should be private
|
private fun getDBTags(): List<TAG> = db.tagsQueries.tags().executeAsList()
|
||||||
fun getDBTags(): List<TAG> = db.tagsQueries.tags().executeAsList()
|
|
||||||
|
|
||||||
// TODO: This function should be private
|
private fun getDBSources(): List<SOURCE> = db.sourcesQueries.sources().executeAsList()
|
||||||
fun getDBSources(): List<SOURCE> = db.sourcesQueries.sources().executeAsList()
|
|
||||||
|
|
||||||
private fun resetDBTagsWithData(tagEntities: List<SelfossModel.Tag>) {
|
private fun resetDBTagsWithData(tagEntities: List<SelfossModel.Tag>) {
|
||||||
db.tagsQueries.deleteAllTags()
|
db.tagsQueries.deleteAllTags()
|
||||||
|
Loading…
Reference in New Issue
Block a user