Skip to content

Commit

Permalink
refactor: switch to kotlinx serialization (closes #184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed May 2, 2024
1 parent 607925e commit 9857d14
Show file tree
Hide file tree
Showing 43 changed files with 187 additions and 153 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("kotlinx-serialization")
}

android {
Expand Down Expand Up @@ -90,7 +91,8 @@ dependencies {

// Retrofit
implementation("com.squareup.retrofit2:retrofit:2.11.0")
implementation("com.squareup.retrofit2:converter-jackson:2.11.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.1")
implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")

// Coil
Expand Down
25 changes: 24 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,27 @@
#-renamesourcefileattribute SourceFile

-keep class com.bnyro.wallpaper.obj.** { *; }
-keep class com.bnyro.wallpaper.api.**.** { *; }
-keep class com.bnyro.wallpaper.api.**.** { *; }

# Keep rules required by Kotlinx Serialization
-if @kotlinx.serialization.Serializable class **
-keepclassmembers class <1> {
static <1>$Companion Companion;
}

-if @kotlinx.serialization.Serializable class ** {
static **$* *;
}
-keepclassmembers class <2>$<3> {
kotlinx.serialization.KSerializer serializer(...);
}

-if @kotlinx.serialization.Serializable class ** {
public static ** INSTANCE;
}
-keepclassmembers class <1> {
public static <1> INSTANCE;
kotlinx.serialization.KSerializer serializer(...);
}

-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class Api {
abstract suspend fun getRandomWallpaperUrl(): String?

fun getPref(key: String, defValue: String): String {
return Preferences.getString(this.name + key, defValue) ?: defValue
return Preferences.getString(this.name + key, defValue)
}

fun setPref(key: String, value: String) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/api/bi/BiApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.bnyro.wallpaper.api.bi

import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitBuilder
import com.bnyro.wallpaper.util.RetrofitHelper

class BiApi : Api() {
override val name: String = "Bing"
Expand All @@ -12,7 +12,7 @@ class BiApi : Api() {
)
private val previewResolution = "1366x768"

val api = RetrofitBuilder.create(baseUrl, Bing::class.java)
val api = RetrofitHelper.create(baseUrl, Bing::class.java)

private fun getImgSrc(path: String, resolution: String): String {
return "$baseUrl${path}_${resolution}.jpg"
Expand Down
34 changes: 17 additions & 17 deletions app/src/main/java/com/bnyro/wallpaper/api/bi/obj/BingImage.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.bnyro.wallpaper.api.bi.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class BingImage(
@JsonProperty("bot") val bot: Int? = null,
@JsonProperty("copyright") val copyright: String? = null,
@JsonProperty("copyrightlink") val copyrightLink: String? = null,
@JsonProperty("drk") val drk: Int? = null,
@JsonProperty("enddate") val endDate: String? = null,
@JsonProperty("fullstartdate") val fullStartDate: String? = null,
@JsonProperty("hsh") val hsh: String? = null,
@JsonProperty("quiz") val quiz: String? = null,
@JsonProperty("startdate") val startDate: String? = null,
@JsonProperty("title") val title: String = "",
@JsonProperty("top") val top: Int? = null,
@JsonProperty("url") val url: String = "",
@JsonProperty("urlbase") val urlBase: String = "",
@JsonProperty("wp") val wp: Boolean = false
@SerialName("bot") val bot: Int? = null,
@SerialName("copyright") val copyright: String? = null,
@SerialName("copyrightlink") val copyrightLink: String? = null,
@SerialName("drk") val drk: Int? = null,
@SerialName("enddate") val endDate: String? = null,
@SerialName("fullstartdate") val fullStartDate: String? = null,
@SerialName("hsh") val hsh: String? = null,
@SerialName("quiz") val quiz: String? = null,
@SerialName("startdate") val startDate: String? = null,
@SerialName("title") val title: String = "",
@SerialName("top") val top: Int? = null,
@SerialName("url") val url: String = "",
@SerialName("urlbase") val urlBase: String = "",
@SerialName("wp") val wp: Boolean = false
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bnyro.wallpaper.api.bi.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class BingImageResponse(
val images: List<BingImage> = emptyList()
)
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/api/le/LeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.bnyro.wallpaper.api.le

import com.bnyro.wallpaper.api.CommunityApi
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitBuilder
import com.bnyro.wallpaper.util.RetrofitHelper

class LeApi : CommunityApi() {
override val name: String = "Lemmy"
Expand Down Expand Up @@ -32,7 +32,7 @@ class LeApi : CommunityApi() {

override val defaultCommunityName: String = "[email protected]"

private val api = RetrofitBuilder.create(baseUrl, Lemmy::class.java)
private val api = RetrofitHelper.create(baseUrl, Lemmy::class.java)

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
return api.getWallpapers(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bnyro.wallpaper.api.le.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class LemmyCreator(
@JsonProperty("name") val name: String = "",
@SerialName("name") val name: String = "",
)
14 changes: 7 additions & 7 deletions app/src/main/java/com/bnyro/wallpaper/api/le/obj/LemmyPost.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.bnyro.wallpaper.api.le.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class LemmyPost(
@JsonProperty("ap_id") val postUrl: String = "",
@JsonProperty("name") val name: String = "",
@JsonProperty("published") val published: String = "",
@JsonProperty("thumbnail_url") val thumbnailUrl: String? = null,
@SerialName("ap_id") val postUrl: String = "",
@SerialName("name") val name: String = "",
@SerialName("published") val published: String = "",
@SerialName("thumbnail_url") val thumbnailUrl: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.bnyro.wallpaper.api.le.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class LemmyPostView(
@JsonProperty("creator") val creator: LemmyCreator = LemmyCreator(),
@JsonProperty("post") val post: LemmyPost = LemmyPost(),
@SerialName("creator") val creator: LemmyCreator = LemmyCreator(),
@SerialName("post") val post: LemmyPost = LemmyPost(),
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.bnyro.wallpaper.api.le.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class LemmyResponse(
val posts: List<LemmyPostView> = emptyList()
)
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/ow/OWalls.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.bnyro.wallpaper.api.ow

import kotlinx.serialization.json.JsonObject
import retrofit2.http.GET

interface OWalls {
@GET("tanujnotes/bf400a269746c5c124a599af040dd82e/raw")
suspend fun getAll(): Any
suspend fun getAll(): JsonObject
}
22 changes: 8 additions & 14 deletions app/src/main/java/com/bnyro/wallpaper/api/ow/OwApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package com.bnyro.wallpaper.api.ow

import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.ext.toJsonObject
import com.bnyro.wallpaper.util.RetrofitBuilder
import com.bnyro.wallpaper.util.RetrofitHelper
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive

class OwApi() : Api() {
class OwApi : Api() {
override val name: String = "OWalls"
override val baseUrl: String = "https://gist.github.com/"
override val filters: Map<String, List<String>> = mapOf(
"style" to listOf("all", "light", "dark")
)

private val api = RetrofitBuilder.create(baseUrl, OWalls::class.java)
private val api = RetrofitHelper.create(baseUrl, OWalls::class.java)
private val resultsPerPage = 20

private var wallpapers: MutableList<Wallpaper> = mutableListOf()
Expand All @@ -36,16 +37,9 @@ class OwApi() : Api() {
}

private suspend fun loadAll() {
val response = api.getAll().toJsonObject()
response.fields().forEach { timePair ->
timePair.value.fields().forEach {
wallpapers.add(
0,
Wallpaper(
imgSrc = it.value.textValue(),
category = it.key
)
)
api.getAll().forEach { _, images ->
images.jsonObject.forEach { category, source ->
wallpapers.add(0, Wallpaper(imgSrc = source.jsonPrimitive.content, category = category))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/api/ps/PsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.bnyro.wallpaper.api.ps

import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitBuilder
import com.bnyro.wallpaper.util.RetrofitHelper

class PsApi : Api() {
override val name: String = "Picsum"
override val baseUrl: String = "https://picsum.photos"

private val api = RetrofitBuilder.create(baseUrl, Picsum::class.java)
private val api = RetrofitHelper.create(baseUrl, Picsum::class.java)

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
return api.getWallpapers(page).map {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/ps/obj/PsImage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.bnyro.wallpaper.api.ps.obj

import kotlinx.serialization.Serializable

@Serializable
data class PsImage(
val author: String? = null,
val download_url: String? = null,
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/api/px/PxApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.bnyro.wallpaper.api.px

import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitBuilder
import com.bnyro.wallpaper.util.RetrofitHelper

class PxApi : Api() {
override val name: String = "Google Pixel"
override val baseUrl: String = "https://api.github.com"
private val api = RetrofitBuilder.create(baseUrl, Pixel::class.java)
private val api = RetrofitHelper.create(baseUrl, Pixel::class.java)
private val imgSrcPrefix = "https://raw.githubusercontent.com/wacko1805/Pixel-Wallpapers/main/"

private var wallpapers: List<Wallpaper> = emptyList()
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWall.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.bnyro.wallpaper.api.px.obj

import kotlinx.serialization.Serializable

@Serializable
data class PixelWall(
val mode: String = "",
val path: String = "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.bnyro.wallpaper.api.px.obj

import kotlinx.serialization.Serializable

@Serializable
data class PixelWallsResponse(
val sha: String = "",
val tree: List<PixelWall> = emptyList(),
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/bnyro/wallpaper/api/re/ReApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.bnyro.wallpaper.api.re

import com.bnyro.wallpaper.api.CommunityApi
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitBuilder
import com.bnyro.wallpaper.util.RetrofitHelper

class ReApi : CommunityApi() {
override val name = "Reddit"
Expand All @@ -12,7 +12,7 @@ class ReApi : CommunityApi() {
"time" to listOf("month", "year", "hour", "day", "week")
)

val api = RetrofitBuilder.create(baseUrl, Reddit::class.java)
val api = RetrofitHelper.create(baseUrl, Reddit::class.java)

override val defaultCommunityName = "r/wallpaper"

Expand All @@ -33,12 +33,12 @@ class ReApi : CommunityApi() {
nextPageAfter = response.data?.after

return response.data?.children?.filter {
it.childdata.url?.matches(imageRegex) == true
it.childData.url?.matches(imageRegex) == true
}?.map {
with(it.childdata) {
with(it.childData) {
Wallpaper(
preview?.images?.firstOrNull()?.source?.imgUrl ?: url!!,
it.childdata.title,
it.childData.title,
thumb = url,
resolution = preview?.images?.firstOrNull()?.source?.let { img -> "${img.width}x${img.height}" }
)
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/com/bnyro/wallpaper/api/re/obj/ChildData.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.bnyro.wallpaper.api.re.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class ChildData(
@JsonProperty("title") val title: String? = null,
@JsonProperty("url") val url: String? = null,
@JsonProperty("preview") val preview: Preview? = null
@SerialName("title") val title: String? = null,
@SerialName("url") val url: String? = null,
@SerialName("preview") val preview: Preview? = null
)
8 changes: 4 additions & 4 deletions app/src/main/java/com/bnyro/wallpaper/api/re/obj/Children.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bnyro.wallpaper.api.re.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Children(
@JsonProperty("data") val childdata: ChildData
@SerialName("data") val childData: ChildData
)
10 changes: 5 additions & 5 deletions app/src/main/java/com/bnyro/wallpaper/api/re/obj/Data.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.bnyro.wallpaper.api.re.obj

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Data(
@JsonProperty("after") val after: String? = null,
@JsonProperty("children") val children: ArrayList<Children>? = arrayListOf()
@SerialName("after") val after: String? = null,
@SerialName("children") val children: ArrayList<Children>? = arrayListOf()
)
Loading

0 comments on commit 9857d14

Please sign in to comment.