-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThenWithComposeModifier.kt
110 lines (103 loc) · 4.38 KB
/
ThenWithComposeModifier.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.arjunjadeja.learnings
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
class ThenWithComposeModifier {
// ✅ Example 1: Conditional Modifier Application Using `then`
@Composable
fun LoadSomeLayout(platform: Platform, content: @Composable () -> Unit) {
Box(
modifier = Modifier
.fillMaxWidth()
.then(
when (platform) {
Platform.MOBILE -> Modifier.aspectRatio(9f / 16f)
Platform.TABLET -> Modifier.aspectRatio(16 / 10f).padding(16.dp)
Platform.DESKTOP -> Modifier.aspectRatio(4 / 3f).padding(32.dp)
}
),
contentAlignment = Alignment.Center
) {
content()
}
}
// ✅ Example 2: Dynamic Modifier Addition with `then`
@Composable
fun DynamicModifierExample(isSelected: Boolean, content: @Composable () -> Unit) {
Box(
modifier = Modifier
.fillMaxWidth()
.then(
if (isSelected) {
Modifier.padding(16.dp).background(Color.Green)
} else {
Modifier.padding(8.dp).background(Color.Gray)
}
),
contentAlignment = Alignment.Center
) {
content()
}
}
// ❌ Example 3: Unnecessary Chaining with `then`
@Composable
fun UnnecessaryChaining(content: @Composable () -> Unit) {
// Avoid using `then` when modifiers can be chained directly
Box(
modifier = Modifier
.fillMaxWidth()
.then(Modifier.padding(8.dp)) // ❌ Unnecessary usage of `then`
.then(Modifier.background(Color.Blue)) // ❌ Another redundant `then`
.then(Modifier.padding(4.dp)), // ❌ Chaining directly would be simpler and clearer
contentAlignment = Alignment.Center
) {
content()
}
}
enum class Platform {
MOBILE,
TABLET,
DESKTOP
}
}
/**
* Notes on Using `then` in Jetpack Compose Modifiers:
*
* 1. What is `then`?
* - `then` is an extension function on `Modifier` that allows chaining another `Modifier` to an existing one.
* - It helps combine multiple modifiers, which can be especially useful when applying conditional modifiers.
* - Each `then` call returns a new `Modifier` representing the combined chain of modifiers.
*
* 2. Benefits of Using `then`:
* - Enhances readability: By separating base modifiers from conditional ones, `then` makes code clearer and more organized.
* - Supports conditional logic: Easily append modifiers based on state, device type, or other conditions.
* - Allows dynamic modifier composition, making UI code more flexible and responsive to changes.
*
* 3. How to Use `then`:
* - Combine base modifiers with conditionally applied modifiers:
* ```kotlin
* val baseModifier = Modifier.fillMaxWidth()
* val combinedModifier = baseModifier.then(
* if (isSelected) Modifier.padding(16.dp) else Modifier.padding(8.dp)
* )
* ```
* - Use in `Composable` functions to adjust layout properties dynamically.
* - In `LoadSomeLayout`, `then` appends the platform-specific modifier to the base `fillMaxWidth` modifier.
*
* 4. What Not to Do:
* - ❌ Avoid using `then` unnecessarily when the modifiers can be directly chained.
* - ❌ Don’t use `then` if it makes the modifier chain harder to read or maintain.
* - ❌ Be cautious of performance when chaining many modifiers; ensure each added modifier serves a clear purpose.
*
* 5. Recommendations:
* - Use `then` when you have conditional modifiers that need to be appended to a base modifier.
* - Keep the chain simple and readable; use `then` to keep conditional logic clean and separate.
* - Test modifiers visually to ensure the combined effect meets design expectations.
*/