Skip to content

Commit

Permalink
feat: support for NASA APOD (closes #212)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 29, 2024
1 parent 31b13a9 commit 9ef51bb
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import coil.ImageLoaderFactory
import coil.disk.DiskCache
import com.bnyro.wallpaper.api.bi.BiApi
import com.bnyro.wallpaper.api.le.LeApi
import com.bnyro.wallpaper.api.na.NaApi
import com.bnyro.wallpaper.api.ow.OwApi
import com.bnyro.wallpaper.api.ps.PsApi
import com.bnyro.wallpaper.api.px.PxApi
Expand Down Expand Up @@ -47,6 +48,6 @@ class App : Application(), ImageLoaderFactory {

companion object {
val apis =
listOf(WhApi(), OwApi(), UsApi(), BiApi(), ReApi(), LeApi(), PxApi(), SpApi(), PsApi())
listOf(WhApi(), OwApi(), UsApi(), BiApi(), ReApi(), LeApi(), PxApi(), SpApi(), NaApi(), PsApi())
}
}
63 changes: 63 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/na/NaApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.bnyro.wallpaper.api.na

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.ui.graphics.vector.ImageVector
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.util.RetrofitHelper
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class NaApi : Api() {
override val name: String = "NASA APOD"
override val icon: ImageVector = Icons.Default.Star
override val baseUrl: String = "https://api.nasa.gov/"

override val filters: Map<String, List<String>>
get() = mapOf("order" to listOf("date", "random"))

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

private var nextEndDate: LocalDateTime? = null

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
val sortByDate = getQuery("order") == "date"

val response = if (sortByDate) {
val endDate = if (page == 1 || nextEndDate == null) {
LocalDateTime.now()
} else {
nextEndDate
}
nextEndDate = endDate!!.minusDays(10)

api.getImages(
endDate = endDate.format(DateTimeFormatter.ISO_LOCAL_DATE),
startDate = nextEndDate!!.format(
DateTimeFormatter.ISO_LOCAL_DATE
)
)
} else {
api.getImages(count = 10)
}

return response
.filter { it.mediaType == "image" && (it.url ?: it.hdUrl) != null }
.map {
Wallpaper(
imgSrc = it.hdUrl ?: it.url!!,
thumb = it.url,
author = it.copyright,
creationDate = it.date,
title = it.title,
description = it.explanation
)
}
}

override suspend fun getRandomWallpaperUrl(): String? {
val image = api.getLatestImage()
return image.hdUrl ?: image.url
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/na/NasaAPOD.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.bnyro.wallpaper.api.na

import com.bnyro.wallpaper.api.na.obj.NasaImage
import retrofit2.http.GET
import retrofit2.http.Query

private const val API_KEY = "DEMO_KEY"

interface NasaAPOD {
@GET("planetary/apod")
suspend fun getImages(
@Query("api_key") apiKey: String = API_KEY,
@Query("count") count: Int? = null,
@Query("start_date") startDate: String? = null,
@Query("end_date") endDate: String? = null
): List<NasaImage>

@GET("planetary/apod")
suspend fun getLatestImage(
@Query("api_key") apiKey: String = API_KEY,
): NasaImage
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/api/na/obj/NasaImage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bnyro.wallpaper.api.na.obj

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class NasaImage(
val url: String? = null,
val title: String,
val explanation: String,
val copyright: String? = null,
val date: String,
@SerialName("hdurl") val hdUrl: String? = null,
@SerialName("media_type") val mediaType: String,
)

0 comments on commit 9ef51bb

Please sign in to comment.