forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationCommon.kt
102 lines (88 loc) · 2.44 KB
/
NotificationCommon.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
package com.microsoft.fluentui.tokenized.notification
import androidx.compose.animation.core.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.AccessibilityManager
import androidx.compose.ui.platform.LocalAccessibilityManager
import kotlinx.coroutines.delay
interface NotificationMetadata {
fun clicked()
fun dismiss()
fun timedOut()
}
enum class NotificationResult {
TIMEOUT,
DISMISSED,
CLICKED
}
enum class NotificationDuration {
SHORT,
LONG,
INDEFINITE;
fun convertToMillis(
hasIcon: Boolean,
hasAction: Boolean,
accessibilityManager: AccessibilityManager?
): Long {
val actualDuration = when (this) {
INDEFINITE -> Long.MAX_VALUE
LONG -> 10000L
SHORT -> 4000L
}
if (accessibilityManager == null)
return actualDuration
return accessibilityManager.calculateRecommendedTimeoutMillis(
actualDuration,
containsIcons = true,
containsText = hasIcon,
containsControls = hasAction
)
}
}
@Composable
internal fun NotificationContainer(
notificationMetadata: NotificationMetadata,
hasIcon: Boolean,
hasAction: Boolean,
duration: NotificationDuration,
content: @Composable (
(
Animatable<Float, AnimationVector1D>,
Animatable<Float, AnimationVector1D>
) -> Unit
)
) {
val accessibilityManager = LocalAccessibilityManager.current
LaunchedEffect(notificationMetadata) {
delay(
duration.convertToMillis(
hasIcon = hasIcon,
hasAction = hasAction,
accessibilityManager = accessibilityManager
)
)
notificationMetadata.timedOut()
}
val alpha = remember { Animatable(0F) }
val scale = remember { Animatable(0.8F) }
LaunchedEffect(notificationMetadata) {
alpha.animateTo(
1F,
animationSpec = tween(
easing = LinearEasing,
durationMillis = 150,
)
)
}
LaunchedEffect(notificationMetadata) {
scale.animateTo(
1F,
animationSpec = tween(
easing = FastOutSlowInEasing,
durationMillis = 150,
)
)
}
content(alpha, scale)
}