Skip to content

Instantly share code, notes, and snippets.

@Composable
fun CategoriesHorizontalList(modifier: Modifier = Modifier, data: List<String>) {
val selectedHourState = remember { mutableStateOf(0) }
Card(
elevation = elevationDefault, shape = boxShapeDefault, modifier = Modifier.padding(8.dp)
) {
LazyRow(
modifier = modifier.background(MaterialTheme.colors.primary.copy(alpha = 0.9f)),
horizontalArrangement = Arrangement.spacedBy(8.dp),
contentPadding = PaddingValues(horizontal = 8.dp)
@EmmanuelGuther
EmmanuelGuther / ComposeTab.kt
Created March 14, 2022 08:55
Jetpack compose tabRow implementation
@Composable
fun IconTab(daysEnergyHistoric: DaysEnergyHistoric) {
var tabIndex by remember { mutableStateOf(0) }
val tabData = daysEnergyHistoric.toList()
TabRow(selectedTabIndex = tabIndex) {
tabData.forEachIndexed { index, pair ->
Tab(selected = tabIndex == index, onClick = {
tabIndex = index
}, text = {
@Test
fun myTest() = coroutineDispatcher.runBlockingTest {
val uc = GetPicturesUseCase(MyFakeRepository())
val item = uc.invoke().first()
assert(item is ResultData.Failure)
}
// Test if number have not numFractionDigits ex: (40.12345).roundTo(7) cant get the last 7 because have 5
private fun Double.maxDecimals(numFractionDigits: Int) = try {
val factor = 10.0.pow(numFractionDigits.toDouble())
(this * factor).roundToInt() / factor
} catch (e: Exception) {
e.localizedMessage?.toString()?.let { Log.e("Double.maxDecimals", it) }
this
}
@EmmanuelGuther
EmmanuelGuther / kotlin.collections.build-list.kt
Created November 16, 2021 16:29
kotlin collections build-list
For those cases where you have to calculate the elements before adding them to the list,
you "save" the boilerplate of creating a mutable list and then returning an immutable list.
The example of the docs is... but it is understood where it goes.
val y = buildList () {
add ('a')
addAll (x)
add ('d')
}
@EmmanuelGuther
EmmanuelGuther / ComposeGradientModifier.kt
Created November 14, 2021 09:22
Jetpack compose gradient modifier
fun Modifier.gradientBackground(colors: List<Color>, angle: Float) = this.then(
Modifier.drawBehind {
val angleRad = angle / 180f * PI
val x = kotlin.math.cos(angleRad).toFloat() //Fractional x
val y = kotlin.math.sin(angleRad).toFloat() //Fractional y
val radius:Float = kotlin.math.sqrt(
((size.width.pow(2) + size.height.pow(2))) / 2f)
val offset = center + Offset(x * radius, y * radius)
@Query("SELECT count(*) FROM alarm")
fun countAlarms(): Flow<Int>
@EmmanuelGuther
EmmanuelGuther / ResultData.kt
Created November 14, 2021 06:53
sealed class to manage api calls
sealed class ResultData<out T> {
class Loading<out T>: ResultData<T>()
data class Success<out T>(
val data: T
): ResultData<T>()
data class Failure<out T>(
val errorMessage: String?=null, val exception: Exception?= null
): ResultData<T>()
@EmmanuelGuther
EmmanuelGuther / gradientView.kt
Last active November 12, 2021 19:29
Jetpack compose gradient background
@Composable
fun GradientView(modifier: Modifier = Modifier) {
Box(
modifier = Modifier
.background(
brush = Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.primary,
MaterialTheme.colorScheme.secondary
@EmmanuelGuther
EmmanuelGuther / backgroundAlphaAnimated.kt
Created November 12, 2021 19:08
Jetpack compose alpha background animated
@Composable
fun BackgroundAlphaAnimated() {
val animatedAlpha = remember { Animatable(0f) }
Box(
Modifier
.background(color = (Color.DarkGray.copy(alpha = animatedAlpha.value)))
.fillMaxSize()
)
LaunchedEffect(animatedAlpha) {