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
@ -387,7 +385,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private fun handleThemeBinding() { private fun handleThemeBinding() {
val scoop = Scoop.getInstance() val scoop = Scoop.getInstance()
scoop.bind(this, Toppings.PRIMARY.value, toolBar) scoop.bind(this, Toppings.PRIMARY.value, toolBar)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
scoop.bindStatusBar(this, Toppings.PRIMARY_DARK.value) scoop.bindStatusBar(this, Toppings.PRIMARY_DARK.value)
} }
} }
@ -397,7 +395,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
val scoop = Scoop.getInstance() val scoop = Scoop.getInstance()
scoop.update(Toppings.PRIMARY.value, appColors.colorPrimary) scoop.update(Toppings.PRIMARY.value, appColors.colorPrimary)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
scoop.update(Toppings.PRIMARY_DARK.value, appColors.colorPrimaryDark) scoop.update(Toppings.PRIMARY_DARK.value, appColors.colorPrimaryDark)
} }
} }
@ -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,18 +730,11 @@ 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) { handleDrawerData(drawerData, loadedFromCache = true)
drawerData = drawerData.copy(tags = tags.map { it.toView() }) drawerApiCalls(drawerData)
}
if (sources != null) {
drawerData = drawerData.copy(sources = sources.map { it.toView() })
}
handleDrawerData(drawerData, loadedFromCache = true)
drawerApiCalls(drawerData)
}
} }
} }
@ -881,8 +874,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
private fun filter(tags: String): Boolean { private fun filter(tags: String): Boolean {
val tagsList = tags.replace("\\s".toRegex(), "").split(",") val tagsList = tags.replace("\\s".toRegex(), "").split(",")
return tagsList.intersect(hiddenTags).isEmpty() return tagsList.intersect(hiddenTags).isEmpty()
} }
private fun doCallTo( private fun doCallTo(
@ -896,7 +889,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
if (shouldUpdate) { if (shouldUpdate) {
items = response.body() as ArrayList<Item> items = response.body() as ArrayList<Item>
items = items.filter { items = items.filter {
maybeTagFilter != null || filter(it.tags) maybeTagFilter != null || filter(it.tags)
} as ArrayList<Item> } as ArrayList<Item>
if (allItems.isEmpty()) { if (allItems.isEmpty()) {

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)