From 368a73037de84a797f3edf702112de7f176dac76 Mon Sep 17 00:00:00 2001 From: Niels Masdorp Date: Fri, 30 Jun 2023 12:17:16 +0200 Subject: [PATCH] Fix more detekt issues --- app/build.gradle | 4 +- .../nederadio/ui/NederadioActivity.kt | 17 ++- .../nielsmasdorp/nederadio/ui/NederadioApp.kt | 113 +++++++++++------- .../nederadio/ui/components/StreamsGrid.kt | 7 +- .../nederadio/ui/equalizer/EqualizerScreen.kt | 2 +- .../ui/extension/BottomSheetScaffoldState.kt | 27 ----- .../nederadio/ui/home/HomeScreen.kt | 5 +- .../bottomsheet/collapsed/SheetCollapsed.kt | 4 +- .../bottomsheet/collapsed/StreamViewSmall.kt | 2 +- .../bottomsheet/expanded/SheetExpanded.kt | 3 + .../bottomsheet/expanded/StreamViewLarge.kt | 33 +++-- .../nederadio/ui/search/SearchScreen.kt | 1 + .../nederadio/util/LiveDataUtils.kt | 52 -------- build.gradle | 4 +- 14 files changed, 117 insertions(+), 157 deletions(-) delete mode 100644 app/src/main/java/com/nielsmasdorp/nederadio/ui/extension/BottomSheetScaffoldState.kt delete mode 100644 app/src/test/java/com/nielsmasdorp/nederadio/util/LiveDataUtils.kt diff --git a/app/build.gradle b/app/build.gradle index 955a58f..566030c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -14,8 +14,8 @@ android { minSdkVersion 26 //noinspection OldTargetApi targetSdkVersion 33 - versionCode 195 - versionName "2.3.1" + versionCode 196 + versionName "2.3.2" } buildTypes { release { diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioActivity.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioActivity.kt index 73a089d..c68f5fb 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioActivity.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioActivity.kt @@ -1,11 +1,13 @@ package com.nielsmasdorp.nederadio.ui +import android.app.Activity import android.os.Bundle +import android.view.View +import android.view.WindowManager import androidx.activity.compose.setContent import androidx.appcompat.app.AppCompatActivity import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.ui.Modifier -import androidx.core.view.WindowCompat import androidx.mediarouter.app.MediaRouteButton import com.google.android.gms.cast.framework.CastButtonFactory import com.nielsmasdorp.nederadio.ui.theme.AppTheme @@ -17,7 +19,7 @@ class NederadioActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - WindowCompat.setDecorFitsSystemWindows(window, false) + makeStatusBarTransparent() // Cast button setup val castButton = MediaRouteButton(this) @@ -38,4 +40,15 @@ class NederadioActivity : AppCompatActivity() { } } } + + @Suppress("DEPRECATION") + fun Activity.makeStatusBarTransparent() { + window.apply { + clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) + addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) + decorView.systemUiVisibility = + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR + statusBarColor = android.graphics.Color.TRANSPARENT + } + } } diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioApp.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioApp.kt index 7739808..d9426ca 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioApp.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/NederadioApp.kt @@ -4,16 +4,16 @@ import android.annotation.SuppressLint import android.view.View import androidx.activity.compose.BackHandler import androidx.compose.animation.AnimatedContentScope +import androidx.compose.animation.core.FastOutSlowInEasing +import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.tween import androidx.compose.foundation.layout.* -import androidx.compose.material.BottomSheetScaffold -import androidx.compose.material.BottomSheetValue -import androidx.compose.material.rememberBottomSheetScaffoldState -import androidx.compose.material.rememberBottomSheetState +import androidx.compose.material3.BottomSheetScaffold import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.SheetValue +import androidx.compose.material3.rememberBottomSheetScaffoldState import androidx.compose.runtime.* import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import com.google.accompanist.navigation.animation.AnimatedNavHost import com.google.accompanist.navigation.animation.composable @@ -25,7 +25,6 @@ import com.nielsmasdorp.nederadio.ui.components.dialog.AboutAppDialog import com.nielsmasdorp.nederadio.ui.components.dialog.SleepTimerDialog import com.nielsmasdorp.nederadio.ui.equalizer.EqualizerScreen import com.nielsmasdorp.nederadio.ui.equalizer.EqualizerViewModel -import com.nielsmasdorp.nederadio.ui.extension.currentFraction import com.nielsmasdorp.nederadio.ui.home.HomeScreen import com.nielsmasdorp.nederadio.ui.home.bottomsheet.SheetContent import com.nielsmasdorp.nederadio.ui.home.bottomsheet.collapsed.SheetCollapsed @@ -37,6 +36,8 @@ import com.nielsmasdorp.nederadio.ui.search.SearchViewModel import kotlinx.coroutines.launch import org.koin.androidx.compose.getViewModel +private const val AnimationDurationMs = 50 + /** * @author Niels Masdorp (NielsMasdorp) */ @@ -53,12 +54,10 @@ fun NederadioApp( val systemUiController = rememberSystemUiController() - DisposableEffect(systemUiController) { - systemUiController.setSystemBarsColor( - color = Color.Transparent, - darkIcons = false - ) - onDispose {} + val systemBarsColor = MaterialTheme.colorScheme.primary + + LaunchedEffect(systemUiController) { + systemUiController.setSystemBarsColor(color = systemBarsColor) } DisposableEffect(key1 = viewModel) { @@ -68,21 +67,19 @@ fun NederadioApp( val navController = rememberAnimatedNavController() val scope = rememberCoroutineScope() - val scaffoldState = rememberBottomSheetScaffoldState( - bottomSheetState = rememberBottomSheetState(BottomSheetValue.Collapsed) - ) + val scaffoldState = rememberBottomSheetScaffoldState() val sheetToggle: () -> Unit = { scope.launch { - if (scaffoldState.bottomSheetState.isCollapsed) { + if (scaffoldState.bottomSheetState.currentValue == SheetValue.PartiallyExpanded) { scaffoldState.bottomSheetState.expand() } else { - scaffoldState.bottomSheetState.collapse() + scaffoldState.bottomSheetState.partialExpand() } } } - BackHandler(enabled = scaffoldState.bottomSheetState.isExpanded) { + BackHandler(enabled = scaffoldState.bottomSheetState.currentValue == SheetValue.Expanded) { sheetToggle() } @@ -92,7 +89,10 @@ fun NederadioApp( val sleepTimer: String? by viewModel.sleepTimer.collectAsState(initial = null) val showAboutAppDialog: Boolean by viewModel.showAboutApp.collectAsState(initial = false) val showSleepTimerDialog: Boolean by viewModel.showSleepTimer.collectAsState(initial = false) - + val currentBottomSheetFraction: Float by animateFloatAsState( + targetValue = if (scaffoldState.bottomSheetState.targetValue == SheetValue.Expanded) 0f else 1f, + animationSpec = tween(durationMillis = AnimationDurationMs, easing = FastOutSlowInEasing) + ) EventHandler(event = viewModel.errorState.error) { scaffoldState.snackbarHostState.showSnackbar(message = it) } @@ -109,14 +109,14 @@ fun NederadioApp( } BottomSheetScaffold( - modifier = modifier - .fillMaxSize(), + modifier = modifier.fillMaxSize(), scaffoldState = scaffoldState, - sheetElevation = if (activeStream is ActiveStream.Filled) 12.dp else 0.dp, - backgroundColor = MaterialTheme.colorScheme.primary, + sheetContainerColor = MaterialTheme.colorScheme.primary, + sheetDragHandle = null, sheetContent = { SheetContent { SheetExpanded( + currentFraction = currentBottomSheetFraction, content = { StreamViewLarge( playerControls = largePlayerControls, @@ -124,14 +124,15 @@ fun NederadioApp( sleepTimer = sleepTimer, onCollapseClick = { sheetToggle() }, onTimerClicked = viewModel::onTimerPicked, - onStreamFavoriteStatusChanged = viewModel::onStreamFavoriteChanged, - currentFraction = scaffoldState.currentFraction + onStreamFavoriteStatusChanged = viewModel::onStreamFavoriteChanged ) } ) SheetCollapsed( - isEnabled = scaffoldState.bottomSheetState.isCollapsed && activeStream is ActiveStream.Filled, - currentFraction = scaffoldState.currentFraction, + isEnabled = scaffoldState.bottomSheetState.currentValue == + SheetValue.PartiallyExpanded && + activeStream is ActiveStream.Filled, + currentFraction = currentBottomSheetFraction, onSheetClick = { sheetToggle() }, activeStream = activeStream ) { currentStream -> @@ -143,18 +144,44 @@ fun NederadioApp( } } }, - sheetPeekHeight = 72.dp + + sheetPeekHeight = 80.dp + WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding(), - sheetGesturesEnabled = false + sheetSwipeEnabled = false ) { AnimatedNavHost( navController = navController, startDestination = "home", route = "route" ) { - composable("home") { + composable( + "home", + enterTransition = { + slideIntoContainer( + towards = AnimatedContentScope.SlideDirection.Up, + animationSpec = tween(delayMillis = AnimationDurationMs) + ) + }, + exitTransition = { + slideOutOfContainer( + towards = AnimatedContentScope.SlideDirection.Up, + animationSpec = tween(delayMillis = AnimationDurationMs) + ) + }, + popEnterTransition = { + slideIntoContainer( + towards = AnimatedContentScope.SlideDirection.Down, + animationSpec = tween(delayMillis = AnimationDurationMs) + ) + }, + popExitTransition = { + slideOutOfContainer( + towards = AnimatedContentScope.SlideDirection.Down, + animationSpec = tween(delayMillis = AnimationDurationMs) + ) + } + ) { HomeScreen( - modifier = Modifier.statusBarsPadding(), + modifier = Modifier.fillMaxSize(), castButton = castButton, streams = streams, activeStream = activeStream, @@ -170,14 +197,14 @@ fun NederadioApp( "search", enterTransition = { slideIntoContainer( - AnimatedContentScope.SlideDirection.Up, - animationSpec = tween(200) + towards = AnimatedContentScope.SlideDirection.Up, + animationSpec = tween(delayMillis = AnimationDurationMs) ) }, exitTransition = { slideOutOfContainer( - AnimatedContentScope.SlideDirection.Down, - animationSpec = tween(200) + towards = AnimatedContentScope.SlideDirection.Down, + animationSpec = tween(delayMillis = AnimationDurationMs) ) } ) { @@ -185,10 +212,10 @@ fun NederadioApp( viewModelStoreOwner = navController.getBackStackEntry("route") ) SearchScreen( - modifier = Modifier.statusBarsPadding(), + modifier = Modifier.fillMaxSize(), viewModel = searchViewModel, onExitSearch = { navController.popBackStack() }, - backPressHandler = if (scaffoldState.bottomSheetState.isExpanded) { + backPressHandler = if (scaffoldState.bottomSheetState.currentValue == SheetValue.Expanded) { { sheetToggle() } } else null ) @@ -197,14 +224,14 @@ fun NederadioApp( "equalizer", enterTransition = { slideIntoContainer( - AnimatedContentScope.SlideDirection.Up, - animationSpec = tween(200) + towards = AnimatedContentScope.SlideDirection.Up, + animationSpec = tween(delayMillis = AnimationDurationMs) ) }, exitTransition = { slideOutOfContainer( - AnimatedContentScope.SlideDirection.Down, - animationSpec = tween(200) + towards = AnimatedContentScope.SlideDirection.Down, + animationSpec = tween(delayMillis = AnimationDurationMs) ) } ) { @@ -212,10 +239,10 @@ fun NederadioApp( viewModelStoreOwner = navController.getBackStackEntry("route") ) EqualizerScreen( - modifier = Modifier.statusBarsPadding(), + modifier = Modifier.fillMaxSize(), viewModel = equalizerViewModel, onExitEqualizer = { navController.popBackStack() }, - backPressHandler = if (scaffoldState.bottomSheetState.isExpanded) { + backPressHandler = if (scaffoldState.bottomSheetState.currentValue == SheetValue.Expanded) { { sheetToggle() } } else null ) diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/components/StreamsGrid.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/components/StreamsGrid.kt index 2075770..3136c05 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/components/StreamsGrid.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/components/StreamsGrid.kt @@ -34,12 +34,11 @@ fun StreamsGrid( Surface( modifier = modifier - .fillMaxSize() - .padding(bottom = 72.dp), + .fillMaxSize(), color = MaterialTheme.colorScheme.primaryContainer, ) { if (streams.isEmpty()) { - Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) { + Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { EmptyView( title = stringResource(id = R.string.streams_overview_no_streams_found), tint = MaterialTheme.colorScheme.onPrimaryContainer @@ -52,7 +51,7 @@ fun StreamsGrid( start = 16.dp, end = 16.dp, top = 16.dp, - bottom = 40.dp + bottom = 96.dp ), verticalArrangement = Arrangement.spacedBy(12.dp), horizontalArrangement = Arrangement.spacedBy(12.dp) diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/equalizer/EqualizerScreen.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/equalizer/EqualizerScreen.kt index 408ba8a..561f2c1 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/equalizer/EqualizerScreen.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/equalizer/EqualizerScreen.kt @@ -92,7 +92,7 @@ fun EqualizerScreen( Box( modifier = modifier .padding(innerPadding) - .padding(bottom = 72.dp) + .padding(bottom = 80.dp) .background(MaterialTheme.colorScheme.background) ) { when (val data = state) { diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/extension/BottomSheetScaffoldState.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/extension/BottomSheetScaffoldState.kt deleted file mode 100644 index 5298934..0000000 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/extension/BottomSheetScaffoldState.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.nielsmasdorp.nederadio.ui.extension - -import androidx.compose.material.BottomSheetScaffoldState -import androidx.compose.material.BottomSheetValue.Collapsed -import androidx.compose.material.BottomSheetValue.Expanded - -/** - * @author Niels Masdorp (NielsMasdorp) - * - * Align fraction states into single value - * - * 1.0f - Expanded - * 0.0f - Collapsed - */ -val BottomSheetScaffoldState.currentFraction: Float - get() { - val fraction = bottomSheetState.progress.fraction - val targetValue = bottomSheetState.targetValue - val currentValue = bottomSheetState.currentValue - - return when { - currentValue == Collapsed && targetValue == Collapsed -> 0f - currentValue == Expanded && targetValue == Expanded -> 1f - currentValue == Collapsed && targetValue == Expanded -> fraction - else -> 1f - fraction - } - } diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/HomeScreen.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/HomeScreen.kt index 43d60f2..adafc04 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/HomeScreen.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/HomeScreen.kt @@ -2,7 +2,7 @@ package com.nielsmasdorp.nederadio.ui.home import android.view.View import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier @@ -34,8 +34,9 @@ fun HomeScreen( val pagerState = rememberPagerState() - Column(modifier = modifier.fillMaxSize()) { + Column(modifier = modifier) { TopBar( + modifier = Modifier.statusBarsPadding(), castButton = castButton, showCastButton = activeStream is ActiveStream.Filled, onAboutClicked = onAbout, diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/SheetCollapsed.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/SheetCollapsed.kt index 08167dc..715dc9a 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/SheetCollapsed.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/SheetCollapsed.kt @@ -34,8 +34,8 @@ fun SheetCollapsed( Row( modifier = modifier .fillMaxWidth() - .graphicsLayer(alpha = 1f - currentFraction) - .height(72.dp) + .graphicsLayer(alpha = currentFraction) + .height(80.dp) .background(background) .clickable( onClick = onSheetClick, diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/StreamViewSmall.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/StreamViewSmall.kt index b42a867..dbca221 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/StreamViewSmall.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/collapsed/StreamViewSmall.kt @@ -66,7 +66,7 @@ fun StreamScreenSmall( Row( modifier = modifier .fillMaxSize() - .padding(8.dp), + .padding(16.dp), verticalAlignment = Alignment.CenterVertically ) { Card( diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/SheetExpanded.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/SheetExpanded.kt index 49520be..b14d573 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/SheetExpanded.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/SheetExpanded.kt @@ -7,18 +7,21 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.graphicsLayer /** * @author Niels Masdorp (NielsMasdorp) */ @Composable fun SheetExpanded( + currentFraction: Float, modifier: Modifier = Modifier, content: @Composable BoxScope.() -> Unit ) { Box( modifier = modifier .fillMaxSize() + .graphicsLayer(alpha = 1f - currentFraction) .background(MaterialTheme.colorScheme.primaryContainer) ) { content() diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/StreamViewLarge.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/StreamViewLarge.kt index 06355ae..011782c 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/StreamViewLarge.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/home/bottomsheet/expanded/StreamViewLarge.kt @@ -15,7 +15,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.ImageBitmap -import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.imageResource @@ -43,7 +42,6 @@ fun StreamViewLarge( onCollapseClick: () -> Unit, onTimerClicked: () -> Unit, onStreamFavoriteStatusChanged: (String, Boolean) -> Unit, - currentFraction: Float, modifier: Modifier = Modifier ) { @@ -62,10 +60,7 @@ fun StreamViewLarge( if (activeStream is ActiveStream.Filled) { // should not be visible to user when stream is not filled val stream = activeStream.stream Spacer(modifier = Modifier.height(16.dp)) - IconButton( - modifier = Modifier.graphicsLayer(alpha = 0f + currentFraction), - onClick = { onCollapseClick() } - ) { + IconButton(onClick = { onCollapseClick() }) { Icon( painter = painterResource(id = R.drawable.chevron_down), contentDescription = stringResource(id = R.string.settings_content_description), @@ -138,25 +133,25 @@ fun StreamViewLarge( Spacer(modifier = Modifier.height(16.dp)) Box( modifier = Modifier - .fillMaxWidth() + .fillMaxWidth(), + contentAlignment = Alignment.Center ) { val playPauseColor = MaterialTheme.colorScheme.onPrimaryContainer.toArgb() Box( modifier = Modifier .size(72.dp) - .align(Alignment.Center) .clip(CircleShape) - .background(MaterialTheme.colorScheme.primaryContainer) - ) - AndroidView( - factory = { playerControls.getView() as PlayerControlView }, - modifier = Modifier - .fillMaxWidth() - .align(Alignment.Center), - update = { view -> - view.setColors(playPauseColor) - } - ) + .background(MaterialTheme.colorScheme.primaryContainer), + contentAlignment = Alignment.Center, + ) { + AndroidView( + factory = { playerControls.getView() as PlayerControlView }, + modifier = Modifier.fillMaxWidth(), + update = { view -> + view.setColors(playPauseColor) + } + ) + } } Spacer(modifier = Modifier.height(8.dp)) TextButton(onClick = { onTimerClicked() }) { diff --git a/app/src/main/java/com/nielsmasdorp/nederadio/ui/search/SearchScreen.kt b/app/src/main/java/com/nielsmasdorp/nederadio/ui/search/SearchScreen.kt index 17971f7..0907a05 100644 --- a/app/src/main/java/com/nielsmasdorp/nederadio/ui/search/SearchScreen.kt +++ b/app/src/main/java/com/nielsmasdorp/nederadio/ui/search/SearchScreen.kt @@ -42,6 +42,7 @@ fun SearchScreen( .fillMaxSize() ) { SearchBar( + modifier = Modifier.statusBarsPadding(), query = query, onSearchQueryChanged = viewModel::onSearchQueryChanged, onExitSearch = onExitSearch diff --git a/app/src/test/java/com/nielsmasdorp/nederadio/util/LiveDataUtils.kt b/app/src/test/java/com/nielsmasdorp/nederadio/util/LiveDataUtils.kt deleted file mode 100644 index 05cb1f2..0000000 --- a/app/src/test/java/com/nielsmasdorp/nederadio/util/LiveDataUtils.kt +++ /dev/null @@ -1,52 +0,0 @@ -package com.nielsmasdorp.nederadio.util - -import androidx.lifecycle.LiveData -import androidx.lifecycle.Observer -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit -import java.util.concurrent.TimeoutException - -/** - * Helper method for testing LiveData objects, from - * https://github.com/googlesamples/android-architecture-components. - * - * Get the value from a LiveData object. We're waiting for LiveData to emit, for 2 seconds. - * Once we got a notification via onChanged, we stop observing. - */ -@Throws(InterruptedException::class) -fun getValue(liveData: LiveData): T { - val data = arrayOfNulls(1) - val latch = CountDownLatch(1) - liveData.observeForever { o -> - data[0] = o - latch.countDown() - } - latch.await(2, TimeUnit.SECONDS) - @Suppress("UNCHECKED_CAST") - return data[0] as T -} - -fun LiveData.getOrAwaitValue( - time: Long = 2, - timeUnit: TimeUnit = TimeUnit.SECONDS, - afterObserve: () -> Unit = {} -): T { - var data: T? = null - val latch = CountDownLatch(1) - val observer = object : Observer { - override fun onChanged(o: T?) { - data = o - latch.countDown() - this@getOrAwaitValue.removeObserver(this) - } - } - this.observeForever(observer) - afterObserve.invoke() - // Don't wait indefinitely if the LiveData is not set. - if (!latch.await(time, timeUnit)) { - this.removeObserver(observer) - throw TimeoutException("LiveData value was never set.") - } - @Suppress("UNCHECKED_CAST") - return data as T -} diff --git a/build.gradle b/build.gradle index b32f44d..fce66e5 100644 --- a/build.gradle +++ b/build.gradle @@ -21,8 +21,8 @@ buildscript { ext.androidx_lifecycle_version = '2.6.1' ext.androidx_navigation_version = '2.6.0' ext.androidx_media3_version = '1.1.0-rc01' - ext.androidx_compose_version = '1.3.1' - ext.androidx_compose_material3_version = '1.0.1' + ext.androidx_compose_version = '1.4.1' + ext.androidx_compose_material3_version = '1.1.1' //koin ext.koin_version = '3.3.3'