forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileCard.kt
194 lines (186 loc) · 8.58 KB
/
FileCard.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
package com.microsoft.fluentui.tokenized.controls
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.ControlTokens
import com.microsoft.fluentui.theme.token.FluentIcon
import com.microsoft.fluentui.theme.token.Icon
import com.microsoft.fluentui.theme.token.controlTokens.BasicCardInfo
import com.microsoft.fluentui.theme.token.controlTokens.BasicCardTokens
import com.microsoft.fluentui.theme.token.controlTokens.FileCardInfo
import com.microsoft.fluentui.theme.token.controlTokens.FileCardTokens
/**
* Cards are flexible containers that group related content and actions together. They reveal more information upon interaction.
* A File card is generally used to refer a document. The card is made of a preview image on the top and some text below.
*
* @param text Primary text of the Card
* @param subText Secondary text of the card, usually a small description
* @param modifier Modifier for the card
* @param interactionSource Optional interaction source
* @param onClick onClick for the card
* @param previewImageVector Optional previewImage for the card
* @param previewImageDrawable Optional previewImage for the card
* @param textIcon Optional textIcon.
* @param actionOverflowIcon Optional actionOverflowIcon. If not provided there will be no button on top of the preview Image.
* @param fileCardTokens Optional tokens for customizing the card
*/
@Composable
fun FileCard(
text: String,
subText: String,
modifier: Modifier = Modifier,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
onClick: (() -> Unit)? = null,
previewImageVector: ImageVector? = null,
@DrawableRes previewImageDrawable: Int? = null,
textIcon: ImageVector,
actionOverflowIcon: FluentIcon? = null,
fileCardTokens: FileCardTokens? = null
) {
val themeID =
FluentTheme.themeID //Adding This only for recomposition in case of Token Updates. Unused otherwise.
val token = fileCardTokens
?: FluentTheme.controlTokens.tokens[ControlTokens.ControlType.FileCardControlType] as FileCardTokens
val isPreviewAvailable = !(previewImageDrawable == null && previewImageVector == null)
val fileCardInfo = FileCardInfo(isPreviewAvailable)
val textColor = token.textColor(fileCardInfo = fileCardInfo)
val subTextColor = token.subTextColor(fileCardInfo = fileCardInfo)
val iconColor = token.iconColor(fileCardInfo = fileCardInfo)
val actionOverFlowBackgroundColor =
token.actionOverFlowBackgroundColor(fileCardInfo = fileCardInfo)
val actionOverFlowIconColor = token.actionOverFlowIconColor(fileCardInfo = fileCardInfo)
val iconSize = token.iconSize(fileCardInfo = fileCardInfo)
val textTypography = token.textTypography(fileCardInfo = fileCardInfo)
val subTextTypography = token.subTextTypography(fileCardInfo = fileCardInfo)
val actionOverflowCornerRadius = token.actionOverflowCornerRadius(fileCardInfo = fileCardInfo)
val actionOverflowIconSize = token.actionOverflowIconSize(fileCardInfo = fileCardInfo)
val iconTextSpacing = token.iconTextSpacing(fileCardInfo = fileCardInfo)
val textSubTextSpacing = token.textSubTextSpacing(fileCardInfo = fileCardInfo)
val actionOverflowPadding = token.actionOverflowPadding(fileCardInfo = fileCardInfo)
val textContainerPadding = token.textContainerPadding(fileCardInfo = fileCardInfo)
class CustomBasicCardTokens : BasicCardTokens() {
@Composable
override fun backgroundBrush(basicCardInfo: BasicCardInfo): Brush {
return token.backgroundBrush(fileCardInfo = fileCardInfo)
}
@Composable
override fun cornerRadius(basicCardInfo: BasicCardInfo): Dp {
return token.cornerRadius(fileCardInfo = fileCardInfo)
}
@Composable
override fun elevation(basicCardInfo: BasicCardInfo): Dp {
return token.elevation(fileCardInfo = fileCardInfo)
}
@Composable
override fun borderColor(basicCardInfo: BasicCardInfo): Color {
return token.borderColor(fileCardInfo = fileCardInfo)
}
@Composable
override fun borderStrokeWidth(basicCardInfo: BasicCardInfo): Dp {
return token.borderStrokeWidth(fileCardInfo = fileCardInfo)
}
}
BasicCard(
modifier = modifier,
basicCardTokens = CustomBasicCardTokens() as BasicCardTokens
) {
Box(contentAlignment = Alignment.Center) {
Column(
modifier = Modifier
.width(IntrinsicSize.Min)
.testTag("Card")
.then(
if (onClick != null) Modifier.clickable(
interactionSource = interactionSource,
indication = rememberRipple(),
enabled = true,
onClick = onClick,
role = Role.Button
) else Modifier
),
horizontalAlignment = Alignment.CenterHorizontally,
) {
if (previewImageDrawable != null) {
Image(
painterResource(id = previewImageDrawable), contentDescription = null
)
} else if (previewImageVector != null) {
Image(
imageVector = previewImageVector,
contentDescription = null
)
}
Row(
modifier = Modifier.padding(
textContainerPadding
),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
modifier = Modifier.size(iconSize),
icon = FluentIcon(textIcon),
tint = iconColor
)
Spacer(modifier = Modifier.width(iconTextSpacing))
Column {
BasicText(
text = text,
style = textTypography.merge(TextStyle(color = textColor))
)
Spacer(
modifier = Modifier
.fillMaxWidth()
.height(textSubTextSpacing)
)
BasicText(
text = subText,
style = subTextTypography.merge(TextStyle(color = subTextColor))
)
}
}
}
if (isPreviewAvailable && actionOverflowIcon != null) {
Box(
modifier = Modifier
.align(Alignment.TopEnd)
.padding(actionOverflowPadding)
.clip(RoundedCornerShape(actionOverflowCornerRadius))
.background(actionOverFlowBackgroundColor)
.clickable(
interactionSource = MutableInteractionSource(),
indication = rememberRipple(),
enabled = true,
onClick = actionOverflowIcon.onClick ?: {},
role = Role.Button
)
) {
Icon(
modifier = Modifier.size(actionOverflowIconSize),
icon = actionOverflowIcon,
tint = actionOverFlowIconColor,
)
}
}
}
}
}