From 3f490c2487d2137c59c44ed78a7d2708d8ca4cbc Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Sun, 20 Feb 2022 02:19:06 +0100 Subject: [PATCH] Update: Extract Author, Screenshot and Donate into a file --- .../com/looker/droidify/entity/Product.kt | 22 ---- .../com/looker/droidify/entity/SubEntities.kt | 106 ++++++++++++++++++ .../com/looker/droidify/index/IndexHandler.kt | 34 +++--- .../looker/droidify/index/IndexV1Parser.kt | 35 +++--- .../looker/droidify/network/CoilDownloader.kt | 10 +- .../droidify/screen/ScreenshotsAdapter.kt | 8 +- .../droidify/screen/ScreenshotsFragment.kt | 6 +- .../droidify/ui/adapters/AppDetailAdapter.kt | 43 +++---- .../ui/fragments/AppDetailFragment.kt | 3 +- .../looker/droidify/ui/fragments/AppSheetX.kt | 3 +- 10 files changed, 175 insertions(+), 95 deletions(-) create mode 100644 src/main/kotlin/com/looker/droidify/entity/SubEntities.kt diff --git a/src/main/kotlin/com/looker/droidify/entity/Product.kt b/src/main/kotlin/com/looker/droidify/entity/Product.kt index b833e8f7..22621289 100644 --- a/src/main/kotlin/com/looker/droidify/entity/Product.kt +++ b/src/main/kotlin/com/looker/droidify/entity/Product.kt @@ -32,28 +32,6 @@ data class Product( val screenshots: List, val releases: List, ) { - data class Author(val name: String, val email: String, val web: String) - - sealed class Donate { - data class Regular(val url: String) : Donate() - data class Bitcoin(val address: String) : Donate() - data class Litecoin(val address: String) : Donate() - data class Flattr(val id: String) : Donate() - data class Liberapay(val id: String) : Donate() - data class OpenCollective(val id: String) : Donate() - } - - class Screenshot(val locale: String, val type: Type, val path: String) { - enum class Type(val jsonName: String) { - PHONE("phone"), - SMALL_TABLET("smallTablet"), - LARGE_TABLET("largeTablet") - } - - val identifier: String - get() = "$locale.${type.name}.$path" - } - // Same releases with different signatures val selectedReleases: List get() = releases.filter { it.selected } diff --git a/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt b/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt new file mode 100644 index 00000000..ddd88012 --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/entity/SubEntities.kt @@ -0,0 +1,106 @@ +package com.looker.droidify.entity + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.looker.droidify.utility.extension.json.forEachKey + +data class Author(val name: String, val email: String, val web: String) + +sealed class Donate { + data class Regular(val url: String) : Donate() + data class Bitcoin(val address: String) : Donate() + data class Litecoin(val address: String) : Donate() + data class Flattr(val id: String) : Donate() + data class Liberapay(val id: String) : Donate() + data class OpenCollective(val id: String) : Donate() + + fun serialize(generator: JsonGenerator) { + when (this) { + is Regular -> { + generator.writeStringField("type", "") + generator.writeStringField("url", url) + } + is Bitcoin -> { + generator.writeStringField("type", "bitcoin") + generator.writeStringField("address", address) + } + is Litecoin -> { + generator.writeStringField("type", "litecoin") + generator.writeStringField("address", address) + } + is Flattr -> { + generator.writeStringField("type", "flattr") + generator.writeStringField("id", id) + } + is Liberapay -> { + generator.writeStringField("type", "liberapay") + generator.writeStringField("id", id) + } + is OpenCollective -> { + generator.writeStringField("type", "openCollective") + generator.writeStringField("id", id) + } + }::class + } + + companion object { + fun deserialize(parser: JsonParser): Donate? { + var type = "" + var url = "" + var address = "" + var id = "" + parser.forEachKey { + when { + it.string("type") -> type = valueAsString + it.string("url") -> url = valueAsString + it.string("address") -> address = valueAsString + it.string("id") -> id = valueAsString + else -> skipChildren() + } + } + return when (type) { + "" -> Regular(url) + "bitcoin" -> Bitcoin(address) + "litecoin" -> Litecoin(address) + "flattr" -> Flattr(id) + "liberapay" -> Liberapay(id) + "openCollective" -> OpenCollective(id) + else -> null + } + } + } +} + +class Screenshot(val locale: String, val type: Type, val path: String) { + enum class Type(val jsonName: String) { + PHONE("phone"), + SMALL_TABLET("smallTablet"), + LARGE_TABLET("largeTablet") + } + + val identifier: String + get() = "$locale.${type.name}.$path" + + fun serialize(generator: JsonGenerator) { + generator.writeStringField("locale", locale) + generator.writeStringField("type", type.jsonName) + generator.writeStringField("path", path) + } + + companion object { + fun deserialize(parser: JsonParser): Screenshot? { + var locale = "" + var type = "" + var path = "" + parser.forEachKey { + when { + it.string("locale") -> locale = valueAsString + it.string("type") -> type = valueAsString + it.string("path") -> path = valueAsString + else -> skipChildren() + } + } + return Type.values().find { it.jsonName == type }?.let { Screenshot(locale, it, path) } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/looker/droidify/index/IndexHandler.kt b/src/main/kotlin/com/looker/droidify/index/IndexHandler.kt index 3969ea17..a354d39f 100644 --- a/src/main/kotlin/com/looker/droidify/index/IndexHandler.kt +++ b/src/main/kotlin/com/looker/droidify/index/IndexHandler.kt @@ -1,6 +1,8 @@ package com.looker.droidify.index import com.looker.droidify.database.entity.Release +import com.looker.droidify.entity.Author +import com.looker.droidify.entity.Donate import com.looker.droidify.entity.Product import com.looker.droidify.utility.extension.android.Android import org.xml.sax.Attributes @@ -36,17 +38,17 @@ class IndexHandler(private val repositoryId: Long, private val callback: Callbac fun onProduct(product: Product) } - internal object DonateComparator : Comparator { + internal object DonateComparator : Comparator { private val classes = listOf( - Product.Donate.Regular::class, - Product.Donate.Bitcoin::class, - Product.Donate.Litecoin::class, - Product.Donate.Flattr::class, - Product.Donate.Liberapay::class, - Product.Donate.OpenCollective::class + Donate.Regular::class, + Donate.Bitcoin::class, + Donate.Litecoin::class, + Donate.Flattr::class, + Donate.Liberapay::class, + Donate.OpenCollective::class ) - override fun compare(donate1: Product.Donate, donate2: Product.Donate): Int { + override fun compare(donate1: Donate, donate2: Donate): Int { val index1 = classes.indexOf(donate1::class) val index2 = classes.indexOf(donate2::class) return when { @@ -84,7 +86,7 @@ class IndexHandler(private val repositoryId: Long, private val callback: Callbac val categories = linkedSetOf() val antiFeatures = linkedSetOf() val licenses = mutableListOf() - val donates = mutableListOf() + val donates = mutableListOf() val releases = mutableListOf() fun build(): Product { @@ -97,7 +99,7 @@ class IndexHandler(private val repositoryId: Long, private val callback: Callbac "", icon, "", - Product.Author(authorName, authorEmail, ""), + Author(authorName, authorEmail, ""), source, changelog, web, @@ -318,12 +320,12 @@ class IndexHandler(private val repositoryId: Long, private val callback: Callbac .filter { it.isNotEmpty() } "license" -> productBuilder.licenses += content.split(',') .filter { it.isNotEmpty() } - "donate" -> productBuilder.donates += Product.Donate.Regular(content) - "bitcoin" -> productBuilder.donates += Product.Donate.Bitcoin(content) - "litecoin" -> productBuilder.donates += Product.Donate.Litecoin(content) - "flattr" -> productBuilder.donates += Product.Donate.Flattr(content) - "liberapay" -> productBuilder.donates += Product.Donate.Liberapay(content) - "openCollective" -> productBuilder.donates += Product.Donate.OpenCollective( + "donate" -> productBuilder.donates += Donate.Regular(content) + "bitcoin" -> productBuilder.donates += Donate.Bitcoin(content) + "litecoin" -> productBuilder.donates += Donate.Litecoin(content) + "flattr" -> productBuilder.donates += Donate.Flattr(content) + "liberapay" -> productBuilder.donates += Donate.Liberapay(content) + "openCollective" -> productBuilder.donates += Donate.OpenCollective( content ) } diff --git a/src/main/kotlin/com/looker/droidify/index/IndexV1Parser.kt b/src/main/kotlin/com/looker/droidify/index/IndexV1Parser.kt index 464f4b9f..56b76059 100644 --- a/src/main/kotlin/com/looker/droidify/index/IndexV1Parser.kt +++ b/src/main/kotlin/com/looker/droidify/index/IndexV1Parser.kt @@ -3,7 +3,10 @@ package com.looker.droidify.index import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.core.JsonToken import com.looker.droidify.database.entity.Release +import com.looker.droidify.entity.Author +import com.looker.droidify.entity.Donate import com.looker.droidify.entity.Product +import com.looker.droidify.entity.Screenshot import com.looker.droidify.utility.extension.android.Android import com.looker.droidify.utility.extension.json.* import com.looker.droidify.utility.extension.text.nullIfEmpty @@ -123,7 +126,7 @@ object IndexV1Parser { var categories = emptyList() var antiFeatures = emptyList() val licenses = mutableListOf() - val donates = mutableListOf() + val donates = mutableListOf() val localizedMap = mutableMapOf() forEachKey { it -> when { @@ -147,11 +150,11 @@ object IndexV1Parser { it.array("antiFeatures") -> antiFeatures = collectDistinctNotEmptyStrings() it.string("license") -> licenses += valueAsString.split(',') .filter { it.isNotEmpty() } - it.string("donate") -> donates += Product.Donate.Regular(valueAsString) - it.string("bitcoin") -> donates += Product.Donate.Bitcoin(valueAsString) - it.string("flattrID") -> donates += Product.Donate.Flattr(valueAsString) - it.string("liberapayID") -> donates += Product.Donate.Liberapay(valueAsString) - it.string("openCollective") -> donates += Product.Donate.OpenCollective( + it.string("donate") -> donates += Donate.Regular(valueAsString) + it.string("bitcoin") -> donates += Donate.Bitcoin(valueAsString) + it.string("flattrID") -> donates += Donate.Flattr(valueAsString) + it.string("liberapayID") -> donates += Donate.Liberapay(valueAsString) + it.string("openCollective") -> donates += Donate.OpenCollective( valueAsString ) it.dictionary("localized") -> forEachKey { keyToken -> @@ -206,23 +209,11 @@ object IndexV1Parser { val screenshots = screenshotPairs ?.let { (key, screenshots) -> screenshots.phone.asSequence() - .map { Product.Screenshot(key, Product.Screenshot.Type.PHONE, it) } + + .map { Screenshot(key, Screenshot.Type.PHONE, it) } + screenshots.smallTablet.asSequence() - .map { - Product.Screenshot( - key, - Product.Screenshot.Type.SMALL_TABLET, - it - ) - } + + .map { Screenshot(key, Screenshot.Type.SMALL_TABLET, it) } + screenshots.largeTablet.asSequence() - .map { - Product.Screenshot( - key, - Product.Screenshot.Type.LARGE_TABLET, - it - ) - } + .map { Screenshot(key, Screenshot.Type.LARGE_TABLET, it) } } .orEmpty().toList() return Product( @@ -234,7 +225,7 @@ object IndexV1Parser { whatsNew, icon, metadataIcon, - Product.Author(authorName, authorEmail, authorWeb), + Author(authorName, authorEmail, authorWeb), source, changelog, web, diff --git a/src/main/kotlin/com/looker/droidify/network/CoilDownloader.kt b/src/main/kotlin/com/looker/droidify/network/CoilDownloader.kt index 17ce009a..7f936736 100644 --- a/src/main/kotlin/com/looker/droidify/network/CoilDownloader.kt +++ b/src/main/kotlin/com/looker/droidify/network/CoilDownloader.kt @@ -4,7 +4,7 @@ import android.content.Context import android.net.Uri import android.view.View import com.looker.droidify.database.entity.Repository -import com.looker.droidify.entity.Product +import com.looker.droidify.entity.Screenshot import com.looker.droidify.utility.extension.text.nullIfEmpty import okhttp3.Cache import okhttp3.Call @@ -92,7 +92,7 @@ object CoilDownloader { fun createScreenshotUri( repository: Repository, packageName: String, - screenshot: Product.Screenshot, + screenshot: Screenshot, ): Uri { return Uri.Builder().scheme("https").authority(HOST_SCREENSHOT) .appendQueryParameter(QUERY_ADDRESS, repository.address) @@ -101,9 +101,9 @@ object CoilDownloader { .appendQueryParameter(QUERY_LOCALE, screenshot.locale) .appendQueryParameter( QUERY_DEVICE, when (screenshot.type) { - Product.Screenshot.Type.PHONE -> "phoneScreenshots" - Product.Screenshot.Type.SMALL_TABLET -> "sevenInchScreenshots" - Product.Screenshot.Type.LARGE_TABLET -> "tenInchScreenshots" + Screenshot.Type.PHONE -> "phoneScreenshots" + Screenshot.Type.SMALL_TABLET -> "sevenInchScreenshots" + Screenshot.Type.LARGE_TABLET -> "tenInchScreenshots" } ) .appendQueryParameter(QUERY_SCREENSHOT, screenshot.path) diff --git a/src/main/kotlin/com/looker/droidify/screen/ScreenshotsAdapter.kt b/src/main/kotlin/com/looker/droidify/screen/ScreenshotsAdapter.kt index 8964a7f8..37cd1e41 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ScreenshotsAdapter.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ScreenshotsAdapter.kt @@ -11,7 +11,7 @@ import com.google.android.material.card.MaterialCardView import com.google.android.material.imageview.ShapeableImageView import com.looker.droidify.R import com.looker.droidify.database.entity.Repository -import com.looker.droidify.entity.Product +import com.looker.droidify.entity.Screenshot import com.looker.droidify.graphics.PaddingDrawable import com.looker.droidify.network.CoilDownloader import com.looker.droidify.utility.extension.resources.getColorFromAttr @@ -19,7 +19,7 @@ import com.looker.droidify.utility.extension.resources.getDrawableCompat import com.looker.droidify.utility.extension.resources.sizeScaled import com.looker.droidify.widget.StableRecyclerAdapter -class ScreenshotsAdapter(private val onClick: (Product.Screenshot) -> Unit) : +class ScreenshotsAdapter(private val onClick: (Screenshot) -> Unit) : StableRecyclerAdapter() { enum class ViewType { SCREENSHOT } @@ -68,7 +68,7 @@ class ScreenshotsAdapter(private val onClick: (Product.Screenshot) -> Unit) : fun setScreenshots( repository: Repository, packageName: String, - screenshots: List + screenshots: List ) { items.clear() items += screenshots.map { Item.ScreenshotItem(repository, packageName, it) } @@ -132,7 +132,7 @@ class ScreenshotsAdapter(private val onClick: (Product.Screenshot) -> Unit) : class ScreenshotItem( val repository: Repository, val packageName: String, - val screenshot: Product.Screenshot, + val screenshot: Screenshot, ) : Item() { override val descriptor: String get() = "screenshot.${repository.id}.${screenshot.identifier}" diff --git a/src/main/kotlin/com/looker/droidify/screen/ScreenshotsFragment.kt b/src/main/kotlin/com/looker/droidify/screen/ScreenshotsFragment.kt index dc235390..32e9aad7 100644 --- a/src/main/kotlin/com/looker/droidify/screen/ScreenshotsFragment.kt +++ b/src/main/kotlin/com/looker/droidify/screen/ScreenshotsFragment.kt @@ -21,7 +21,7 @@ import com.google.android.material.imageview.ShapeableImageView import com.looker.droidify.R import com.looker.droidify.database.DatabaseX import com.looker.droidify.database.entity.Repository -import com.looker.droidify.entity.Product +import com.looker.droidify.entity.Screenshot import com.looker.droidify.graphics.PaddingDrawable import com.looker.droidify.network.CoilDownloader import com.looker.droidify.utility.RxUtils @@ -211,12 +211,12 @@ class ScreenshotsFragment() : DialogFragment() { } private var repository: Repository? = null - private var screenshots = emptyList() + private var screenshots = emptyList() fun update( viewPager: ViewPager2, repository: Repository?, - screenshots: List, + screenshots: List, ) { this.repository = repository this.screenshots = screenshots diff --git a/src/main/kotlin/com/looker/droidify/ui/adapters/AppDetailAdapter.kt b/src/main/kotlin/com/looker/droidify/ui/adapters/AppDetailAdapter.kt index 95e9684a..4e8fe56b 100644 --- a/src/main/kotlin/com/looker/droidify/ui/adapters/AppDetailAdapter.kt +++ b/src/main/kotlin/com/looker/droidify/ui/adapters/AppDetailAdapter.kt @@ -48,6 +48,7 @@ import com.looker.droidify.database.entity.Release import com.looker.droidify.database.entity.Repository import com.looker.droidify.entity.Product import com.looker.droidify.entity.ProductPreference +import com.looker.droidify.entity.Screenshot import com.looker.droidify.network.CoilDownloader import com.looker.droidify.screen.ScreenshotsAdapter import com.looker.droidify.utility.KParcelable @@ -76,7 +77,7 @@ class AppDetailAdapter(private val callbacks: Callbacks) : fun onActionClick(action: Action) fun onPreferenceChanged(preference: ProductPreference) fun onPermissionsClick(group: String?, permissions: List) - fun onScreenshotClick(screenshot: Product.Screenshot) + fun onScreenshotClick(screenshot: Screenshot) fun onReleaseClick(release: Release) fun onUriClick(uri: Uri, shouldConfirm: Boolean): Boolean } @@ -150,7 +151,7 @@ class AppDetailAdapter(private val callbacks: Callbacks) : } class ScreenshotItem( - val screenshots: List, + val screenshots: List, val packageName: String, val repository: Repository, ) : Item() { @@ -234,36 +235,36 @@ class AppDetailAdapter(private val callbacks: Callbacks) : } } - class Donate(val donate: Product.Donate) : LinkItem() { + class Donate(val donate: com.looker.droidify.entity.Donate) : LinkItem() { override val descriptor: String get() = "link.donate.$donate" override val iconResId: Int get() = when (donate) { - is Product.Donate.Regular -> R.drawable.ic_donate_regular - is Product.Donate.Bitcoin -> R.drawable.ic_donate_bitcoin - is Product.Donate.Litecoin -> R.drawable.ic_donate_litecoin - is Product.Donate.Flattr -> R.drawable.ic_donate_flattr - is Product.Donate.Liberapay -> R.drawable.ic_donate_liberapay - is Product.Donate.OpenCollective -> R.drawable.ic_donate_opencollective + is com.looker.droidify.entity.Donate.Regular -> R.drawable.ic_donate_regular + is com.looker.droidify.entity.Donate.Bitcoin -> R.drawable.ic_donate_bitcoin + is com.looker.droidify.entity.Donate.Litecoin -> R.drawable.ic_donate_litecoin + is com.looker.droidify.entity.Donate.Flattr -> R.drawable.ic_donate_flattr + is com.looker.droidify.entity.Donate.Liberapay -> R.drawable.ic_donate_liberapay + is com.looker.droidify.entity.Donate.OpenCollective -> R.drawable.ic_donate_opencollective } override fun getTitle(context: Context): String = when (donate) { - is Product.Donate.Regular -> context.getString(R.string.website) - is Product.Donate.Bitcoin -> "Bitcoin" - is Product.Donate.Litecoin -> "Litecoin" - is Product.Donate.Flattr -> "Flattr" - is Product.Donate.Liberapay -> "Liberapay" - is Product.Donate.OpenCollective -> "Open Collective" + is com.looker.droidify.entity.Donate.Regular -> context.getString(R.string.website) + is com.looker.droidify.entity.Donate.Bitcoin -> "Bitcoin" + is com.looker.droidify.entity.Donate.Litecoin -> "Litecoin" + is com.looker.droidify.entity.Donate.Flattr -> "Flattr" + is com.looker.droidify.entity.Donate.Liberapay -> "Liberapay" + is com.looker.droidify.entity.Donate.OpenCollective -> "Open Collective" } override val uri: Uri? = when (donate) { - is Product.Donate.Regular -> Uri.parse(donate.url) - is Product.Donate.Bitcoin -> Uri.parse("bitcoin:${donate.address}") - is Product.Donate.Litecoin -> Uri.parse("litecoin:${donate.address}") - is Product.Donate.Flattr -> Uri.parse("https://flattr.com/thing/${donate.id}") - is Product.Donate.Liberapay -> Uri.parse("https://liberapay.com/~${donate.id}") - is Product.Donate.OpenCollective -> Uri.parse("https://opencollective.com/${donate.id}") + is com.looker.droidify.entity.Donate.Regular -> Uri.parse(donate.url) + is com.looker.droidify.entity.Donate.Bitcoin -> Uri.parse("bitcoin:${donate.address}") + is com.looker.droidify.entity.Donate.Litecoin -> Uri.parse("litecoin:${donate.address}") + is com.looker.droidify.entity.Donate.Flattr -> Uri.parse("https://flattr.com/thing/${donate.id}") + is com.looker.droidify.entity.Donate.Liberapay -> Uri.parse("https://liberapay.com/~${donate.id}") + is com.looker.droidify.entity.Donate.OpenCollective -> Uri.parse("https://opencollective.com/${donate.id}") } } } diff --git a/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt b/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt index 59dc50bb..48b52389 100644 --- a/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt +++ b/src/main/kotlin/com/looker/droidify/ui/fragments/AppDetailFragment.kt @@ -21,6 +21,7 @@ import com.looker.droidify.database.entity.Release import com.looker.droidify.database.entity.Repository import com.looker.droidify.entity.Product import com.looker.droidify.entity.ProductPreference +import com.looker.droidify.entity.Screenshot import com.looker.droidify.installer.AppInstaller import com.looker.droidify.screen.MessageDialog import com.looker.droidify.screen.ScreenFragment @@ -507,7 +508,7 @@ class AppDetailFragment() : ScreenFragment(), AppDetailAdapter.Callbacks { ) } - override fun onScreenshotClick(screenshot: Product.Screenshot) { + override fun onScreenshotClick(screenshot: Screenshot) { val pair = products.asSequence() .map { it -> Pair( diff --git a/src/main/kotlin/com/looker/droidify/ui/fragments/AppSheetX.kt b/src/main/kotlin/com/looker/droidify/ui/fragments/AppSheetX.kt index 6d3a0c18..f3360f4a 100644 --- a/src/main/kotlin/com/looker/droidify/ui/fragments/AppSheetX.kt +++ b/src/main/kotlin/com/looker/droidify/ui/fragments/AppSheetX.kt @@ -23,6 +23,7 @@ import com.looker.droidify.database.entity.Repository import com.looker.droidify.databinding.SheetAppXBinding import com.looker.droidify.entity.Product import com.looker.droidify.entity.ProductPreference +import com.looker.droidify.entity.Screenshot import com.looker.droidify.installer.AppInstaller import com.looker.droidify.screen.MessageDialog import com.looker.droidify.screen.ScreenshotsFragment @@ -385,7 +386,7 @@ class AppSheetX() : FullscreenBottomSheetDialogFragment(), AppDetailAdapter.Call ) } - override fun onScreenshotClick(screenshot: Product.Screenshot) { + override fun onScreenshotClick(screenshot: Screenshot) { val pair = productRepos.asSequence() .map { it -> Pair(