Updating lateral panel tags without network call.

This commit is contained in:
Amine Bou 2018-04-07 18:19:24 +02:00
parent 67a30b92f6
commit db89fe5aad
2 changed files with 49 additions and 9 deletions

View File

@ -66,6 +66,7 @@ import com.mikepenz.aboutlibraries.Libs
import com.mikepenz.aboutlibraries.LibsBuilder import com.mikepenz.aboutlibraries.LibsBuilder
import com.mikepenz.materialdrawer.Drawer import com.mikepenz.materialdrawer.Drawer
import com.mikepenz.materialdrawer.holder.BadgeStyle import com.mikepenz.materialdrawer.holder.BadgeStyle
import com.mikepenz.materialdrawer.holder.StringHolder
import com.mikepenz.materialdrawer.model.DividerDrawerItem import com.mikepenz.materialdrawer.model.DividerDrawerItem
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem import com.mikepenz.materialdrawer.model.PrimaryDrawerItem
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem import com.mikepenz.materialdrawer.model.SecondaryDrawerItem
@ -128,6 +129,9 @@ 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>
data class DrawerData(val tags: List<Tag>?, val sources: List<Sources>?) data class DrawerData(val tags: List<Tag>?, val sources: List<Sources>?)
override fun onStart() { override fun onStart() {
@ -220,6 +224,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
if (items.size > 0) { if (items.size > 0) {
badgeNew-- badgeNew--
reloadBadgeContent() reloadBadgeContent()
tagsBadge = tagsBadge.map {
if (it.key == i.tags.longHash()) {
(it.key to (it.value - 1))
} else {
(it.key to it.value)
}
}.toMap()
reloadTagsBadges()
} else { } else {
tabNewBadge.hide() tabNewBadge.hide()
} }
@ -417,6 +430,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
private fun handleDrawerItems() { private fun handleDrawerItems() {
tagsBadge = emptyMap()
fun handleDrawerData(maybeDrawerData: DrawerData?, loadedFromCache: Boolean = false) { fun handleDrawerData(maybeDrawerData: DrawerData?, loadedFromCache: Boolean = false) {
fun handleTags(maybeTags: List<Tag>?) { fun handleTags(maybeTags: List<Tag>?) {
if (maybeTags == null) { if (maybeTags == null) {
@ -428,10 +442,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
) )
} }
} else { } else {
for (tag in maybeTags) { tagsBadge = maybeTags.map {
val gd = GradientDrawable() val gd = GradientDrawable()
val color = try { val color = try {
Color.parseColor(tag.color) Color.parseColor(it.color)
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
resources.getColor(R.color.primary) resources.getColor(R.color.primary)
} }
@ -442,22 +456,24 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
gd.cornerRadius = 30F gd.cornerRadius = 30F
drawer.addItem( drawer.addItem(
PrimaryDrawerItem() PrimaryDrawerItem()
.withName(tag.tag) .withName(it.tag)
.withIdentifier(tag.tag.longHash()) .withIdentifier(it.tag.longHash())
.withIcon(gd) .withIcon(gd)
.withBadge("${tag.unread}") .withBadge("${it.unread}")
.withBadgeStyle( .withBadgeStyle(
BadgeStyle().withTextColor(Color.WHITE) BadgeStyle().withTextColor(Color.WHITE)
.withColor(appColors.accent) .withColor(appColors.accent)
) )
.withOnDrawerItemClickListener { _, _, _ -> .withOnDrawerItemClickListener { _, _, _ ->
allItems = ArrayList() allItems = ArrayList()
maybeTagFilter = tag maybeTagFilter = it
getElementsAccordingToTab() getElementsAccordingToTab()
false false
} }
) )
}
(it.tag.longHash() to it.unread)
}.toMap()
} }
} }
@ -963,6 +979,13 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
} }
private fun reloadTagsBadges() {
tagsBadge.forEach {
drawer.updateBadge(it.key, StringHolder("${it.value}"))
}
drawer.resetDrawerContent()
}
private fun calculateNoOfColumns(): Int { private fun calculateNoOfColumns(): Int {
val displayMetrics = resources.displayMetrics val displayMetrics = resources.displayMetrics
val dpWidth = displayMetrics.widthPixels / displayMetrics.density val dpWidth = displayMetrics.widthPixels / displayMetrics.density
@ -1054,6 +1077,11 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
needsConfirmation(R.string.readAll, R.string.markall_dialog_message, { needsConfirmation(R.string.readAll, R.string.markall_dialog_message, {
swipeRefreshLayout.isRefreshing = false swipeRefreshLayout.isRefreshing = false
val ids = allItems.map { it.id } val ids = allItems.map { it.id }
val itemsByTag: Map<Long, Int> =
allItems
.groupBy { it.tags.longHash() }
.map { it.key to it.value.size }
.toMap()
fun readAllDebug(e: Throwable) { fun readAllDebug(e: Throwable) {
Crashlytics.setUserIdentifier(userIdentifier) Crashlytics.setUserIdentifier(userIdentifier)
@ -1074,6 +1102,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
tabNewBadge.removeBadge() tabNewBadge.removeBadge()
tagsBadge = itemsByTag.map {
(it.key to ((tagsBadge[it.key] ?: it.value) - it.value))
}.toMap()
reloadTagsBadges()
getElementsAccordingToTab()
} else { } else {
Toast.makeText( Toast.makeText(
this@HomeActivity, this@HomeActivity,

View File

@ -70,7 +70,8 @@ data class Item(
@SerializedName("thumbnail") val thumbnail: String, @SerializedName("thumbnail") val thumbnail: String,
@SerializedName("icon") val icon: String, @SerializedName("icon") val icon: String,
@SerializedName("link") val link: String, @SerializedName("link") val link: String,
@SerializedName("sourcetitle") val sourcetitle: String @SerializedName("sourcetitle") val sourcetitle: String,
@SerializedName("tags") val tags: String
) : Parcelable { ) : Parcelable {
var config: Config? = null var config: Config? = null
@ -92,7 +93,8 @@ data class Item(
thumbnail = source.readString(), thumbnail = source.readString(),
icon = source.readString(), icon = source.readString(),
link = source.readString(), link = source.readString(),
sourcetitle = source.readString() sourcetitle = source.readString(),
tags = source.readString()
) )
override fun describeContents() = 0 override fun describeContents() = 0
@ -108,6 +110,7 @@ data class Item(
dest.writeString(icon) dest.writeString(icon)
dest.writeString(link) dest.writeString(link)
dest.writeString(sourcetitle) dest.writeString(sourcetitle)
dest.writeString(tags)
} }
fun getIcon(app: Context): String { fun getIcon(app: Context): String {