forked from microsoft/fluentui-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV2SegmentedControlActivity.kt
405 lines (383 loc) · 17.8 KB
/
V2SegmentedControlActivity.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
package com.microsoft.fluentuidemo.demos
import android.os.Bundle
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.microsoft.fluentui.icons.AvatarIcons
import com.microsoft.fluentui.icons.avataricons.Icon
import com.microsoft.fluentui.icons.avataricons.icon.Anonymous
import com.microsoft.fluentui.icons.avataricons.icon.anonymous.Xxlarge
import com.microsoft.fluentui.theme.FluentTheme
import com.microsoft.fluentui.theme.token.FluentColor
import com.microsoft.fluentui.theme.token.FluentGlobalTokens
import com.microsoft.fluentui.theme.token.FluentStyle
import com.microsoft.fluentui.tokenized.controls.ToggleSwitch
import com.microsoft.fluentui.tokenized.listitem.ChevronOrientation
import com.microsoft.fluentui.tokenized.listitem.ListItem
import com.microsoft.fluentui.tokenized.segmentedcontrols.*
import com.microsoft.fluentuidemo.V2DemoActivity
// Tags used for testing
const val SEGMENTED_CONTROL_PILL_BUTTON = "Segmented Control Pill Button"
const val SEGMENTED_CONTROL_PILL_BAR = "Segmented Control Pill Bar"
const val SEGMENTED_CONTROL_TABS = "Segmented Control Tabs"
const val SEGMENTED_CONTROL_SWITCH = "Segmented Control Switch"
const val SEGMENTED_CONTROL_PILL_BUTTON_TOGGLE = "Segmented Control Pill Button Toggle"
const val SEGMENTED_CONTROL_PILL_BAR_TOGGLE = "Segmented Control Pill Bar Toggle"
const val SEGMENTED_CONTROL_TABS_TOGGLE = "Segmented Control Tabs Toggle"
const val SEGMENTED_CONTROL_SWITCH_TOGGLE = "Segmented Control Switch Toggle"
const val SEGMENTED_CONTROL_PILL_BUTTON_COMPONENT = "Segmented Control Pill Button Component"
const val SEGMENTED_CONTROL_PILL_BAR_COMPONENT = "Segmented Control Pill Bar Component"
const val SEGMENTED_CONTROL_TABS_COMPONENT = "Segmented Control Tabs Component"
const val SEGMENTED_CONTROL_SWITCH_COMPONENT = "Segmented Control Switch Component"
class V2SegmentedControlActivity : V2DemoActivity() {
init {
setupActivity(this)
}
override val paramsUrl = "https://github.com/microsoft/fluentui-android/wiki/Controls#params-30"
override val controlTokensUrl =
"https://github.com/microsoft/fluentui-android/wiki/Controls#control-tokens-28"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context = this
setActivityContent {
LazyColumn(
Modifier
.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(5.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
item {
var enabled by rememberSaveable { mutableStateOf(true) }
var selected by rememberSaveable { mutableStateOf(false) }
var unread by rememberSaveable { mutableStateOf(true) }
var selectedIcon by rememberSaveable { mutableStateOf(false) }
var selectedBrand by rememberSaveable { mutableStateOf(false) }
var selectedBrandIcon by rememberSaveable { mutableStateOf(false) }
template(
"Pill Button",
testTag = SEGMENTED_CONTROL_PILL_BUTTON,
enableSwitch = {
ToggleSwitch(
Modifier
.padding(vertical = 3.dp)
.testTag(SEGMENTED_CONTROL_PILL_BUTTON_TOGGLE),
onValueChange = { enabled = it },
checkedState = enabled
)
},
neutralContent = {
PillButton(
PillMetaData(
"Ally 1",
{
selected = !selected
unread = false
},
selected = selected,
enabled = enabled,
notificationDot = unread,
)
)
PillButton(
modifier = Modifier.testTag(SEGMENTED_CONTROL_PILL_BUTTON_COMPONENT),
pillMetaData = PillMetaData(
"Neutral",
{ selectedIcon = !selectedIcon },
icon = AvatarIcons.Icon.Anonymous.Xxlarge,
selected = selectedIcon,
enabled = enabled,
)
)
},
brandContent = {
PillButton(
PillMetaData(
"Brand",
{ selectedBrand = !selectedBrand },
selected = selectedBrand,
enabled = enabled
),
style = FluentStyle.Brand
)
PillButton(
PillMetaData(
"Brand",
{ selectedBrandIcon = !selectedBrandIcon },
icon = AvatarIcons.Icon.Anonymous.Xxlarge,
selected = selectedBrandIcon,
enabled = enabled,
notificationDot = true
),
style = FluentStyle.Brand,
)
}
)
}
item {
var enableBar by rememberSaveable { mutableStateOf(true) }
var selectedList = rememberSaveable(
saver = listSaver(
save = { stateList ->
if (stateList.isNotEmpty()) {
val first = stateList.first()
if (!canBeSaved(first)) {
throw IllegalStateException("${first::class} cannot be saved. By default only types which can be stored in the Bundle class can be saved.")
}
}
stateList.toList()
},
restore = { it.toMutableStateList() }
)) {
mutableStateListOf(
false,
false,
false,
false,
false,
false
)
}
var pillList: MutableList<PillMetaData> = mutableListOf()
for (idx in 0..5) {
val label = "Ally ${idx + 1}"
pillList.add(
PillMetaData(
text = label,
icon = if (idx % 2 == 1) AvatarIcons.Icon.Anonymous.Xxlarge else null,
onClick = {
Toast.makeText(
context,
"Button " + (idx + 1).toString(),
Toast.LENGTH_SHORT
).show()
selectedList[idx] = !selectedList[idx]
},
enabled = enableBar,
selected = selectedList[idx],
notificationDot = !selectedList[idx]
)
)
}
template(
"Pill Bar",
testTag = SEGMENTED_CONTROL_PILL_BAR,
enableSwitch = {
ToggleSwitch(
Modifier
.padding(vertical = 3.dp)
.testTag(SEGMENTED_CONTROL_PILL_BAR_TOGGLE),
onValueChange = { enableBar = it },
checkedState = enableBar
)
},
neutralContent = {
PillBar(
modifier = Modifier.testTag(SEGMENTED_CONTROL_PILL_BAR_COMPONENT),
metadataList = pillList
)
},
brandContent = {
PillBar(
pillList,
style = FluentStyle.Brand
)
}
)
}
item {
var enableTabs by rememberSaveable { mutableStateOf(true) }
var selectedTab by rememberSaveable { mutableStateOf(0) }
var tabsList: MutableList<PillMetaData> = mutableListOf()
for (idx in 0..5) {
val label = "Neutral ${idx + 1}"
tabsList.add(
PillMetaData(
text = label,
icon = if (idx % 2 == 1) AvatarIcons.Icon.Anonymous.Xxlarge else null,
onClick = {
Toast.makeText(
context,
"Button " + (idx + 1).toString(),
Toast.LENGTH_SHORT
).show()
selectedTab = idx
},
enabled = enableTabs,
notificationDot = selectedTab != idx
)
)
}
template(
"Tabs",
testTag = SEGMENTED_CONTROL_TABS,
enableSwitch = {
ToggleSwitch(
Modifier
.padding(vertical = 3.dp)
.testTag(SEGMENTED_CONTROL_TABS_TOGGLE),
onValueChange = { enableTabs = it },
checkedState = enableTabs
)
},
neutralContent = {
Column(verticalArrangement = Arrangement.spacedBy(5.dp)) {
PillTabs(
modifier = Modifier.testTag(SEGMENTED_CONTROL_TABS_COMPONENT),
metadataList = tabsList.subList(0, 4),
selectedIndex = selectedTab,
scrollable = true
)
PillTabs(
tabsList.subList(0, 4),
style = FluentStyle.Brand,
selectedIndex = selectedTab,
scrollable = false
)
}
},
brandContent = {
Column(verticalArrangement = Arrangement.spacedBy(5.dp)) {
PillTabs(
tabsList,
selectedIndex = selectedTab,
scrollable = true
)
PillTabs(
tabsList,
style = FluentStyle.Brand,
selectedIndex = selectedTab,
scrollable = false
)
}
}
)
}
item {
var enableSwitch by rememberSaveable { mutableStateOf(true) }
var selectedSwitch by rememberSaveable { mutableStateOf(0) }
var switchList: MutableList<PillMetaData> = mutableListOf()
for (idx in 0..7) {
val label = "Neutral ${idx + 1}"
switchList.add(
PillMetaData(
text = label,
icon = if (idx % 2 == 1) AvatarIcons.Icon.Anonymous.Xxlarge else null,
onClick = {
Toast.makeText(
context,
"Button " + (idx + 1).toString(),
Toast.LENGTH_SHORT
).show()
selectedSwitch = idx
},
enabled = enableSwitch,
notificationDot = selectedSwitch != idx
)
)
}
template(
"Switch",
testTag = SEGMENTED_CONTROL_SWITCH,
enableSwitch = {
ToggleSwitch(
Modifier
.padding(vertical = 3.dp)
.testTag(SEGMENTED_CONTROL_SWITCH_TOGGLE),
onValueChange = { enableSwitch = it },
checkedState = enableSwitch
)
},
neutralContent = {
Column(
verticalArrangement = Arrangement.spacedBy(5.dp),
) {
PillSwitch(
switchList.subList(0, 2),
selectedIndex = selectedSwitch
)
PillSwitch(
modifier = Modifier.testTag(SEGMENTED_CONTROL_SWITCH_COMPONENT),
metadataList = switchList.subList(0, 2),
style = FluentStyle.Brand,
selectedIndex = selectedSwitch
)
}
},
brandContent = {
Column(
verticalArrangement = Arrangement.spacedBy(5.dp),
) {
PillSwitch(
switchList,
selectedIndex = selectedSwitch
)
PillSwitch(
switchList,
style = FluentStyle.Brand,
selectedIndex = selectedSwitch
)
}
}
)
}
}
}
}
}
@Composable
private fun template(
label: String,
enableSwitch: (@Composable () -> Unit),
neutralContent: (@Composable RowScope.() -> Unit),
brandContent: (@Composable RowScope.() -> Unit),
testTag: String = ""
) {
ListItem.SectionHeader(
title = label,
enableChevron = true,
enableContentOpenCloseTransition = true,
chevronOrientation = ChevronOrientation(90f, 0f),
trailingAccessoryContent = enableSwitch,
modifier = Modifier.testTag(testTag)
) {
Column(
modifier = Modifier.background(
FluentColor(
light = FluentGlobalTokens.neutralColor(FluentGlobalTokens.NeutralColorTokens.Grey98),
dark = FluentGlobalTokens.neutralColor(FluentGlobalTokens.NeutralColorTokens.Grey8)
).value(
FluentTheme.themeMode
)
)
) {
Row(
Modifier
.fillMaxWidth()
.padding(5.dp),
horizontalArrangement = Arrangement.spacedBy(
10.dp,
Alignment.CenterHorizontally
),
content = neutralContent
)
Row(
Modifier
.fillMaxWidth()
.padding(5.dp),
horizontalArrangement = Arrangement.spacedBy(
10.dp,
Alignment.CenterHorizontally
),
content = brandContent
)
}
}
}