Last active
September 12, 2024 19:56
-
-
Save Lucodivo/fd4fb8e486aec3dd1c34bddc28f84969 to your computer and use it in GitHub Desktop.
Kotlin: multiple if let
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
// taken from Jayson Minard's answer to the | |
// StackOverflow question "Multiple variable let in Kotlin" by Daniel Gomez Rico | |
// source: https://stackoverflow.com/a/35522422/3926619 | |
// 2 | |
inline fun <T1: Any, T2: Any, R: Any> multiIfLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? { | |
return if (p1 != null && p2 != null) block(p1, p2) else null | |
} | |
// 3 | |
inline fun <T1: Any, T2: Any, T3: Any, R: Any> multiIfLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3)->R?): R? { | |
return if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null | |
} | |
// 4 | |
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, R: Any> multiIfLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, block: (T1, T2, T3, T4)->R?): R? { | |
return if (p1 != null && p2 != null && p3 != null && p4 != null) block(p1, p2, p3, p4) else null | |
} | |
// 5 | |
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, T5: Any, R: Any> multiIfLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, p5: T5?, block: (T1, T2, T3, T4, T5)->R?): R? { | |
return if (p1 != null && p2 != null && p3 != null && p4 != null && p5 != null) block(p1, p2, p3, p4, p5) else null | |
} | |
// 6 | |
inline fun <T1: Any, T2: Any, T3: Any, T4: Any, T5: Any, T6: Any, R: Any> multiIfLet(p1: T1?, p2: T2?, p3: T3?, p4: T4?, p5: T5?, p6: T6?, block: (T1, T2, T3, T4, T5, T6)->R?): R? { | |
return if (p1 != null && p2 != null && p3 != null && p4 != null && p5 != null && p6 != null) block(p1, p2, p3, p4, p5, p6) else null | |
} | |
@Composable | |
override fun uiState(): StatisticsUIState { | |
val ensembleCount by remember { ensembleRepository.getCountEnsembles () }.collectAsState(null) | |
val articleCount by remember { articleRepository.getCountArticles() }.collectAsState(null) | |
val articleImageCount by remember { articleRepository.getCountArticleImages() }.collectAsState(null) | |
val mostPopularEnsembles by remember { ensembleRepository.getMostPopularEnsembles(TOP_ENSEMBLES_COUNT) }.collectAsState(null) | |
val mostPopularArticleByImages by remember { articleRepository.getMostPopularArticlesImageCount(1) }.collectAsState(null) | |
val mostPopularArticleByEnsembles by remember { ensembleRepository.getMostPopularArticlesEnsembleCount(1) }.collectAsState(null) | |
val result = multiIfLet(ensembleCount, articleCount, articleImageCount, mostPopularEnsembles, mostPopularArticleByImages, mostPopularArticleByEnsembles){ | |
ensembleCount, articleCount, articleImageCount, mostPopularEnsembles, mostPopularArticleByImages, mostPopularArticleByEnsembles -> | |
StatisticsUIState.Success( | |
ensembleCount = ensembleCount, | |
articleCount = articleCount, | |
articleImageCount = articleImageCount, | |
ensemblesWithMostArticles = mostPopularEnsembles, | |
articleWithMostImagesUriStrings = mostPopularArticleByImages, | |
articleWithMostEnsembles = mostPopularArticleByEnsembles, | |
) | |
} ?: StatisticsUIState.Loading | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment