|
| 1 | +/* |
| 2 | + * Copyright 2022 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.wear.snippets.m3.list |
| 18 | + |
| 19 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 20 | +import androidx.compose.material.icons.Icons |
| 21 | +import androidx.compose.material.icons.filled.Build |
| 22 | +import androidx.compose.runtime.Composable |
| 23 | +import androidx.compose.ui.Modifier |
| 24 | +import androidx.compose.ui.platform.LocalConfiguration |
| 25 | +import androidx.compose.ui.text.style.TextOverflow |
| 26 | +import androidx.wear.compose.foundation.lazy.rememberTransformingLazyColumnState |
| 27 | +import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices |
| 28 | +import androidx.wear.compose.ui.tooling.preview.WearPreviewFontScales |
| 29 | +import com.google.android.horologist.annotations.ExperimentalHorologistApi |
| 30 | +import com.google.android.horologist.compose.layout.ColumnItemType |
| 31 | +import com.google.android.horologist.compose.layout.rememberResponsiveColumnPadding |
| 32 | +import com.google.android.horologist.compose.layout.rememberResponsiveColumnState |
| 33 | +import androidx.wear.compose.material3.ListHeader |
| 34 | +import androidx.wear.compose.material3.ScreenScaffold |
| 35 | +import androidx.wear.compose.foundation.lazy.TransformingLazyColumn |
| 36 | +import androidx.wear.compose.material3.Icon |
| 37 | +import com.google.android.horologist.compose.layout.ScalingLazyColumn |
| 38 | +import com.google.android.horologist.compose.layout.ScalingLazyColumnDefaults |
| 39 | +import com.google.android.horologist.compose.layout.ScalingLazyColumnState |
| 40 | +import com.google.android.horologist.compose.layout.ScreenScaffold |
| 41 | +import com.google.android.horologist.compose.material.Button |
| 42 | +import com.google.android.horologist.compose.material.ListHeaderDefaults.firstItemPadding |
| 43 | +import com.google.android.horologist.compose.material.ResponsiveListHeader |
| 44 | +import androidx.wear.compose.material3.Button |
| 45 | +import androidx.wear.compose.material3.SurfaceTransformation |
| 46 | +import androidx.wear.compose.material3.lazy.rememberTransformationSpec |
| 47 | +import androidx.wear.compose.material3.lazy.transformedHeight |
| 48 | +import androidx.wear.compose.material3.Text |
| 49 | + |
| 50 | +@Composable |
| 51 | +fun ComposeList() { |
| 52 | + // [START android_wear_list] |
| 53 | + val columnState = rememberTransformingLazyColumnState() |
| 54 | + val contentPadding = rememberResponsiveColumnPadding( |
| 55 | + first = ColumnItemType.ListHeader, |
| 56 | + last = ColumnItemType.Button, |
| 57 | + ) |
| 58 | + val transformationSpec = rememberTransformationSpec() |
| 59 | + ScreenScaffold(scrollState = columnState, |
| 60 | + contentPadding = contentPadding) { contentPadding -> |
| 61 | + TransformingLazyColumn( |
| 62 | + state = columnState, |
| 63 | + contentPadding = contentPadding |
| 64 | + ) { |
| 65 | + item { |
| 66 | + ListHeader(modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec), |
| 67 | + transformation = SurfaceTransformation(transformationSpec)){ |
| 68 | + Text(text = "Header") |
| 69 | + } |
| 70 | + } |
| 71 | + // ... other items |
| 72 | + item { |
| 73 | + Button(modifier = Modifier.fillMaxWidth().transformedHeight(this, transformationSpec), |
| 74 | + transformation = SurfaceTransformation(transformationSpec), |
| 75 | + onClick = { /* ... */ }, |
| 76 | + icon = { |
| 77 | + Icon( |
| 78 | + imageVector = Icons.Default.Build, |
| 79 | + contentDescription = "build", |
| 80 | + ) |
| 81 | + }, |
| 82 | + ){ |
| 83 | + Text( |
| 84 | + text = "Build", |
| 85 | + maxLines = 1, |
| 86 | + overflow = TextOverflow.Ellipsis, |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + // [END android_wear_list] |
| 94 | +} |
| 95 | + |
| 96 | +@OptIn(ExperimentalHorologistApi::class) |
| 97 | +@Composable |
| 98 | +fun SnapAndFlingComposeList() { |
| 99 | + // [START android_wear_snap] |
| 100 | + val columnState = rememberResponsiveColumnState( |
| 101 | + // ... |
| 102 | + // [START_EXCLUDE] |
| 103 | + contentPadding = ScalingLazyColumnDefaults.padding( |
| 104 | + first = ScalingLazyColumnDefaults.ItemType.Text, |
| 105 | + last = ScalingLazyColumnDefaults.ItemType.SingleButton |
| 106 | + ), |
| 107 | + // [END_EXCLUDE] |
| 108 | + rotaryMode = ScalingLazyColumnState.RotaryMode.Snap |
| 109 | + ) |
| 110 | + ScreenScaffold(scrollState = columnState) { |
| 111 | + ScalingLazyColumn( |
| 112 | + columnState = columnState |
| 113 | + ) { |
| 114 | + // ... |
| 115 | + // [START_EXCLUDE] |
| 116 | + item { |
| 117 | + ResponsiveListHeader(contentPadding = firstItemPadding()) { |
| 118 | + androidx.wear.compose.material.Text(text = "Header") |
| 119 | + } |
| 120 | + } |
| 121 | + // ... other items |
| 122 | + item { |
| 123 | + Button( |
| 124 | + imageVector = Icons.Default.Build, |
| 125 | + contentDescription = "Example Button", |
| 126 | + onClick = { } |
| 127 | + ) |
| 128 | + } |
| 129 | + // [END_EXCLUDE] |
| 130 | + } |
| 131 | + } |
| 132 | + // [END android_wear_snap] |
| 133 | +} |
| 134 | + |
| 135 | +// [START android_wear_list_breakpoint] |
| 136 | +const val LARGE_DISPLAY_BREAKPOINT = 225 |
| 137 | + |
| 138 | +@Composable |
| 139 | +fun isLargeDisplay() = |
| 140 | + LocalConfiguration.current.screenWidthDp >= LARGE_DISPLAY_BREAKPOINT |
| 141 | + |
| 142 | +// [START_EXCLUDE] |
| 143 | +@Composable |
| 144 | +fun breakpointDemo() { |
| 145 | + // [END_EXCLUDE] |
| 146 | +// ... use in your Composables: |
| 147 | + if (isLargeDisplay()) { |
| 148 | + // Show additional content. |
| 149 | + } else { |
| 150 | + // Show content only for smaller displays. |
| 151 | + } |
| 152 | + // [START_EXCLUDE] |
| 153 | +} |
| 154 | +// [END_EXCLUDE] |
| 155 | +// [END android_wear_list_breakpoint] |
| 156 | + |
| 157 | +// [START android_wear_list_preview] |
| 158 | +@WearPreviewDevices |
| 159 | +@WearPreviewFontScales |
| 160 | +@Composable |
| 161 | +fun ComposeListPreview() { |
| 162 | + ComposeList() |
| 163 | +} |
| 164 | +// [END android_wear_list_preview] |
| 165 | + |
| 166 | +@WearPreviewDevices |
| 167 | +@WearPreviewFontScales |
| 168 | +@Composable |
| 169 | +fun SnapAndFlingComposeListPreview() { |
| 170 | + SnapAndFlingComposeList() |
| 171 | +} |
0 commit comments