Skip to content

Instantly share code, notes, and snippets.

@Pouyaa91
Pouyaa91 / StateRestoreTest.kt
Created August 15, 2023 01:04
StateRestoreTest
@RunWith(AndroidJUnit4::class)
class StateRestoreTest {
@get: Rule
val rule = createComposeRule()
@Test
fun testStateRestoration() {
val restorationTester = StateRestorationTester(rule)
restorationTester.setContent { ClickCounter() }
@Pouyaa91
Pouyaa91 / ClickCounter.kt
Created August 15, 2023 01:05
ClickCounter
@Composable
fun ClickCounter(modifier: Modifier = Modifier) {
var saveableCount by rememberSaveable { mutableStateOf(0) }
var count by remember { mutableStateOf(0) }
Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(32.dp, Alignment.CenterVertically)
) {
@Serializable
data class NetworkCategory(
@SerialName(value = "idCategory") val id: String?,
@SerialName(value = "strCategory") val name: String?,
@SerialName(value = "strCategoryThumb") val imageUrl: String?,
@SerialName(value = "strCategoryDescription") val description: String?
)
{
"categories": [
{
"idCategory": "1",
"strCategory": "first category",
"strCategoryThumb": "first thumb",
"strCategoryDescription": "first description"
},
{
"idCategory": "2",
interface CategoriesApiService {
@GET("categories.php")
suspend fun getCategories(): NetworkCategoriesWrapper
}
class CategoriesApiServiceTest {
private lateinit var apiService: CategoriesApiService
private lateinit var mockWebServer: MockWebServer
private val json = Json { ignoreUnknownKeys = true }
@Before
fun setup() {
mockWebServer = MockWebServer().apply(MockWebServer::start)
@Serializable
data class NetworkCategoriesWrapper(
@SerialName(value = "categories") val categories: List<NetworkCategory>
)
@Composable
fun ShimmerEffectBrush(): ShaderBrush {
val infiniteTransition = rememberInfiniteTransition(label = "shimmer transition")
val offset by infiniteTransition.animateFloat(
initialValue = 0f,
targetValue = 1f,
animationSpec = infiniteRepeatable(
animation = tween(durationMillis = 1_000),
),
@Composable
private fun CategoryImageView(
modifier: Modifier = Modifier,
categoryName: String,
imageUrl: String
) {
val painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.data(imageUrl)
.crossfade(200)
@Pouyaa91
Pouyaa91 / ExpandableSectionTitle.kt
Created October 20, 2023 00:07
ExpandableSectionTitle
@Composable
fun ExpandableSectionTitle(modifier: Modifier = Modifier, isExpanded: Boolean, title: String) {
val icon = if (isExpanded) Icons.Rounded.KeyboardArrowUp else Icons.Rounded.KeyboardArrowDown
Row(modifier = modifier.padding(8.dp), verticalAlignment = Alignment.CenterVertically) {
Image(
modifier = Modifier.size(32.dp),
imageVector = icon,
colorFilter = ColorFilter.tint(color = MaterialTheme.colorScheme.onPrimaryContainer),