-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for NASA APOD (closes #212)
- Loading branch information
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
app/src/main/java/com/bnyro/wallpaper/api/na/obj/NasaImage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |