-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support @PreviewParameter annotated preview params (#271)
* [WIP] Support @PreviewParameter annotated preview params * Minor cleanups and handle limit * More examples * Adjust param * unit tests and lint * Add some logging * Index * Race * Adjust * Add preview filter * Remove log
- Loading branch information
Showing
9 changed files
with
356 additions
and
72 deletions.
There are no files selected for viewing
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
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
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
100 changes: 100 additions & 0 deletions
100
snapshots/sample/app/src/main/kotlin/com/emergetools/snapshots/sample/ui/UserRow.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.emergetools.snapshots.sample.ui | ||
|
||
import android.content.res.Configuration | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.tooling.preview.PreviewParameter | ||
import androidx.compose.ui.tooling.preview.PreviewParameterProvider | ||
import androidx.compose.ui.unit.dp | ||
import com.emergetools.snapshots.sample.ui.theme.SnapshotsSampleTheme | ||
|
||
data class User(val name: String, val email: String) | ||
|
||
@Composable | ||
fun UserRow(user: User) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
// Profile picture | ||
Surface( | ||
modifier = Modifier | ||
.size(40.dp) | ||
.clip(CircleShape) | ||
.background(Color.Cyan), | ||
color = Color.Transparent | ||
) { | ||
// Center content vertically and horizontally | ||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Text( | ||
text = user.name.firstOrNull()?.uppercase() ?: "", | ||
style = MaterialTheme.typography.bodyLarge, | ||
textAlign = TextAlign.Center | ||
) | ||
} | ||
} | ||
|
||
Spacer(modifier = Modifier.width(16.dp)) | ||
|
||
// User details | ||
Column { | ||
Text(text = user.name, style = MaterialTheme.typography.bodyMedium) | ||
Text(text = user.email, style = MaterialTheme.typography.bodySmall, color = Color.Gray) | ||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) | ||
@Composable | ||
fun UserRowPreview( | ||
@PreviewParameter(UserRowPreviewProvider::class) user: User, | ||
) { | ||
SnapshotsSampleTheme { | ||
UserRow(user = user) | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) | ||
@Composable | ||
fun UserRowPreviewWithLimit( | ||
@PreviewParameter(UserRowPreviewProvider::class, limit = 2) user: User, | ||
) { | ||
SnapshotsSampleTheme { | ||
UserRow(user = user) | ||
} | ||
} | ||
|
||
class UserRowPreviewProvider : PreviewParameterProvider<User> { | ||
override val values: Sequence<User> = sequenceOf( | ||
User(name = "Ryan", email = "[email protected]"), | ||
User(name = "Trevor", email = "[email protected]"), | ||
User(name = "Josh", email = "[email protected]"), | ||
) | ||
} |
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
Oops, something went wrong.