-
Notifications
You must be signed in to change notification settings - Fork 98
/
CardNudge.kt
275 lines (257 loc) · 12.3 KB
/
CardNudge.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
package com.microsoft.fluentui.tokenized.notification
import androidx.compose.animation.core.TweenSpec
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.compose.FractionalThreshold
import com.microsoft.fluentui.compose.rememberSwipeableState
import com.microsoft.fluentui.compose.swipeable
import com.microsoft.fluentui.notification.R
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.FluentTheme.aliasTokens
import com.microsoft.fluentui.theme.token.*
import com.microsoft.fluentui.theme.token.controlTokens.CardNudgeInfo
import com.microsoft.fluentui.theme.token.controlTokens.CardNudgeTokens
import com.microsoft.fluentui.theme.token.controlTokens.PillButtonInfo
import com.microsoft.fluentui.theme.token.controlTokens.PillButtonTokens
import com.microsoft.fluentui.tokenized.segmentedcontrols.PillButton
import com.microsoft.fluentui.tokenized.segmentedcontrols.PillMetaData
import kotlin.math.roundToInt
// TAGS FOR TESTING
const val CARD_NUDGE = "Fluent Card Nudge"
const val CARD_NUDGE_ICON = "Fluent Card Nudge Icon"
const val CARD_NUDGE_ACCENT_ICON = "Fluent Card Nudge Accent Icon"
const val CARD_NUDGE_ACCENT_TEXT = "Fluent Card Nudge Accent Text"
const val CARD_NUDGE_SUBTITLE = "Fluent Card Nudge Subtitle"
const val CARD_NUDGE_ACTION_BUTTON = "Fluent Card Nudge Action Button"
const val CARD_NUDGE_DISMISS_BUTTON = "Fluent Card Nudge Dismiss Button"
class CardNudgeMetaData(
val message: String,
val dismissOnClick: (() -> Unit)? = null,
val icon: FluentIcon? = null,
val subTitle: String? = null,
val accentText: String? = null,
val accentIcon: FluentIcon? = null,
val actionMetaData: PillMetaData? = null,
val leftSwipeGesture: ((Float) -> Unit)? = null,
val rightSwipeGesture: ((Float) -> Unit)? = null
)
private enum class SwipeGesture {
NONE,
LEFT,
RIGHT
}
/**
* A card nudge is a short message that helps people discover what they can do in an app.
* It appears at the top of a screen, beneath the navigation bar, and pushes all other content below it.
* Card nudges are helpful for sending reminders and recommendations, but they don’t necessarily relates
* to someone’s current task.
*
* @param metadata [CardNudgeMetaData] storing the information for the Nudge to be displayed.
* @param modifier Optional Modifier to be applied to CardNudge.
* @param outlineMode Boolean for enabling outline on CardNudge. Default: [false]
* @param cardNudgeTokens Optional Tokens for customizing CardNudge.
*/
@Composable
fun CardNudge(
metadata: CardNudgeMetaData,
modifier: Modifier = Modifier,
outlineMode: Boolean = false,
cardNudgeTokens: CardNudgeTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = cardNudgeTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.CardNudgeControlType] as CardNudgeTokens
val cardNudgeInfo = CardNudgeInfo()
val animationSpec = TweenSpec<Float>(durationMillis = 300)
val state = rememberSwipeableState(initialValue = SwipeGesture.NONE, animationSpec) { true }
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
BoxWithConstraints {
val maxWidth = constraints.maxWidth.toFloat()
val shape = RoundedCornerShape(12.dp)
Row(
modifier
.fillMaxWidth()
.offset { IntOffset(state.offset.value.roundToInt(), 0) }
.background(
token.backgroundBrush(cardNudgeInfo),
shape
)
.then(
if (outlineMode)
Modifier.border(
token.borderSize(cardNudgeInfo),
token.borderStrokeColor(cardNudgeInfo),
shape
)
else
Modifier
)
.padding(horizontal = 16.dp, vertical = 12.dp)
.swipeable(
state,
anchors = mapOf(
-maxWidth to SwipeGesture.LEFT,
0F to SwipeGesture.NONE,
maxWidth to SwipeGesture.RIGHT
),
reverseDirection = isRtl,
thresholds = { _, _ -> FractionalThreshold(0.3F) },
orientation = Orientation.Horizontal,
)
.testTag(CARD_NUDGE),
verticalAlignment = Alignment.CenterVertically
) {
LaunchedEffect(state.offset.value) {
if (state.offset.value > 0.1F) {
metadata.rightSwipeGesture?.invoke(state.offset.value / maxWidth)
} else if (state.offset.value < -0.1F) {
metadata.leftSwipeGesture?.invoke(state.offset.value / maxWidth)
}
}
LaunchedEffect(state.currentValue) {
if (state.currentValue != SwipeGesture.NONE)
state.animateTo(SwipeGesture.NONE)
}
if (metadata.icon != null && metadata.icon.isIconAvailable()) {
Box(
modifier = Modifier
.size(token.iconBackgroundSize(cardNudgeInfo))
.background(
token.iconBackgroundBrush(cardNudgeInfo),
CircleShape
)
.then(
if (metadata.icon.onClick != null) {
Modifier.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(),
enabled = true,
role = Role.Image,
onClick = metadata.icon.onClick!!
)
} else Modifier
)
.testTag(CARD_NUDGE_ICON),
contentAlignment = Alignment.Center
) {
Icon(
metadata.icon,
modifier = Modifier
.size(token.iconSize(cardNudgeInfo)),
tint = metadata.icon.tint ?: token.iconColor(cardNudgeInfo)
)
}
}
Column(
modifier = Modifier
.weight(1F)
.padding(start = 16.dp)
) {
BasicText(
metadata.message,
style = token.titleTypography(cardNudgeInfo),
)
if (!metadata.subTitle.isNullOrBlank() || !metadata.accentText.isNullOrBlank() || metadata.accentIcon != null) {
Row(
horizontalArrangement = Arrangement.spacedBy(4.dp),
verticalAlignment = Alignment.CenterVertically
) {
if (metadata.accentIcon != null) {
Box(
modifier = Modifier
.size(12.dp)
.testTag(CARD_NUDGE_ACCENT_ICON),
contentAlignment = Alignment.Center
) {
Icon(
metadata.accentIcon,
tint = metadata.accentIcon.tint ?: token.accentColor(cardNudgeInfo)
)
}
}
if (!metadata.accentText.isNullOrBlank()) {
BasicText(
metadata.accentText,
style = token.accentTypography(cardNudgeInfo),
modifier = Modifier.testTag(CARD_NUDGE_ACCENT_TEXT)
)
}
if (!metadata.subTitle.isNullOrBlank()) {
BasicText(
metadata.subTitle,
style = token.subtitleTypography(cardNudgeInfo),
modifier = Modifier.testTag(CARD_NUDGE_SUBTITLE)
)
}
}
}
Spacer(modifier = Modifier.width(4.dp))
}
if (metadata.actionMetaData != null) {
metadata.actionMetaData.calloutSelectionState = false
PillButton(
metadata.actionMetaData,
modifier = Modifier.testTag(CARD_NUDGE_ACTION_BUTTON),
pillButtonTokens = object : PillButtonTokens() {
@Composable
override fun backgroundBrush(pillButtonInfo: PillButtonInfo): StateBrush {
return StateBrush(
rest = SolidColor(aliasTokens.brandBackgroundColor[FluentAliasTokens.BrandBackgroundColorTokens.BrandBackgroundTint].value()),
pressed = SolidColor(aliasTokens.brandBackgroundColor[FluentAliasTokens.BrandBackgroundColorTokens.BrandBackgroundTint].value()),
focused = SolidColor(aliasTokens.brandBackgroundColor[FluentAliasTokens.BrandBackgroundColorTokens.BrandBackgroundTint].value())
)
}
@Composable
override fun textColor(pillButtonInfo: PillButtonInfo): StateColor {
return StateColor(
rest = aliasTokens.brandForegroundColor[FluentAliasTokens.BrandForegroundColorTokens.BrandForegroundTint].value(),
pressed = aliasTokens.brandForegroundColor[FluentAliasTokens.BrandForegroundColorTokens.BrandForegroundTint].value(),
focused = aliasTokens.brandForegroundColor[FluentAliasTokens.BrandForegroundColorTokens.BrandForegroundTint].value()
)
}
@Composable
override fun typography(pillButtonInfo: PillButtonInfo): TextStyle {
return aliasTokens.typography[FluentAliasTokens.TypographyTokens.Body2Strong]
}
}
)
Spacer(modifier = Modifier.width(12.dp))
}
if (metadata.dismissOnClick != null) {
Icon(
Icons.Filled.Close,
LocalContext.current.resources.getString(R.string.fluentui_dismiss_button),
modifier = Modifier
.padding(10.dp)
.size(token.dismissIconSize(cardNudgeInfo))
.testTag(CARD_NUDGE_DISMISS_BUTTON),
tint = token.dismissIconColor(cardNudgeInfo),
onClick = metadata.dismissOnClick
)
}
}
}
}