Skip to content

Commit afffc04

Browse files
This PR contains Snippets that will be hosted in the New Visibility API DAC. (#664)
* This PR contains Snippets that will be hosted in the New Visibility API DAC.
1 parent 8a28727 commit afffc04

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.modifiers
18+
19+
import androidx.annotation.DrawableRes
20+
import androidx.compose.foundation.Image
21+
import androidx.compose.foundation.background
22+
import androidx.compose.foundation.border
23+
import androidx.compose.foundation.layout.Box
24+
import androidx.compose.foundation.layout.fillMaxSize
25+
import androidx.compose.foundation.layout.fillMaxWidth
26+
import androidx.compose.foundation.layout.height
27+
import androidx.compose.foundation.layout.padding
28+
import androidx.compose.foundation.lazy.LazyColumn
29+
import androidx.compose.material3.Card
30+
import androidx.compose.material3.Text
31+
import androidx.compose.runtime.Composable
32+
import androidx.compose.runtime.getValue
33+
import androidx.compose.runtime.mutableStateOf
34+
import androidx.compose.runtime.remember
35+
import androidx.compose.runtime.setValue
36+
import androidx.compose.ui.Alignment
37+
import androidx.compose.ui.Modifier
38+
import androidx.compose.ui.graphics.Color
39+
import androidx.compose.ui.layout.onFirstVisible
40+
import androidx.compose.ui.layout.onVisibilityChanged
41+
import androidx.compose.ui.res.painterResource
42+
import androidx.compose.ui.unit.dp
43+
44+
private object OnVisibilityChangedSample {
45+
46+
@Composable
47+
fun OnVisibilityChangedExample(modifier: Modifier = Modifier) {
48+
// [START android_compose_modifiers_onVisibilityChanged]
49+
Text(
50+
text = "Some text",
51+
modifier = Modifier
52+
.onVisibilityChanged { visible ->
53+
if (visible) {
54+
// Do something if visible
55+
} else {
56+
// Do something if not visible
57+
}
58+
}
59+
.padding(vertical = 8.dp)
60+
)
61+
// [END android_compose_modifiers_onVisibilityChanged]
62+
}
63+
}
64+
65+
private object OnVisibilityChangedMinFractionVisible {
66+
67+
@Composable
68+
fun OnVisibilityChangedModifierMinFractionExample(modifier: Modifier = Modifier) {
69+
// [START android_compose_modifiers_onVisibilityChangedMinFraction]
70+
LazyColumn(
71+
modifier = modifier.fillMaxSize()
72+
) {
73+
item {
74+
Box(
75+
modifier = Modifier
76+
// [START_EXCLUDE]
77+
.fillParentMaxWidth()
78+
.border(0.5.dp, Color.Gray)
79+
// [END_EXCLUDE]
80+
// Here the visible callback gets triggered when 20% of the composable is visible
81+
.onVisibilityChanged(
82+
minFractionVisible = 0.2f,
83+
) { visible ->
84+
if (visible) {
85+
// Call specific logic here
86+
// viewModel.fetchDataFromNetwork()
87+
}
88+
}
89+
.padding(vertical = 16.dp)
90+
) {
91+
Text(
92+
text = "Sample Text",
93+
modifier = Modifier.padding(horizontal = 16.dp)
94+
)
95+
}
96+
}
97+
}
98+
// [END android_compose_modifiers_onVisibilityChangedMinFraction]
99+
}
100+
}
101+
102+
private object onVisibilityChangedMinDuration {
103+
104+
val MutedPlum = Color(0xFF7B4B6B)
105+
val PalePink = Color(0xFFF3E9EB)
106+
107+
@Composable
108+
fun OnVisibilityChangedMinDurationExample(
109+
@DrawableRes imageRes: Int,
110+
modifier: Modifier = Modifier,
111+
) {
112+
// [START android_compose_modifiers_onVisibilityChangedMinDuration]
113+
var background by remember { mutableStateOf(PalePink) }
114+
Card(
115+
modifier = modifier
116+
// [START_EXCLUDE]
117+
.height(300.dp)
118+
.fillMaxWidth()
119+
// [END_EXCLUDE]
120+
.onVisibilityChanged(minDurationMs = 3000) {
121+
if (it) {
122+
background = MutedPlum
123+
}
124+
}
125+
) {
126+
127+
Box(
128+
modifier = Modifier
129+
// [START_EXCLUDE]
130+
.fillMaxSize()
131+
// [END_EXCLUDE]
132+
.background(background),
133+
contentAlignment = Alignment.Center,
134+
) {
135+
// [START_EXCLUDE]
136+
Image(
137+
painter = painterResource(id = imageRes),
138+
contentDescription = "Androidify Bot",
139+
)
140+
// [END_EXCLUDE]
141+
}
142+
}
143+
// [END android_compose_modifiers_onVisibilityChangedMinDuration]
144+
}
145+
}
146+
147+
148+
private object OnFirstVisibleSample {
149+
150+
@Composable
151+
fun OnFirstVisibleExample(id: Int, modifier: Modifier = Modifier) {
152+
Card(
153+
modifier = modifier
154+
.fillMaxWidth()
155+
.padding(8.dp)
156+
) {
157+
// [START android_compose_modifiers_onFirstVisible]
158+
Text(
159+
modifier = Modifier
160+
.fillMaxSize()
161+
.onFirstVisible {
162+
println("OnFirstVisible : ProductCard: $id is visible")
163+
}
164+
.padding(8.dp),
165+
text = "Product $id"
166+
)
167+
// [END android_compose_modifiers_onFirstVisible]
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)