This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnackbarHost.kt
456 lines (418 loc) · 16.1 KB
/
SnackbarHost.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.material3
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.RecomposeScope
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.currentRecomposeScope
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.AccessibilityManager
import androidx.compose.ui.platform.LocalAccessibilityManager
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.dismiss
import androidx.compose.ui.semantics.liveRegion
import androidx.compose.ui.semantics.semantics
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.delay
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlin.coroutines.resume
/**
* State of the [SnackbarHost], which controls the queue and the current [Snackbar] being shown
* inside the [SnackbarHost].
*
* This state is usually [remember]ed and used to provide a [SnackbarHost] to a [Scaffold].
*/
@Stable
class SnackbarHostState {
/**
* Only one [Snackbar] can be shown at a time. Since a suspending Mutex is a fair queue, this
* manages our message queue and we don't have to maintain one.
*/
private val mutex = Mutex()
/**
* The current [SnackbarData] being shown by the [SnackbarHost], or `null` if none.
*/
var currentSnackbarData by mutableStateOf<SnackbarData?>(null)
private set
/**
* Shows or queues to be shown a [Snackbar] at the bottom of the [Scaffold] to which this state
* is attached and suspends until the snackbar has disappeared.
*
* [SnackbarHostState] guarantees to show at most one snackbar at a time. If this function is
* called while another snackbar is already visible, it will be suspended until this snackbar is
* shown and subsequently addressed. If the caller is cancelled, the snackbar will be removed
* from display and/or the queue to be displayed.
*
* All of this allows for granular control over the snackbar queue from within:
*
* @sample androidx.compose.material3.samples.ScaffoldWithCoroutinesSnackbar
*
* To change the Snackbar appearance, change it in 'snackbarHost' on the [Scaffold].
*
* @param message text to be shown in the Snackbar
* @param actionLabel optional action label to show as button in the Snackbar
* @param withDismissAction a boolean to show a dismiss action in the Snackbar. This is
* recommended to be set to true for better accessibility when a Snackbar is set with a
* [SnackbarDuration.Indefinite]
* @param duration duration to control how long snackbar will be shown in [SnackbarHost], either
* [SnackbarDuration.Short], [SnackbarDuration.Long] or [SnackbarDuration.Indefinite].
*
* @return [SnackbarResult.ActionPerformed] if option action has been clicked or
* [SnackbarResult.Dismissed] if snackbar has been dismissed via timeout or by the user
*/
@OptIn(ExperimentalMaterial3Api::class)
suspend fun showSnackbar(
message: String,
actionLabel: String? = null,
withDismissAction: Boolean = false,
duration: SnackbarDuration = SnackbarDuration.Short
): SnackbarResult =
showSnackbar(SnackbarVisualsImpl(message, actionLabel, withDismissAction, duration))
/**
* Shows or queues to be shown a [Snackbar] at the bottom of the [Scaffold] to which this state
* is attached and suspends until the snackbar has disappeared.
*
* [SnackbarHostState] guarantees to show at most one snackbar at a time. If this function is
* called while another snackbar is already visible, it will be suspended until this snackbar is
* shown and subsequently addressed. If the caller is cancelled, the snackbar will be removed
* from display and/or the queue to be displayed.
*
* All of this allows for granular control over the snackbar queue from within:
*
* @sample androidx.compose.material3.samples.ScaffoldWithCustomSnackbar
*
* @param visuals [SnackbarVisuals] that are used to create a Snackbar
*
* @return [SnackbarResult.ActionPerformed] if option action has been clicked or
* [SnackbarResult.Dismissed] if snackbar has been dismissed via timeout or by the user
*/
@ExperimentalMaterial3Api
suspend fun showSnackbar(visuals: SnackbarVisuals): SnackbarResult = mutex.withLock {
try {
return suspendCancellableCoroutine { continuation ->
currentSnackbarData = SnackbarDataImpl(visuals, continuation)
}
} finally {
currentSnackbarData = null
}
}
private class SnackbarVisualsImpl(
override val message: String,
override val actionLabel: String?,
override val withDismissAction: Boolean,
override val duration: SnackbarDuration
) : SnackbarVisuals {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as SnackbarVisualsImpl
if (message != other.message) return false
if (actionLabel != other.actionLabel) return false
if (withDismissAction != other.withDismissAction) return false
if (duration != other.duration) return false
return true
}
override fun hashCode(): Int {
var result = message.hashCode()
result = 31 * result + actionLabel.hashCode()
result = 31 * result + withDismissAction.hashCode()
result = 31 * result + duration.hashCode()
return result
}
}
private class SnackbarDataImpl(
override val visuals: SnackbarVisuals,
private val continuation: CancellableContinuation<SnackbarResult>
) : SnackbarData {
override fun performAction() {
if (continuation.isActive) continuation.resume(SnackbarResult.ActionPerformed)
}
override fun dismiss() {
if (continuation.isActive) continuation.resume(SnackbarResult.Dismissed)
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as SnackbarDataImpl
if (visuals != other.visuals) return false
if (continuation != other.continuation) return false
return true
}
override fun hashCode(): Int {
var result = visuals.hashCode()
result = 31 * result + continuation.hashCode()
return result
}
}
}
/**
* Host for [Snackbar]s to be used in [Scaffold] to properly show, hide and dismiss items based
* on Material specification and the [hostState].
*
* This component with default parameters comes build-in with [Scaffold], if you need to show a
* default [Snackbar], use [SnackbarHostState.showSnackbar].
*
* @sample androidx.compose.material3.samples.ScaffoldWithSimpleSnackbar
*
* If you want to customize appearance of the [Snackbar], you can pass your own version as a child
* of the [SnackbarHost] to the [Scaffold]:
*
* @sample androidx.compose.material3.samples.ScaffoldWithCustomSnackbar
*
* @param hostState state of this component to read and show [Snackbar]s accordingly
* @param modifier the [Modifier] to be applied to this component
* @param snackbar the instance of the [Snackbar] to be shown at the appropriate time with
* appearance based on the [SnackbarData] provided as a param
*/
@Composable
fun SnackbarHost(
hostState: SnackbarHostState,
modifier: Modifier = Modifier,
snackbar: @Composable (SnackbarData) -> Unit = { Snackbar(it) }
) {
val currentSnackbarData = hostState.currentSnackbarData
val accessibilityManager = LocalAccessibilityManager.current
LaunchedEffect(currentSnackbarData) {
if (currentSnackbarData != null) {
val duration = currentSnackbarData.visuals.duration.toMillis(
currentSnackbarData.visuals.actionLabel != null,
accessibilityManager
)
delay(duration)
currentSnackbarData.dismiss()
}
}
FadeInFadeOutWithScale(
current = hostState.currentSnackbarData,
modifier = modifier,
content = snackbar
)
}
/**
* Interface to represent the visuals of one particular [Snackbar] as a piece of the [SnackbarData].
*
* @property message text to be shown in the Snackbar
* @property actionLabel optional action label to show as button in the Snackbar
* @property withDismissAction a boolean to show a dismiss action in the Snackbar. This is
* recommended to be set to true better accessibility when a Snackbar is set with a
* [SnackbarDuration.Indefinite]
* @property duration duration of the Snackbar
*/
@Stable
interface SnackbarVisuals {
val message: String
val actionLabel: String?
val withDismissAction: Boolean
val duration: SnackbarDuration
}
/**
* Interface to represent the data of one particular [Snackbar] as a piece of the
* [SnackbarHostState].
*
* @property visuals Holds the visual representation for a particular [Snackbar].
*/
@Stable
interface SnackbarData {
val visuals: SnackbarVisuals
/**
* Function to be called when Snackbar action has been performed to notify the listeners.
*/
fun performAction()
/**
* Function to be called when Snackbar is dismissed either by timeout or by the user.
*/
fun dismiss()
}
/**
* Possible results of the [SnackbarHostState.showSnackbar] call
*/
enum class SnackbarResult {
/**
* [Snackbar] that is shown has been dismissed either by timeout of by user
*/
Dismissed,
/**
* Action on the [Snackbar] has been clicked before the time out passed
*/
ActionPerformed,
}
/**
* Possible durations of the [Snackbar] in [SnackbarHost]
*/
enum class SnackbarDuration {
/**
* Show the Snackbar for a short period of time
*/
Short,
/**
* Show the Snackbar for a long period of time
*/
Long,
/**
* Show the Snackbar indefinitely until explicitly dismissed or action is clicked
*/
Indefinite
}
// TODO: magic numbers adjustment
internal fun SnackbarDuration.toMillis(
hasAction: Boolean,
accessibilityManager: AccessibilityManager?
): Long {
val original = when (this) {
SnackbarDuration.Indefinite -> Long.MAX_VALUE
SnackbarDuration.Long -> 10000L
SnackbarDuration.Short -> 4000L
}
if (accessibilityManager == null) {
return original
}
return accessibilityManager.calculateRecommendedTimeoutMillis(
original,
containsIcons = true,
containsText = true,
containsControls = hasAction
)
}
// TODO: to be replaced with the public customizable implementation
// it's basically tweaked nullable version of Crossfade
@Composable
private fun FadeInFadeOutWithScale(
current: SnackbarData?,
modifier: Modifier = Modifier,
content: @Composable (SnackbarData) -> Unit
) {
val state = remember { FadeInFadeOutState<SnackbarData?>() }
if (current != state.current) {
state.current = current
val keys = state.items.map { it.key }.toMutableList()
if (!keys.contains(current)) {
keys.add(current)
}
state.items.clear()
keys.filterNotNull().mapTo(state.items) { key ->
FadeInFadeOutAnimationItem(key) { children ->
val isVisible = key == current
val duration = if (isVisible) SnackbarFadeInMillis else SnackbarFadeOutMillis
val delay = SnackbarFadeOutMillis + SnackbarInBetweenDelayMillis
val animationDelay = if (isVisible && keys.filterNotNull().size != 1) delay else 0
val opacity = animatedOpacity(
animation = tween(
easing = LinearEasing,
delayMillis = animationDelay,
durationMillis = duration
),
visible = isVisible,
onAnimationFinish = {
if (key != state.current) {
// leave only the current in the list
state.items.removeAll { it.key == key }
state.scope?.invalidate()
}
}
)
val scale = animatedScale(
animation = tween(
easing = FastOutSlowInEasing,
delayMillis = animationDelay,
durationMillis = duration
),
visible = isVisible
)
Box(
Modifier
.graphicsLayer(
scaleX = scale.value,
scaleY = scale.value,
alpha = opacity.value
)
.semantics {
liveRegion = LiveRegionMode.Polite
dismiss { key.dismiss(); true }
}
) {
children()
}
}
}
}
Box(modifier) {
state.scope = currentRecomposeScope
state.items.forEach { (item, opacity) ->
key(item) {
opacity {
content(item!!)
}
}
}
}
}
private class FadeInFadeOutState<T> {
// we use Any here as something which will not be equals to the real initial value
var current: Any? = Any()
var items = mutableListOf<FadeInFadeOutAnimationItem<T>>()
var scope: RecomposeScope? = null
}
private data class FadeInFadeOutAnimationItem<T>(
val key: T,
val transition: FadeInFadeOutTransition
)
private typealias FadeInFadeOutTransition = @Composable (content: @Composable () -> Unit) -> Unit
@Composable
private fun animatedOpacity(
animation: AnimationSpec<Float>,
visible: Boolean,
onAnimationFinish: () -> Unit = {}
): State<Float> {
val alpha = remember { Animatable(if (!visible) 1f else 0f) }
LaunchedEffect(visible) {
alpha.animateTo(
if (visible) 1f else 0f,
animationSpec = animation
)
onAnimationFinish()
}
return alpha.asState()
}
@Composable
private fun animatedScale(animation: AnimationSpec<Float>, visible: Boolean): State<Float> {
val scale = remember { Animatable(if (!visible) 1f else 0.8f) }
LaunchedEffect(visible) {
scale.animateTo(
if (visible) 1f else 0.8f,
animationSpec = animation
)
}
return scale.asState()
}
private const val SnackbarFadeInMillis = 150
private const val SnackbarFadeOutMillis = 75
private const val SnackbarInBetweenDelayMillis = 0