-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathDemo.kt
More file actions
325 lines (310 loc) · 11.9 KB
/
Demo.kt
File metadata and controls
325 lines (310 loc) · 11.9 KB
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
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.maps.android.compose
import androidx.activity.ComponentActivity
import androidx.annotation.StringRes
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.google.maps.android.compose.markerexamples.AdvancedMarkersActivity
import com.google.maps.android.compose.markerexamples.MarkerClusteringActivity
import com.google.maps.android.compose.markerexamples.draggablemarkerscollectionwithpolygon.DraggableMarkersCollectionWithPolygonActivity
import com.google.maps.android.compose.markerexamples.markerdragevents.MarkerDragEventsActivity
import com.google.maps.android.compose.markerexamples.markerscollection.MarkersCollectionActivity
import com.google.maps.android.compose.markerexamples.syncingdraggablemarkerwithdatamodel.SyncingDraggableMarkerWithDataModelActivity
import com.google.maps.android.compose.markerexamples.updatingnodragmarkerwithdatamodel.UpdatingNoDragMarkerWithDataModelActivity
import kotlin.reflect.KClass
// This file defines the data structures and composable functions used to display the list of
// demo activities on the main screen of the app. The main goal is to present the demos in a
// clear, organized, and easy-to-navigate way.
/**
* A sealed class representing a group of related demo activities. Using a sealed class here
* allows us to define a closed set of categories, which is ideal for a static list of demos.
* This ensures that we can handle all possible categories in a type-safe way.
*
* @param title The title of the activity group.
* @param activities The list of activities belonging to this group.
*/
sealed class ActivityGroup(
@StringRes val title: Int,
val activities: List<Activity>
) {
object MapTypes : ActivityGroup(
R.string.map_types_title,
listOf(
Activity(
R.string.basic_map_activity,
R.string.basic_map_activity_description,
BasicMapActivity::class
),
Activity(
R.string.street_view_activity,
R.string.street_view_activity_description,
StreetViewActivity::class
),
)
)
object MapFeatures : ActivityGroup(
R.string.map_features_title,
listOf(
Activity(
R.string.location_tracking_activity,
R.string.location_tracking_activity_description,
LocationTrackingActivity::class
),
Activity(
R.string.scale_bar_activity,
R.string.scale_bar_activity_description,
ScaleBarActivity::class
),
Activity(
R.string.custom_controls_activity,
R.string.custom_controls_activity_description,
CustomControlsActivity::class
),
Activity(
R.string.accessibility_activity,
R.string.accessibility_activity_description,
AccessibilityActivity::class
),
Activity(
R.string.tile_overlay_activity,
R.string.tile_overlay_activity_description,
TileOverlayActivity::class
),
Activity(
R.string.ground_overlay_activity,
R.string.ground_overlay_activity_description,
GroundOverlayActivity::class
),
)
)
object Markers : ActivityGroup(
R.string.markers_title,
listOf(
Activity(
R.string.advanced_markers_activity,
R.string.advanced_markers_activity_description,
AdvancedMarkersActivity::class
),
Activity(
R.string.marker_clustering_activity,
R.string.marker_clustering_activity_description,
MarkerClusteringActivity::class
),
Activity(
R.string.marker_drag_events_activity,
R.string.marker_drag_events_activity_description,
MarkerDragEventsActivity::class
),
Activity(
R.string.markers_collection_activity,
R.string.markers_collection_activity_description,
MarkersCollectionActivity::class
),
Activity(
R.string.syncing_draggable_marker_with_data_model_activity,
R.string.syncing_draggable_marker_with_data_model_activity_description,
SyncingDraggableMarkerWithDataModelActivity::class
),
Activity(
R.string.updating_no_drag_marker_with_data_model_activity,
R.string.updating_no_drag_marker_with_data_model_activity_description,
UpdatingNoDragMarkerWithDataModelActivity::class
),
Activity(
R.string.draggable_markers_collection_with_polygon_activity,
R.string.draggable_markers_collection_with_polygon_activity_description,
DraggableMarkersCollectionWithPolygonActivity::class
),
)
)
object UIIntegration : ActivityGroup(
R.string.ui_integration_title,
listOf(
Activity(
R.string.map_in_column_activity,
R.string.map_in_column_activity_description,
MapInColumnActivity::class
),
Activity(
R.string.maps_in_lazy_column_activity,
R.string.maps_in_lazy_column_activity_description,
MapsInLazyColumnActivity::class
),
Activity(
R.string.fragment_demo_activity,
R.string.fragment_demo_activity_description,
FragmentDemoActivity::class
),
)
)
object Performance : ActivityGroup(
R.string.performance_title,
listOf(
Activity(
R.string.recomposition_activity,
R.string.recomposition_activity_description,
RecompositionActivity::class
),
)
)
}
/**
* A data class representing a single demo activity. This class serves as a model for each
* item in the demo list.
*
* @param title The title of the activity.
* @param description A short description of what the demo showcases.
* @param kClass The class of the activity to be launched.
*/
data class Activity(
@StringRes val title: Int,
@StringRes val description: Int,
val kClass: KClass<out ComponentActivity>
)
/**
* The single source of truth for all the demo activity groups. This list is used to populate
* the main screen.
*/
val allActivityGroups = listOf(
ActivityGroup.MapTypes,
ActivityGroup.MapFeatures,
ActivityGroup.Markers,
ActivityGroup.UIIntegration,
ActivityGroup.Performance,
)
/**
* A composable function that displays a collapsible list of demo activity groups. This is the
* main UI component for the main screen.
*
* The list is built using a `LazyColumn` for performance, ensuring that only the visible items
* are rendered. Each group is represented by a clickable `Card` that expands or collapses to
* reveal the activities within it. This approach keeps the UI clean and organized, especially
* with a large number of demos.
*
* @param onActivityClick A lambda function to be invoked when a demo activity is clicked. This
* allows the navigation logic to be decoupled from the UI.
*/
@Composable
fun DemoList(
onActivityClick: (KClass<out ComponentActivity>) -> Unit
) {
// Tracks the currently expanded group to ensure only one is open at a time,
// creating a clean, accordion-style user interface.
var expandedGroup by remember { mutableStateOf<ActivityGroup?>(null) }
LazyColumn {
items(allActivityGroups) { group ->
val isExpanded = expandedGroup == group
Column {
// The card representing the group header.
GroupHeaderItem(group, isExpanded) {
expandedGroup = (if (isExpanded) null else group)
}
// Animate the visibility of the activities within the group.
AnimatedVisibility(visible = isExpanded) {
Column(
modifier = Modifier.padding(start = 16.dp, end = 16.dp)
) {
// Create a card for each activity in the group.
group.activities.forEach { activity ->
DemoActivityItem(onActivityClick, activity)
}
}
}
}
}
}
}
@Composable
private fun DemoActivityItem(
onActivityClick: (KClass<out ComponentActivity>) -> Unit,
activity: Activity
) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp)
.clickable { onActivityClick(activity.kClass) }
) {
Column(modifier = Modifier.padding(16.dp)) {
Text(text = stringResource(activity.title), fontWeight = FontWeight.Bold)
Text(text = stringResource(activity.description))
}
}
}
@Composable
private fun GroupHeaderItem(
group: ActivityGroup,
isExpanded: Boolean,
onGroupClicked: () -> Unit = {}
) {
Card(
// Highlight the card when it's expanded.
colors = if (isExpanded) {
CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer)
} else {
CardDefaults.cardColors()
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
.clickable {
onGroupClicked()
}
) {
Row(
modifier = Modifier.padding(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = stringResource(group.title),
fontWeight = FontWeight.Bold,
modifier = Modifier.weight(1f)
)
// Show an up or down arrow to indicate the expanded/collapsed state.
Icon(
imageVector = if (isExpanded) {
Icons.Default.KeyboardArrowUp
} else {
Icons.Default.KeyboardArrowDown
},
contentDescription = null
)
}
}
}