App was a little slow with livedata.

This commit is contained in:
Amine 2018-10-13 10:24:58 +02:00
parent 4901e7174c
commit 3b6891c84a
2 changed files with 16 additions and 23 deletions

View File

@ -107,7 +107,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private var lastFetchDone: Boolean = false private var lastFetchDone: Boolean = false
private var hiddenTags: List<String> = emptyList() private var hiddenTags: List<String> = emptyList()
private lateinit var tabNewBadge: TextBadgeItem private lateinit var tabNewBadge: TextBadgeItem
private lateinit var tabArchiveBadge: TextBadgeItem private lateinit var tabArchiveBadge: TextBadgeItem
private lateinit var tabStarredBadge: TextBadgeItem private lateinit var tabStarredBadge: TextBadgeItem
@ -128,7 +127,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private var badgeAll: Int = -1 private var badgeAll: Int = -1
private var badgeFavs: Int = -1 private var badgeFavs: Int = -1
private lateinit var tagsBadge: Map<Long, Int> private lateinit var tagsBadge: Map<Long, Int>
private lateinit var db: AppDatabase private lateinit var db: AppDatabase
@ -521,7 +519,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
) )
} }
} else { } else {
val filteredHiddenTags: List<Tag> = maybeTags.filter { hiddenTags.contains(it.tag) } val filteredHiddenTags: List<Tag> =
maybeTags.filter { hiddenTags.contains(it.tag) }
tagsBadge = filteredHiddenTags.map { tagsBadge = filteredHiddenTags.map {
val gd = GradientDrawable() val gd = GradientDrawable()
val color = try { val color = try {
@ -664,7 +663,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
if (maybeDrawerData.sources != null) { if (maybeDrawerData.sources != null) {
thread { thread {
val sourceEntities = maybeDrawerData.sources.map { it.toEntity(this@HomeActivity) } val sourceEntities =
maybeDrawerData.sources.map { it.toEntity(this@HomeActivity) }
db.drawerDataDao().insertAllSources(*sourceEntities.toTypedArray()) db.drawerDataDao().insertAllSources(*sourceEntities.toTypedArray())
} }
} }
@ -730,20 +730,13 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
) )
) )
db.drawerDataDao().tags().observeForever { tags -> thread {
db.drawerDataDao().sources().observeForever { sources -> var drawerData = DrawerData(db.drawerDataDao().tags().map { it.toView() },
var drawerData = DrawerData(null, null) db.drawerDataDao().sources().map { it.toView() })
if (tags != null) {
drawerData = drawerData.copy(tags = tags.map { it.toView() })
}
if (sources != null) {
drawerData = drawerData.copy(sources = sources.map { it.toView() })
}
handleDrawerData(drawerData, loadedFromCache = true) handleDrawerData(drawerData, loadedFromCache = true)
drawerApiCalls(drawerData) drawerApiCalls(drawerData)
} }
} }
}
private fun reloadLayoutManager() { private fun reloadLayoutManager() {
val currentManager = recyclerView.layoutManager val currentManager = recyclerView.layoutManager

View File

@ -12,10 +12,10 @@ import apps.amine.bou.readerforselfoss.persistence.entities.TagEntity
@Dao @Dao
interface DrawerDataDao { interface DrawerDataDao {
@Query("SELECT * FROM tags") @Query("SELECT * FROM tags")
fun tags(): LiveData<List<TagEntity>> fun tags(): List<TagEntity>
@Query("SELECT * FROM sources") @Query("SELECT * FROM sources")
fun sources(): LiveData<List<SourceEntity>> fun sources(): List<SourceEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAllTags(vararg tags: TagEntity) fun insertAllTags(vararg tags: TagEntity)