Skip to content

Commit

Permalink
refactor: use inline function to create retrofit instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Feb 4, 2025
1 parent 91e52b7 commit 05bd99f
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/bi/BiApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BiApi : Api() {
)
private val previewResolution = "768x1366"

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

private fun getImgSrc(path: String, resolution: String): String {
return "$baseUrl${path}_${resolution}.jpg"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/le/LeApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class LeApi : CommunityApi() {

override val defaultCommunityName: String = "pics@lemmy.world"

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

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
return api.getWallpapers(
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/na/NaApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NaApi : Api() {
override val filters: Map<String, List<String>>
get() = mapOf("order" to listOf("date", "random"))

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

private var nextEndDate: LocalDateTime? = null

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/ow/OwApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OwApi : Api() {
"style" to listOf("all", "light", "dark")
)

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

private var wallpapers: MutableList<Wallpaper> = mutableListOf()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/ps/PsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PsApi : Api() {
override val baseUrl: String = "https://picsum.photos"
override val icon = Icons.Default.Pix

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

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
return api.getWallpapers(page).map {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/px/PxApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class PxApi : Api() {
override val baseUrl: String = "https://api.github.com"
override val icon = Icons.Default.Pix

private val api = RetrofitHelper.create(baseUrl, Pixel::class.java)
private val api = RetrofitHelper.create<Pixel>(baseUrl)
private val imgSrcPrefix = "https://raw.githubusercontent.com/wacko1805/Pixel-Wallpapers/main/"

private var wallpapers: List<Wallpaper> = emptyList()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/re/ReApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ReApi : CommunityApi() {
"time" to listOf("month", "year", "hour", "day", "week")
)

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

override val defaultCommunityName = "r/wallpaper"

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/sp/SpApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SpApi: Api() {
"orientation" to listOf("portrait", "landscape")
)

private val api = RetrofitHelper.create(baseUrl, Spotlight::class.java)
private val api = RetrofitHelper.create<Spotlight>(baseUrl)

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
val country = getQuery("country")
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/us/UsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UsApi : Api() {
)
override val supportsTags: Boolean = true

private val api = RetrofitHelper.create(baseUrl, Unsplash::class.java)
private val api = RetrofitHelper.create<Unsplash>(baseUrl)

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
val tags = getTags()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/wh/WhApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WhApi : Api() {
)
override val supportsTags: Boolean = true

private val api = RetrofitHelper.create(baseUrl, Wallhaven::class.java)
private val api = RetrofitHelper.create<Wallhaven>(baseUrl)

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
return api.search(
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/api/wi/WiAPi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WiAPi : Api() {
override val icon: ImageVector = Icons.Default.Today
override val baseUrl = "https://en.wikipedia.org"

private val api = RetrofitHelper.create(baseUrl, WikiPOTD::class.java)
private val api = RetrofitHelper.create<WikiPOTD>(baseUrl)
private val dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd")

override suspend fun getWallpapers(page: Int): List<Wallpaper> {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/bnyro/wallpaper/util/RetrofitHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object RetrofitHelper {
}
}

private val okHttpClient by lazy {
val okHttpClient by lazy {
val builder = OkHttpClient.Builder()

if (BuildConfig.DEBUG) {
Expand All @@ -28,14 +28,14 @@ object RetrofitHelper {
builder.build()
}

fun <T> create(baseUrl: String, type: Class<T>): T {
inline fun <reified T> create(baseUrl: String): T {
val mediaType = "application/json".toMediaType()

return Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(json.asConverterFactory(mediaType))
.build()
.create(type)
.create(T::class.java)
}
}

0 comments on commit 05bd99f

Please sign in to comment.