-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use new user settings for search (#1024)
* feat: use the new settings for search as well Show the user chosen image quality, and user chosen titles for the search results. * test: update tests * test: fix search object creation in tests * test: fix another failure model creation in tests * style: ktlint fixes
- Loading branch information
Showing
15 changed files
with
200 additions
and
98 deletions.
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
55 changes: 55 additions & 0 deletions
55
features/search/src/main/java/com/chesire/nekome/app/search/host/ui/DomainMapper.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,55 @@ | ||
package com.chesire.nekome.app.search.host.ui | ||
|
||
import com.chesire.nekome.core.models.ImageModel | ||
import com.chesire.nekome.core.preferences.SeriesPreferences | ||
import com.chesire.nekome.core.preferences.flags.ImageQuality | ||
import com.chesire.nekome.core.preferences.flags.TitleLanguage | ||
import com.chesire.nekome.datasource.search.SearchDomain | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.runBlocking | ||
|
||
class DomainMapper @Inject constructor( | ||
private val seriesPreferences: SeriesPreferences | ||
) { | ||
|
||
fun toResultModels(models: List<SearchDomain>, currentSeriesIds: List<Int>): List<ResultModel> { | ||
return models.map { model -> | ||
ResultModel( | ||
id = model.id, | ||
type = model.type, | ||
synopsis = model.synopsis, | ||
title = chooseTitle(model), | ||
subtype = model.subtype.name, | ||
posterImage = choosePosterImageUrl(model.posterImage), | ||
canTrack = !currentSeriesIds.contains(model.id), | ||
isTracking = false | ||
) | ||
} | ||
} | ||
|
||
private fun chooseTitle(series: SearchDomain): String { | ||
val titleLanguage = runBlocking { | ||
seriesPreferences.titleLanguage.first() | ||
} | ||
|
||
return when (titleLanguage) { | ||
TitleLanguage.Canonical -> series.canonicalTitle | ||
else -> series.otherTitles[titleLanguage.key] | ||
.takeIf { !it.isNullOrBlank() } | ||
?: series.canonicalTitle | ||
} | ||
} | ||
|
||
private fun choosePosterImageUrl(imageModel: ImageModel): String { | ||
val imageQuality = runBlocking { | ||
seriesPreferences.imageQuality.first() | ||
} | ||
|
||
return when (imageQuality) { | ||
ImageQuality.Low -> imageModel.smallest?.url | ||
ImageQuality.Medium -> imageModel.middlest?.url | ||
ImageQuality.High -> imageModel.largest?.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
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
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
107 changes: 107 additions & 0 deletions
107
features/search/src/test/java/com/chesire/nekome/app/search/host/ui/DomainMapperTest.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,107 @@ | ||
package com.chesire.nekome.app.search.host.ui | ||
|
||
import com.chesire.nekome.core.flags.SeriesType | ||
import com.chesire.nekome.core.flags.Subtype | ||
import com.chesire.nekome.core.models.ImageModel | ||
import com.chesire.nekome.core.preferences.SeriesPreferences | ||
import com.chesire.nekome.core.preferences.flags.ImageQuality | ||
import com.chesire.nekome.core.preferences.flags.TitleLanguage | ||
import com.chesire.nekome.datasource.search.SearchDomain | ||
import io.mockk.clearAllMocks | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class DomainMapperTest { | ||
|
||
private val seriesPreferences = mockk<SeriesPreferences>() | ||
private lateinit var mapper: DomainMapper | ||
private val initialDomain = SearchDomain( | ||
id = 0, | ||
type = SeriesType.Anime, | ||
synopsis = "Synopsis", | ||
canonicalTitle = "canonicalTitle", | ||
otherTitles = mapOf( | ||
TitleLanguage.English.key to "en", | ||
TitleLanguage.Japanese.key to "jp" | ||
), | ||
subtype = Subtype.Movie, | ||
posterImage = ImageModel( | ||
tiny = ImageModel.ImageData( | ||
url = "tiny", | ||
width = 0, | ||
height = 0 | ||
), | ||
small = ImageModel.ImageData( | ||
url = "small", | ||
width = 0, | ||
height = 0 | ||
), | ||
medium = ImageModel.ImageData( | ||
url = "medium", | ||
width = 0, | ||
height = 0 | ||
), | ||
large = ImageModel.ImageData( | ||
url = "large", | ||
width = 0, | ||
height = 0 | ||
) | ||
) | ||
) | ||
|
||
@Before | ||
fun setup() { | ||
clearAllMocks() | ||
|
||
every { seriesPreferences.imageQuality } returns flowOf(ImageQuality.Medium) | ||
every { seriesPreferences.titleLanguage } returns flowOf(TitleLanguage.Canonical) | ||
mapper = DomainMapper(seriesPreferences) | ||
} | ||
|
||
@Test | ||
fun `When toResultModels, Then list of ResultModel item is returned`() = runTest { | ||
val input = listOf(initialDomain) | ||
val expected = listOf( | ||
ResultModel( | ||
id = 0, | ||
type = SeriesType.Anime, | ||
synopsis = "Synopsis", | ||
title = "canonicalTitle", | ||
subtype = Subtype.Movie.name, | ||
posterImage = "medium", | ||
canTrack = true, | ||
isTracking = false | ||
) | ||
) | ||
|
||
val result = mapper.toResultModels(input, emptyList()) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@Test | ||
fun `Given user already tracks ID, When toResultModels, Then canTrack is false`() = runTest { | ||
val input = listOf(initialDomain) | ||
val expected = listOf( | ||
ResultModel( | ||
id = 0, | ||
type = SeriesType.Anime, | ||
synopsis = "Synopsis", | ||
title = "canonicalTitle", | ||
subtype = Subtype.Movie.name, | ||
posterImage = "medium", | ||
canTrack = false, | ||
isTracking = false | ||
) | ||
) | ||
|
||
val result = mapper.toResultModels(input, listOf(0)) | ||
|
||
assertEquals(expected, result) | ||
} | ||
} |
Oops, something went wrong.