Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: filter before mapping in ImageNameParser.kt
refactor: rename TimestampedKey to ValueAndTimestamp
- Loading branch information
Showing
6 changed files
with
630 additions
and
47 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
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
35 changes: 21 additions & 14 deletions
35
app/src/main/java/openfoodfacts/github/scrachx/openfood/images/ImageNameParser.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 |
---|---|---|
@@ -1,31 +1,38 @@ | ||
package openfoodfacts.github.scrachx.openfood.images | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import openfoodfacts.github.scrachx.openfood.models.ValueAndTimestamp | ||
|
||
|
||
object ImageNamesParser { | ||
internal fun extractImageNames(jsonNode: JsonNode): List<TimestampedKey<String>> { | ||
object ImageNameParser { | ||
/** | ||
* @return a sorted by timestamp [List] of [ValueAndTimestamp] that have the | ||
* image name as key. | ||
* | ||
* The list is sorted in descending order (first newer images). | ||
*/ | ||
internal fun extractImageNames(jsonNode: JsonNode): List<ValueAndTimestamp<String>> { | ||
return jsonNode["product"]["images"]?.fields() | ||
?.asSequence() | ||
?.toList().orEmpty() | ||
.map { TimestampedKey(it.value["uploaded_t"].asLong(), it.key) } | ||
.filter { | ||
// do not include images with contain nutrients, ingredients or other in their names | ||
// as they are duplicate and do not load as well | ||
isNameOk(it.key) | ||
// Do not include images with contain nutrients, | ||
// ingredients or other in their names. They are duplicates and | ||
// sometimes they do not have the `uploaded_t` field and make | ||
// the mapping throw a NPE | ||
isValidImageName(it.key) | ||
} | ||
.map { ValueAndTimestamp(it.value["uploaded_t"].asLong(), it.key) } | ||
.sortedByTimestampDescending() | ||
|
||
} | ||
|
||
fun isNameOk(name: String) = | ||
internal fun isValidImageName(name: String) = | ||
name.isNotBlank() && !Regex("[nfio]").containsMatchIn(name) | ||
} | ||
|
||
internal data class TimestampedKey<T>( | ||
val timestamp: Long, | ||
val key: T, | ||
) | ||
private fun <T> List<ValueAndTimestamp<T>>.sortedByTimestampDescending() = | ||
sortedByDescending { it.timestamp } | ||
} | ||
|
||
|
||
internal fun <T> List<TimestampedKey<T>>.sortedByTimestampDescending() = | ||
sortedByDescending { it.timestamp } | ||
|
6 changes: 6 additions & 0 deletions
6
app/src/main/java/openfoodfacts/github/scrachx/openfood/models/ValueAndTimestamp.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,6 @@ | ||
package openfoodfacts.github.scrachx.openfood.models | ||
|
||
internal data class ValueAndTimestamp<V>( | ||
val timestamp: Long, | ||
val value: V, | ||
) |
Oops, something went wrong.