Fix image loader duplicating slashes
This commit is contained in:
@@ -1,21 +1,37 @@
|
|||||||
package gq.kirmanak.mealient.data.recipes.impl
|
package gq.kirmanak.mealient.data.recipes.impl
|
||||||
|
|
||||||
import android.widget.ImageView
|
import android.widget.ImageView
|
||||||
|
import androidx.annotation.VisibleForTesting
|
||||||
import gq.kirmanak.mealient.R
|
import gq.kirmanak.mealient.R
|
||||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||||
import gq.kirmanak.mealient.data.recipes.RecipeImageLoader
|
import gq.kirmanak.mealient.data.recipes.RecipeImageLoader
|
||||||
import gq.kirmanak.mealient.ui.ImageLoader
|
import gq.kirmanak.mealient.ui.ImageLoader
|
||||||
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class RecipeImageLoaderImpl @Inject constructor(
|
class RecipeImageLoaderImpl @Inject constructor(
|
||||||
private val imageLoader: ImageLoader,
|
private val imageLoader: ImageLoader,
|
||||||
private val authRepo: AuthRepo
|
private val authRepo: AuthRepo
|
||||||
): RecipeImageLoader {
|
): RecipeImageLoader {
|
||||||
|
|
||||||
override suspend fun loadRecipeImage(view: ImageView, slug: String?) {
|
override suspend fun loadRecipeImage(view: ImageView, slug: String?) {
|
||||||
val baseUrl = authRepo.getBaseUrl()
|
Timber.v("loadRecipeImage() called with: view = $view, slug = $slug")
|
||||||
val recipeImageUrl =
|
imageLoader.loadImage(generateImageUrl(slug), R.drawable.placeholder_recipe, view)
|
||||||
if (baseUrl.isNullOrBlank() || slug.isNullOrBlank()) null
|
}
|
||||||
else "$baseUrl/api/media/recipes/$slug/images/original.webp"
|
|
||||||
imageLoader.loadImage(recipeImageUrl, R.drawable.placeholder_recipe, view)
|
@VisibleForTesting
|
||||||
|
suspend fun generateImageUrl(slug: String?): String? {
|
||||||
|
Timber.v("generateImageUrl() called with: slug = $slug")
|
||||||
|
val result = authRepo.getBaseUrl()
|
||||||
|
?.takeIf { it.isNotBlank() }
|
||||||
|
?.takeUnless { slug.isNullOrBlank() }
|
||||||
|
?.toHttpUrlOrNull()
|
||||||
|
?.newBuilder()
|
||||||
|
?.addPathSegments("api/media/recipes/$slug/images/original.webp")
|
||||||
|
?.build()
|
||||||
|
?.toString()
|
||||||
|
Timber.v("generateImageUrl() returned: $result")
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
package gq.kirmanak.mealient.data.recipes.impl
|
||||||
|
|
||||||
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import dagger.hilt.android.testing.HiltAndroidTest
|
||||||
|
import gq.kirmanak.mealient.data.auth.AuthStorage
|
||||||
|
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_TOKEN
|
||||||
|
import gq.kirmanak.mealient.test.AuthImplTestData.TEST_URL
|
||||||
|
import gq.kirmanak.mealient.test.HiltRobolectricTest
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.junit.Test
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@HiltAndroidTest
|
||||||
|
class RecipeImageLoaderImplTest : HiltRobolectricTest() {
|
||||||
|
@Inject
|
||||||
|
lateinit var subject: RecipeImageLoaderImpl
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var authStorage: AuthStorage
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when url has slash then generated doesn't add new`() = runBlocking {
|
||||||
|
authStorage.storeAuthData(TEST_TOKEN, "https://google.com/")
|
||||||
|
val actual = subject.generateImageUrl("cake")
|
||||||
|
assertThat(actual).isEqualTo("https://google.com/api/media/recipes/cake/images/original.webp")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when url doesn't have slash then generated adds new`() = runBlocking {
|
||||||
|
authStorage.storeAuthData(TEST_TOKEN, "https://google.com")
|
||||||
|
val actual = subject.generateImageUrl("cake")
|
||||||
|
assertThat(actual).isEqualTo("https://google.com/api/media/recipes/cake/images/original.webp")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when url is null then generated is null`() = runBlocking {
|
||||||
|
val actual = subject.generateImageUrl("cake")
|
||||||
|
assertThat(actual).isNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when url is blank then generated is null`() = runBlocking {
|
||||||
|
authStorage.storeAuthData(TEST_TOKEN, " ")
|
||||||
|
val actual = subject.generateImageUrl("cake")
|
||||||
|
assertThat(actual).isNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when url is empty then generated is null`() = runBlocking {
|
||||||
|
authStorage.storeAuthData(TEST_TOKEN, "")
|
||||||
|
val actual = subject.generateImageUrl("cake")
|
||||||
|
assertThat(actual).isNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when slug is empty then generated is null`() = runBlocking {
|
||||||
|
authStorage.storeAuthData(TEST_TOKEN, TEST_URL)
|
||||||
|
val actual = subject.generateImageUrl("")
|
||||||
|
assertThat(actual).isNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when slug is blank then generated is null`() = runBlocking {
|
||||||
|
authStorage.storeAuthData(TEST_TOKEN, TEST_URL)
|
||||||
|
val actual = subject.generateImageUrl(" ")
|
||||||
|
assertThat(actual).isNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when slug is null then generated is null`() = runBlocking {
|
||||||
|
authStorage.storeAuthData(TEST_TOKEN, TEST_URL)
|
||||||
|
val actual = subject.generateImageUrl(null)
|
||||||
|
assertThat(actual).isNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user