From 40d9c97f735fb4846075b8be364bfdbe2ccaf249 Mon Sep 17 00:00:00 2001 From: Amine Date: Mon, 5 Nov 2018 20:29:38 +0100 Subject: [PATCH] Fixes #216. --- .../bou/readerforselfoss/HomeActivity.kt | 8 ++--- .../api/selfoss/SelfossApi.kt | 1 + .../api/selfoss/SelfossModels.kt | 29 +++++++++++++++++-- .../api/selfoss/SelfossTagTypeTypeAdapter.kt | 22 ++++++++++++++ .../bou/readerforselfoss/utils/ItemsUtils.kt | 7 +++-- .../utils/persistence/EntitiesUtils.kt | 5 ++-- 6 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossTagTypeTypeAdapter.kt diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index dcca697..e667cdc 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -248,7 +248,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { badgeNew-- reloadBadgeContent() - val tagHashes = i.tags.split(",").map { it.longHash() } + val tagHashes = i.tags.tags.split(",").map { it.longHash() } tagsBadge = tagsBadge.map { if (tagHashes.contains(it.key)) { (it.key to (it.value - 1)) @@ -367,7 +367,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { thread { if (response.body() != null) { val apiItems = (response.body() as ArrayList).filter { - maybeTagFilter != null || filter(it.tags) + maybeTagFilter != null || filter(it.tags.tags) } as ArrayList db.itemsDao().deleteAllItems() db.itemsDao() @@ -999,7 +999,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { getAndStoreAllItems() items = response.body() as ArrayList items = items.filter { - maybeTagFilter != null || filter(it.tags) + maybeTagFilter != null || filter(it.tags.tags) } as ArrayList if (allItems.isEmpty()) { @@ -1299,7 +1299,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { val ids = allItems.map { it.id } val itemsByTag: Map = allItems.flattenTags() - .groupBy { it.tags.longHash() } + .groupBy { it.tags.tags.longHash() } .map { it.key to it.value.size } .toMap() diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt index b361eb3..59c2a86 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossApi.kt @@ -66,6 +66,7 @@ class SelfossApi( val gson = GsonBuilder() .registerTypeAdapter(Boolean::class.javaPrimitiveType, BooleanTypeAdapter()) + .registerTypeAdapter(SelfossTagType::class.java, SelfossTagTypeTypeAdapter()) .setLenient() .create() diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt index edb8935..78cee13 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossModels.kt @@ -71,7 +71,7 @@ data class Item( @SerializedName("icon") val icon: String, @SerializedName("link") val link: String, @SerializedName("sourcetitle") val sourcetitle: String, - @SerializedName("tags") val tags: String + @SerializedName("tags") val tags: SelfossTagType ) : Parcelable { var config: Config? = null @@ -94,7 +94,7 @@ data class Item( icon = source.readString(), link = source.readString(), sourcetitle = source.readString(), - tags = source.readString() + tags = source.readParcelable(ClassLoader.getSystemClassLoader()) ) override fun describeContents() = 0 @@ -110,7 +110,7 @@ data class Item( dest.writeString(icon) dest.writeString(link) dest.writeString(sourcetitle) - dest.writeString(tags) + dest.writeParcelable(tags, flags) } fun getIcon(app: Context): String { @@ -153,4 +153,27 @@ data class Item( return stringUrl } +} + +data class SelfossTagType(val tags: String) : Parcelable { + + companion object { + @JvmField val CREATOR: Parcelable.Creator = + object : Parcelable.Creator { + override fun createFromParcel(source: Parcel): SelfossTagType = + SelfossTagType(source) + + override fun newArray(size: Int): Array = arrayOfNulls(size) + } + } + + constructor(source: Parcel) : this( + tags = source.readString() + ) + + override fun describeContents() = 0 + + override fun writeToParcel(dest: Parcel, flags: Int) { + dest.writeString(tags) + } } \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossTagTypeTypeAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossTagTypeTypeAdapter.kt new file mode 100644 index 0000000..4e9c3cb --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/api/selfoss/SelfossTagTypeTypeAdapter.kt @@ -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 { + + @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()) + } +} diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt index 348147d..5180169 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt @@ -3,6 +3,7 @@ package apps.amine.bou.readerforselfoss.utils import android.content.Context import android.text.format.DateUtils import apps.amine.bou.readerforselfoss.api.selfoss.Item +import apps.amine.bou.readerforselfoss.api.selfoss.SelfossTagType import org.acra.ACRA import java.text.ParseException import java.text.SimpleDateFormat @@ -44,8 +45,8 @@ fun Item.toggleStar(): Item { fun List.flattenTags(): List = this.flatMap { val item = it - val tags: List = it.tags.split(",") - tags.map { - item.copy(tags = it.trim()) + val tags: List = it.tags.tags.split(",") + tags.map { t -> + item.copy(tags = SelfossTagType(t.trim())) } } \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/persistence/EntitiesUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/persistence/EntitiesUtils.kt index 2cad189..31406e9 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/persistence/EntitiesUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/persistence/EntitiesUtils.kt @@ -1,6 +1,7 @@ package apps.amine.bou.readerforselfoss.utils.persistence 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.Tag import apps.amine.bou.readerforselfoss.persistence.entities.ItemEntity @@ -53,7 +54,7 @@ fun ItemEntity.toView(): Item = this.icon, this.link, this.sourcetitle, - this.tags + SelfossTagType(this.tags) ) fun Item.toEntity(): ItemEntity = @@ -68,5 +69,5 @@ fun Item.toEntity(): ItemEntity = this.icon, this.link, this.sourcetitle, - this.tags + this.tags.tags ) \ No newline at end of file