From b5b820c64b7117edcf68ef39e2b89b342f3e4cb5 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Fri, 30 Sep 2022 15:00:01 +0200 Subject: [PATCH] Remove database access from the Home --- .../android/HomeActivity.kt | 35 +++++-------------- .../repository/RepositoryImpl.kt | 6 ++-- 2 files changed, 11 insertions(+), 30 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 29c7835..877994e 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 @@ -15,9 +15,6 @@ import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AlertDialog 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.core.view.doOnNextLayout import androidx.drawerlayout.widget.DrawerLayout @@ -98,7 +95,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar private val repository : Repository by instance() private val appSettingsService : AppSettingsService by instance() - data class DrawerData(val tags: List?, val sources: List?) + data class DrawerData(val tags: List, val sources: List) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -352,28 +349,14 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar ) CoroutineScope(Dispatchers.IO).launch { - val drawerData = DrawerData(repository.getDBTags().map { it.toView() }, - repository.getDBSources().map { it.toView() }) + val drawerData = DrawerData(repository.getTags(), repository.getSources()) runOnUiThread { - // TODO: All this logic should be handled by the repository, simplify and remove direct DB access - // 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) - } + handleDrawerData(drawerData) } } } - private fun drawerApiCalls(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) { + private fun handleDrawerData(drawerData: DrawerData) { binding.mainDrawer.itemAdapter.clear() // Filters title with clear action @@ -387,7 +370,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } // Hidden tags - if (drawerData.tags != null && drawerData.tags.isNotEmpty() && appSettingsService.getHiddenTags().isNotEmpty()) { + if (drawerData.tags.isNotEmpty() && appSettingsService.getHiddenTags().isNotEmpty()) { secondaryItem( withDivider = true, R.string.drawer_item_hidden_tags, @@ -398,13 +381,13 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar // 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( SecondaryDrawerItem() .apply { nameRes = R.string.drawer_error_loading_tags; isSelectable = false } ) } else { - handleTags(drawerData.tags!!) + handleTags(drawerData.tags) } // Sources @@ -412,7 +395,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar startActivity(Intent(v!!.context, SourcesActivity::class.java)) false } - if (drawerData.sources == null && !loadedFromCache) { + if (drawerData.sources.isEmpty()) { binding.mainDrawer.itemAdapter.add( SecondaryDrawerItem().apply { nameRes = R.string.drawer_error_loading_tags @@ -420,7 +403,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } ) } else { - handleSources(drawerData.sources!!) + handleSources(drawerData.sources) } // About action 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 8a1fa86..ca9321f 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 @@ -410,11 +410,9 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap private fun deleteDBAction(action: ACTION) = db.actionsQueries.deleteAction(action.id) - // TODO: This function should be private - fun getDBTags(): List = db.tagsQueries.tags().executeAsList() + private fun getDBTags(): List = db.tagsQueries.tags().executeAsList() - // TODO: This function should be private - fun getDBSources(): List = db.sourcesQueries.sources().executeAsList() + private fun getDBSources(): List = db.sourcesQueries.sources().executeAsList() private fun resetDBTagsWithData(tagEntities: List) { db.tagsQueries.deleteAllTags()