generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ExpandableFloatingActionButton.kt
180 lines (165 loc) · 6.27 KB
/
ExpandableFloatingActionButton.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
package com.bitwarden.authenticator.ui.platform.components.fab
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkHorizontally
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SmallFloatingActionButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
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.draw.rotate
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.bitwarden.authenticator.R
import com.bitwarden.authenticator.ui.platform.base.util.Text
import com.bitwarden.authenticator.ui.platform.components.model.IconResource
import com.bitwarden.authenticator.ui.platform.theme.Typography
@Composable
fun <T : ExpandableFabOption> ExpandableFloatingActionButton(
modifier: Modifier = Modifier,
label: Text?,
items: List<T>,
expandableFabState: MutableState<ExpandableFabState> = rememberExpandableFabState(),
expandableFabIcon: ExpandableFabIcon,
onStateChange: (expandableFabState: ExpandableFabState) -> Unit = { },
) {
val rotation by animateFloatAsState(
targetValue = if (expandableFabState.value == ExpandableFabState.Expanded) {
expandableFabIcon.iconRotation ?: 0f
} else {
0f
},
label = stringResource(R.string.add_item_rotation),
)
Column(
modifier = modifier.wrapContentSize(),
horizontalAlignment = Alignment.End,
) {
AnimatedVisibility(
visible = expandableFabState.value.isExpanded(),
enter = fadeIn() + expandVertically(),
exit = fadeOut() + shrinkVertically(),
) {
LazyColumn(
modifier = Modifier
.wrapContentSize()
.padding(bottom = 16.dp),
horizontalAlignment = Alignment.End,
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
items(items) { expandableFabOption ->
ExpandableFabOption(
onFabOptionClick = {
expandableFabState.value = expandableFabState.value.toggleValue()
onStateChange(expandableFabState.value)
expandableFabOption.onFabOptionClick()
},
expandableFabOption = expandableFabOption
)
}
}
}
ExtendedFloatingActionButton(
onClick = {
expandableFabState.value = expandableFabState.value.toggleValue()
onStateChange(expandableFabState.value)
},
containerColor = MaterialTheme.colorScheme.primaryContainer,
) {
if (label != null) {
AnimatedVisibility(
visible = expandableFabState.value.isExpanded(),
enter = fadeIn() + expandHorizontally(),
exit = fadeOut() + shrinkHorizontally(),
) {
Text(
modifier = Modifier.padding(end = 8.dp),
text = label(),
)
}
}
Icon(
modifier = Modifier
.rotate(rotation)
.semantics { expandableFabIcon.iconData.testTag },
painter = expandableFabIcon.iconData.iconPainter,
contentDescription = expandableFabIcon.iconData.contentDescription,
)
}
}
}
@Composable
private fun <T : ExpandableFabOption> ExpandableFabOption(
expandableFabOption: T,
onFabOptionClick: (option: T) -> Unit,
) {
SmallFloatingActionButton(
onClick = { onFabOptionClick(expandableFabOption) },
) {
Row(
modifier = Modifier
.wrapContentSize()
.padding(end = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
expandableFabOption.label?.let { label ->
Text(
modifier = Modifier
.clip(RoundedCornerShape(size = 8.dp))
.padding(all = 8.dp),
text = label(),
style = Typography.labelSmall,
)
}
Icon(
painter = expandableFabOption.iconData.iconPainter,
contentDescription = expandableFabOption.iconData.contentDescription,
)
}
}
}
@Composable
fun rememberExpandableFabState() =
remember { mutableStateOf<ExpandableFabState>(ExpandableFabState.Collapsed) }
abstract class ExpandableFabOption(
val label: Text?,
val iconData: IconResource,
val onFabOptionClick: () -> Unit,
)
data class ExpandableFabIcon(
val iconData: IconResource,
val iconRotation: Float?,
)
sealed class ExpandableFabState {
fun isExpanded() = this is Expanded
fun toggleValue() = if (isExpanded()) {
Collapsed
} else {
Expanded
}
data object Collapsed : ExpandableFabState()
data object Expanded : ExpandableFabState()
}