This commit is contained in:
Amine 2018-11-05 20:29:38 +01:00
parent 1aa68d3449
commit 40d9c97f73
6 changed files with 60 additions and 12 deletions

View File

@ -248,7 +248,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
badgeNew-- badgeNew--
reloadBadgeContent() reloadBadgeContent()
val tagHashes = i.tags.split(",").map { it.longHash() } val tagHashes = i.tags.tags.split(",").map { it.longHash() }
tagsBadge = tagsBadge.map { tagsBadge = tagsBadge.map {
if (tagHashes.contains(it.key)) { if (tagHashes.contains(it.key)) {
(it.key to (it.value - 1)) (it.key to (it.value - 1))
@ -367,7 +367,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
thread { thread {
if (response.body() != null) { if (response.body() != null) {
val apiItems = (response.body() as ArrayList<Item>).filter { val apiItems = (response.body() as ArrayList<Item>).filter {
maybeTagFilter != null || filter(it.tags) maybeTagFilter != null || filter(it.tags.tags)
} as ArrayList<Item> } as ArrayList<Item>
db.itemsDao().deleteAllItems() db.itemsDao().deleteAllItems()
db.itemsDao() db.itemsDao()
@ -999,7 +999,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
getAndStoreAllItems() getAndStoreAllItems()
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.tags)
} as ArrayList<Item> } as ArrayList<Item>
if (allItems.isEmpty()) { if (allItems.isEmpty()) {
@ -1299,7 +1299,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
val ids = allItems.map { it.id } val ids = allItems.map { it.id }
val itemsByTag: Map<Long, Int> = val itemsByTag: Map<Long, Int> =
allItems.flattenTags() allItems.flattenTags()
.groupBy { it.tags.longHash() } .groupBy { it.tags.tags.longHash() }
.map { it.key to it.value.size } .map { it.key to it.value.size }
.toMap() .toMap()

View File

@ -66,6 +66,7 @@ class SelfossApi(
val gson = val gson =
GsonBuilder() GsonBuilder()
.registerTypeAdapter(Boolean::class.javaPrimitiveType, BooleanTypeAdapter()) .registerTypeAdapter(Boolean::class.javaPrimitiveType, BooleanTypeAdapter())
.registerTypeAdapter(SelfossTagType::class.java, SelfossTagTypeTypeAdapter())
.setLenient() .setLenient()
.create() .create()

View File

@ -71,7 +71,7 @@ data class Item(
@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 @SerializedName("tags") val tags: SelfossTagType
) : Parcelable { ) : Parcelable {
var config: Config? = null var config: Config? = null
@ -94,7 +94,7 @@ data class Item(
icon = source.readString(), icon = source.readString(),
link = source.readString(), link = source.readString(),
sourcetitle = source.readString(), sourcetitle = source.readString(),
tags = source.readString() tags = source.readParcelable(ClassLoader.getSystemClassLoader())
) )
override fun describeContents() = 0 override fun describeContents() = 0
@ -110,7 +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) dest.writeParcelable(tags, flags)
} }
fun getIcon(app: Context): String { fun getIcon(app: Context): String {
@ -154,3 +154,26 @@ data class Item(
return stringUrl return stringUrl
} }
} }
data class SelfossTagType(val tags: String) : Parcelable {
companion object {
@JvmField val CREATOR: Parcelable.Creator<SelfossTagType> =
object : Parcelable.Creator<SelfossTagType> {
override fun createFromParcel(source: Parcel): SelfossTagType =
SelfossTagType(source)
override fun newArray(size: Int): Array<SelfossTagType?> = arrayOfNulls(size)
}
}
constructor(source: Parcel) : this(
tags = source.readString()
)
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(tags)
}
}

View File

@ -0,0 +1,22 @@
package apps.amine.bou.readerforselfoss.api.selfoss
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonParseException
import java.lang.reflect.Type
internal class SelfossTagTypeTypeAdapter : JsonDeserializer<SelfossTagType> {
@Throws(JsonParseException::class)
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): SelfossTagType? =
if (json.isJsonArray) {
SelfossTagType(json.asJsonArray.joinToString(",") { it.toString() })
} else {
SelfossTagType(json.toString())
}
}

View File

@ -3,6 +3,7 @@ package apps.amine.bou.readerforselfoss.utils
import android.content.Context import android.content.Context
import android.text.format.DateUtils import android.text.format.DateUtils
import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.Item
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossTagType
import org.acra.ACRA import org.acra.ACRA
import java.text.ParseException import java.text.ParseException
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -44,8 +45,8 @@ fun Item.toggleStar(): Item {
fun List<Item>.flattenTags(): List<Item> = fun List<Item>.flattenTags(): List<Item> =
this.flatMap { this.flatMap {
val item = it val item = it
val tags: List<String> = it.tags.split(",") val tags: List<String> = it.tags.tags.split(",")
tags.map { tags.map { t ->
item.copy(tags = it.trim()) item.copy(tags = SelfossTagType(t.trim()))
} }
} }

View File

@ -1,6 +1,7 @@
package apps.amine.bou.readerforselfoss.utils.persistence package apps.amine.bou.readerforselfoss.utils.persistence
import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.Item
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossTagType
import apps.amine.bou.readerforselfoss.api.selfoss.Source import apps.amine.bou.readerforselfoss.api.selfoss.Source
import apps.amine.bou.readerforselfoss.api.selfoss.Tag import apps.amine.bou.readerforselfoss.api.selfoss.Tag
import apps.amine.bou.readerforselfoss.persistence.entities.ItemEntity import apps.amine.bou.readerforselfoss.persistence.entities.ItemEntity
@ -53,7 +54,7 @@ fun ItemEntity.toView(): Item =
this.icon, this.icon,
this.link, this.link,
this.sourcetitle, this.sourcetitle,
this.tags SelfossTagType(this.tags)
) )
fun Item.toEntity(): ItemEntity = fun Item.toEntity(): ItemEntity =
@ -68,5 +69,5 @@ fun Item.toEntity(): ItemEntity =
this.icon, this.icon,
this.link, this.link,
this.sourcetitle, this.sourcetitle,
this.tags this.tags.tags
) )