-
-
Save KatieBarnett/90fa4812ac7c40c3bbaaa22f68244729 to your computer and use it in GitHub Desktop.
LocalInspectionMode - Other examples
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
@Composable | |
fun ImageComponent() { | |
Column( | |
verticalArrangement = Arrangement.spacedBy(16.dp), | |
modifier = Modifier.padding(16.dp) | |
) { | |
val imageUrl = "https://img.goodfon.com/original/800x600/e/12/voda-more-aysberg-nebo.jpg" | |
if (LocalInspectionMode.current) { | |
// Show this image from the resources rather than loading an image from the internet | |
Image( | |
painter = painterResource(id = R.drawable.iceberg_preview), | |
contentDescription = null, | |
contentScale = ContentScale.FillWidth, | |
modifier = Modifier.fillMaxWidth() | |
) | |
} else { | |
// Show this image in the live version | |
AsyncImage( | |
model = imageUrl, | |
contentDescription = null, | |
contentScale = ContentScale.FillWidth, | |
modifier = Modifier.fillMaxWidth() | |
) | |
} | |
} | |
} | |
@Composable | |
fun AnalyticsComponent() { | |
if (!LocalInspectionMode.current) { | |
// Firebase is not initialised in Previews and will cause an error | |
Firebase.analytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, Bundle()) | |
} | |
Column(modifier = Modifier.padding(16.dp)) { | |
Text("This is a component that contains an analytics call that runs on display") | |
} | |
} | |
@Composable | |
fun ViewModelComponent() { | |
val viewModel: MainViewModel = hiltViewModel() | |
val list: List<String> = if (!LocalInspectionMode.current) { | |
viewModel.someViewModelFlow.collectAsState(initial = emptyList()).value | |
} else { | |
// For Previews, we want to show the flow in a different state | |
// In this case, it would be better to create a composable from the actual content | |
// (the Row below) and pass in a list | |
listOf("d", "e", "f") | |
} | |
Row(horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.padding(16.dp)) { | |
Text("From the view model flow: ") | |
list.forEach { | |
Text(it) | |
} | |
} | |
} |
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
@HiltViewModel | |
class MainViewModel @Inject constructor() : ViewModel() { | |
val someViewModelFlow = MutableStateFlow(listOf("a", "b", "c")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment