From 2b27bfc207e337c06ede6cd514e0ca14c84002d6 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 30 Oct 2023 17:14:59 +0100 Subject: [PATCH] feat: new wallpaper source - pixel walls --- app/release/output-metadata.json | 4 +- app/src/main/java/com/bnyro/wallpaper/App.kt | 2 + .../java/com/bnyro/wallpaper/api/px/Pixel.kt | 9 ++++ .../java/com/bnyro/wallpaper/api/px/PxApi.kt | 41 +++++++++++++++++++ .../bnyro/wallpaper/api/px/obj/PixelWall.kt | 10 +++++ .../api/px/obj/PixelWallsResponse.kt | 8 ++++ .../bnyro/wallpaper/ui/nav/DrawerScreens.kt | 3 +- .../com/bnyro/wallpaper/util/Preferences.kt | 1 + app/src/main/res/values/strings.xml | 1 + 9 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/bnyro/wallpaper/api/px/Pixel.kt create mode 100644 app/src/main/java/com/bnyro/wallpaper/api/px/PxApi.kt create mode 100644 app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWall.kt create mode 100644 app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWallsResponse.kt diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json index d62adc24..52c80adc 100644 --- a/app/release/output-metadata.json +++ b/app/release/output-metadata.json @@ -11,8 +11,8 @@ "type": "SINGLE", "filters": [], "attributes": [], - "versionCode": 19, - "versionName": "4.1", + "versionCode": 22, + "versionName": "6.1", "outputFile": "app-release.apk" } ], diff --git a/app/src/main/java/com/bnyro/wallpaper/App.kt b/app/src/main/java/com/bnyro/wallpaper/App.kt index 05bcafa8..09227586 100644 --- a/app/src/main/java/com/bnyro/wallpaper/App.kt +++ b/app/src/main/java/com/bnyro/wallpaper/App.kt @@ -8,6 +8,7 @@ import com.bnyro.wallpaper.api.bi.BiApi import com.bnyro.wallpaper.api.le.LeApi import com.bnyro.wallpaper.api.ow.OwApi import com.bnyro.wallpaper.api.ps.PsApi +import com.bnyro.wallpaper.api.px.PxApi import com.bnyro.wallpaper.api.re.ReApi import com.bnyro.wallpaper.api.us.UsApi import com.bnyro.wallpaper.api.wh.WhApi @@ -51,5 +52,6 @@ class App : Application(), ImageLoaderFactory { val reApi = ReApi() val leApi = LeApi() val whApi = WhApi() + val pxApi = PxApi() } } diff --git a/app/src/main/java/com/bnyro/wallpaper/api/px/Pixel.kt b/app/src/main/java/com/bnyro/wallpaper/api/px/Pixel.kt new file mode 100644 index 00000000..5847509e --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/api/px/Pixel.kt @@ -0,0 +1,9 @@ +package com.bnyro.wallpaper.api.px + +import com.bnyro.wallpaper.api.px.obj.PixelWallsResponse +import retrofit2.http.GET + +interface Pixel { + @GET("/repos/wacko1805/Pixel-Wallpapers/git/trees/main?recursive=1") + suspend fun getWallpapers(): PixelWallsResponse +} \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/api/px/PxApi.kt b/app/src/main/java/com/bnyro/wallpaper/api/px/PxApi.kt new file mode 100644 index 00000000..164908a7 --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/api/px/PxApi.kt @@ -0,0 +1,41 @@ +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 + +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 imgSrcPrefix = "https://raw.githubusercontent.com/wacko1805/Pixel-Wallpapers/main/" + + private var wallpapers: List = emptyList() + private val resultsPerPage = 20 + + private suspend fun updateWallpaperList() { + wallpapers = api.getWallpapers() + .tree + .filter { it.path.endsWith(".jpg") || it.path.endsWith(".png") } + .map { + Wallpaper( + title = it.path.split("/").last().split(".").first(), + imgSrc = imgSrcPrefix + it.path, + category = it.path.split("/").first(), + fileSize = it.size + ) + } + } + + override suspend fun getWallpapers(page: Int): List { + if (wallpapers.isEmpty()) { + updateWallpaperList() + } + return wallpapers.subList((page - 1) * resultsPerPage, page * resultsPerPage) + } + + override suspend fun getRandomWallpaperUrl(): String? { + if (wallpapers.isEmpty()) updateWallpaperList() + return wallpapers.randomOrNull()?.imgSrc + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWall.kt b/app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWall.kt new file mode 100644 index 00000000..347463ec --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWall.kt @@ -0,0 +1,10 @@ +package com.bnyro.wallpaper.api.px.obj + +data class PixelWall( + val mode: String = "", + val path: String = "", + val sha: String = "", + val size: Long = 0, + val type: String = "", + val url: String = "" +) \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWallsResponse.kt b/app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWallsResponse.kt new file mode 100644 index 00000000..fe6e1755 --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/api/px/obj/PixelWallsResponse.kt @@ -0,0 +1,8 @@ +package com.bnyro.wallpaper.api.px.obj + +data class PixelWallsResponse( + val sha: String = "", + val tree: List = emptyList(), + val truncated: Boolean = false, + val url: String = "" +) \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt b/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt index 76f55f49..85d82440 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt @@ -29,12 +29,13 @@ sealed class DrawerScreens( object BingDaily : DrawerScreens(R.string.bing_daily, "bi", Icons.Default.Nightlight) object Reddit : DrawerScreens(R.string.reddit, "redd", Icons.Default.Forum) object Lemmy : DrawerScreens(R.string.lemmy, "le", Icons.Default.Book) + object Pixel : DrawerScreens(R.string.pixel, "px", Icons.Default.Pix) object Favorites : DrawerScreens(R.string.favorites, "favorites", Icons.Default.Favorite, true) object Settings : DrawerScreens(R.string.settings, "settings", Icons.Default.Settings, true) object About : DrawerScreens(R.string.about, "about", Icons.Default.Info) companion object { - val apiScreens by lazy { listOf(Wallhaven, Unsplash, OWalls, Picsum, BingDaily, Reddit, Lemmy) } + val apiScreens by lazy { listOf(Wallhaven, Unsplash, OWalls, Picsum, BingDaily, Reddit, Lemmy, Pixel) } val screens by lazy { listOf(*apiScreens.toTypedArray(), Favorites, Settings, About) } } } diff --git a/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt b/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt index 92c9e686..4131cda4 100644 --- a/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt +++ b/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt @@ -53,6 +53,7 @@ object Preferences { DrawerScreens.BingDaily.route -> App.biApi DrawerScreens.Reddit.route -> App.reApi DrawerScreens.Lemmy.route -> App.leApi + DrawerScreens.Pixel.route -> App.pxApi else -> App.whApi } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e4d5a866..2efefd0c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,6 +8,7 @@ Bing daily Reddit Lemmy + Google Pixel Favorites Settings