|
| 1 | +package com.smarttoolfactory.tutorial1_1basics.chapter4_state |
| 2 | + |
| 3 | +import android.widget.Toast |
| 4 | +import androidx.compose.foundation.background |
| 5 | +import androidx.compose.foundation.border |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 9 | +import androidx.compose.foundation.layout.padding |
| 10 | +import androidx.compose.material.Button |
| 11 | +import androidx.compose.material.Text |
| 12 | +import androidx.compose.runtime.Composable |
| 13 | +import androidx.compose.runtime.getValue |
| 14 | +import androidx.compose.runtime.mutableStateOf |
| 15 | +import androidx.compose.runtime.referentialEqualityPolicy |
| 16 | +import androidx.compose.runtime.remember |
| 17 | +import androidx.compose.runtime.setValue |
| 18 | +import androidx.compose.runtime.structuralEqualityPolicy |
| 19 | +import androidx.compose.ui.Modifier |
| 20 | +import androidx.compose.ui.graphics.Color |
| 21 | +import androidx.compose.ui.platform.LocalContext |
| 22 | +import androidx.compose.ui.tooling.preview.Preview |
| 23 | +import androidx.compose.ui.unit.dp |
| 24 | +import androidx.compose.ui.unit.sp |
| 25 | +import com.smarttoolfactory.tutorial1_1basics.ui.Blue400 |
| 26 | +import com.smarttoolfactory.tutorial1_1basics.ui.Orange400 |
| 27 | +import com.smarttoolfactory.tutorial1_1basics.ui.components.StyleableTutorialText |
| 28 | +import com.smarttoolfactory.tutorial1_1basics.ui.components.getRandomColor |
| 29 | + |
| 30 | +@Preview |
| 31 | +@Composable |
| 32 | +fun Tutorial4_1Screen2() { |
| 33 | + TutorialContent() |
| 34 | +} |
| 35 | + |
| 36 | +@Composable |
| 37 | +private fun TutorialContent() { |
| 38 | + Column( |
| 39 | + modifier = Modifier |
| 40 | + .fillMaxSize() |
| 41 | + .padding(8.dp) |
| 42 | + ) { |
| 43 | + |
| 44 | + |
| 45 | + StyleableTutorialText( |
| 46 | + text = "SnapshotMutationPolicy controls how changes are handled in mutable " + |
| 47 | + "snapshots. **structuralEqualityPolicy**(default) policy check " + |
| 48 | + "if equals function of a class returns true.\n" + |
| 49 | + "**copy function** of **data class** with same message returns equals so " + |
| 50 | + "no recomposition is triggered. In first example recomposition is " + |
| 51 | + "not trigger when we copy a new object since data class checks primary " + |
| 52 | + "constructor values in **equals** functions", |
| 53 | + bullets = false |
| 54 | + ) |
| 55 | + |
| 56 | + StructuralEqualitySample() |
| 57 | + StyleableTutorialText( |
| 58 | + text = |
| 59 | + "**referentialEqualityPolicy** policy checks if both instances are the same" + |
| 60 | + " to trigger recomposition.\n" + |
| 61 | + "Since new instance is created with copy each time button is clicked " + |
| 62 | + "new one time event is fired. Border of Text changes on each recomposition " + |
| 63 | + "with a random color to show recomposition visually.", |
| 64 | + bullets = false |
| 65 | + ) |
| 66 | + ReferentialEqualitySample() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +@Composable |
| 71 | +private fun StructuralEqualitySample() { |
| 72 | + |
| 73 | + val context = LocalContext.current |
| 74 | + |
| 75 | + var oneTimeEventData by remember { |
| 76 | + mutableStateOf( |
| 77 | + value = OneTimeEventData(message = "structuralEqualityPolicy message"), |
| 78 | + // 🔥 For recomposition to be triggered we need to assign an object with |
| 79 | + // different message since data class checks primary constructor values |
| 80 | + // for equals function |
| 81 | + policy = structuralEqualityPolicy() |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + // This is for showing toast message only on each recomposition |
| 86 | + Toast.makeText(context, oneTimeEventData.message, Toast.LENGTH_SHORT).show() |
| 87 | + |
| 88 | + Column( |
| 89 | + modifier = Modifier |
| 90 | + .background(Orange400) |
| 91 | + .fillMaxWidth() |
| 92 | + .padding(4.dp) |
| 93 | + ) { |
| 94 | + |
| 95 | + Button( |
| 96 | + modifier = Modifier |
| 97 | + .fillMaxWidth() |
| 98 | + .padding(vertical = 4.dp), |
| 99 | + onClick = { |
| 100 | + oneTimeEventData = |
| 101 | + oneTimeEventData.copy(message = "structuralEqualityPolicy message") |
| 102 | + |
| 103 | + } |
| 104 | + ) { |
| 105 | + Text(text = "Click to create Event") |
| 106 | + } |
| 107 | + |
| 108 | + Text( |
| 109 | + modifier = Modifier |
| 110 | + .border(2.dp, getRandomColor()) |
| 111 | + .fillMaxWidth() |
| 112 | + .padding(10.dp), |
| 113 | + text = oneTimeEventData.message, |
| 114 | + color = Color.White, |
| 115 | + fontSize = 16.sp |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +@Composable |
| 122 | +private fun ReferentialEqualitySample() { |
| 123 | + |
| 124 | + val context = LocalContext.current |
| 125 | + |
| 126 | + |
| 127 | + var oneTimeEventData by remember { |
| 128 | + mutableStateOf( |
| 129 | + value = OneTimeEventData(message = "referentialEqualityPolicy message"), |
| 130 | + policy = referentialEqualityPolicy() |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + Toast.makeText(context, oneTimeEventData.message, Toast.LENGTH_SHORT).show() |
| 135 | + |
| 136 | + Column( |
| 137 | + modifier = Modifier |
| 138 | + .background(Blue400) |
| 139 | + .fillMaxWidth() |
| 140 | + .padding(4.dp) |
| 141 | + ) { |
| 142 | + |
| 143 | + Button( |
| 144 | + modifier = Modifier |
| 145 | + .fillMaxWidth() |
| 146 | + .padding(vertical = 4.dp), |
| 147 | + onClick = { |
| 148 | + oneTimeEventData = |
| 149 | + oneTimeEventData.copy(message = "referentialEqualityPolicy message") |
| 150 | + |
| 151 | + } |
| 152 | + ) { |
| 153 | + Text(text = "Click to create Event") |
| 154 | + } |
| 155 | + |
| 156 | + Text( |
| 157 | + modifier = Modifier |
| 158 | + .border(2.dp, getRandomColor()) |
| 159 | + .fillMaxWidth() |
| 160 | + .padding(10.dp), |
| 161 | + text = oneTimeEventData.message, |
| 162 | + color = Color.White, |
| 163 | + fontSize = 16.sp |
| 164 | + ) |
| 165 | + |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +private data class OneTimeEventData(val message: String) |
0 commit comments