Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Last active April 26, 2024 06:41
Show Gist options
  • Save KatieBarnett/90fa4812ac7c40c3bbaaa22f68244729 to your computer and use it in GitHub Desktop.
Save KatieBarnett/90fa4812ac7c40c3bbaaa22f68244729 to your computer and use it in GitHub Desktop.
LocalInspectionMode - Other examples
@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)
}
}
}
@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