From f2874e53693bd1ed62f3370a024d05c0d9814d0e Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Mon, 2 Dec 2019 16:02:16 +0000 Subject: [PATCH] #1574 New standard tags should be interpreted properly - add models --- .../library_view/adapter/KiwixTag.kt | 63 +++++++++++++++++++ .../library_view/adapter/LibraryListItem.kt | 1 + 2 files changed, 64 insertions(+) create mode 100644 app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/KiwixTag.kt diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/KiwixTag.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/KiwixTag.kt new file mode 100644 index 000000000..de930214b --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/KiwixTag.kt @@ -0,0 +1,63 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.zim_manager.library_view.adapter + +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.KiwixTag.Companion.YesNoValueTag.DetailsTag +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.KiwixTag.Companion.YesNoValueTag.FtIndexTag +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.KiwixTag.Companion.YesNoValueTag.PicturesTag +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.KiwixTag.Companion.YesNoValueTag.VideoTag +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.KiwixTag.TagValue.NO +import org.kiwix.kiwixmobile.zim_manager.library_view.adapter.KiwixTag.TagValue.YES + +sealed class KiwixTag { + companion object { + fun from(tagString: String?) = tagString?.split(";") + ?.map { tags -> + val split = tags.split(":") + val value = split.getOrNull(1) + when (val tag = split[0]) { + "_ftindex" -> FtIndexTag(value!!) + "_pictures" -> PicturesTag(value!!) + "_video" -> VideoTag(value!!) + "_details" -> DetailsTag(value!!) + "_category" -> CategoryTag(value!!) + else -> value?.let { ArbitraryTag(tag, it) } ?: TagOnly(tag) + } + } ?: emptyList() + + data class CategoryTag(val categoryValue: String) : KiwixTag() + data class ArbitraryTag(val tag: String, val value: String) : KiwixTag() + data class TagOnly(val tag: String) : KiwixTag() + + sealed class YesNoValueTag( + _value: String, + val value: TagValue = if (_value == "yes") YES else NO + ) : KiwixTag() { + class FtIndexTag(value: String) : YesNoValueTag(value) + class PicturesTag(value: String) : YesNoValueTag(value) + class VideoTag(value: String) : YesNoValueTag(value) + class DetailsTag(value: String) : YesNoValueTag(value) + } + } + + enum class TagValue { + YES, + NO + } +} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt index a39b2b296..6882816c3 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/zim_manager/library_view/adapter/LibraryListItem.kt @@ -30,6 +30,7 @@ sealed class LibraryListItem { data class BookItem( val book: Book, + val tags: List = KiwixTag.from(book.tags), override val id: Long = book.id.hashCode().toLong() ) : LibraryListItem() }