parent
d5feca0ec2
commit
4f07a16a7c
@ -0,0 +1,212 @@
|
||||
/*
|
||||
* Copyright 2024 The Android Open Source Project
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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.samples.apps.nowinandroid.core.ui
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.ListItem
|
||||
import androidx.compose.material3.ListItemDefaults
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.semantics.selected
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.unit.dp
|
||||
import coil3.ImageLoader
|
||||
import coil3.compose.LocalPlatformContext
|
||||
import com.google.samples.apps.nowinandroid.core.designsystem.component.DynamicAsyncImage
|
||||
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaIconToggleButton
|
||||
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
|
||||
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
|
||||
import nowinandroid.core.ui.generated.resources.Res
|
||||
import nowinandroid.core.ui.generated.resources.core_ui_interests_card_follow_button_content_desc
|
||||
import nowinandroid.core.ui.generated.resources.core_ui_interests_card_unfollow_button_content_desc
|
||||
import org.jetbrains.compose.resources.stringResource
|
||||
import org.jetbrains.compose.ui.tooling.preview.Preview
|
||||
|
||||
@Composable
|
||||
fun InterestsItem(
|
||||
name: String,
|
||||
following: Boolean,
|
||||
topicImageUrl: String,
|
||||
onClick: () -> Unit,
|
||||
onFollowButtonClick: (Boolean) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
iconModifier: Modifier = Modifier,
|
||||
description: String = "",
|
||||
isSelected: Boolean = false,
|
||||
) {
|
||||
ListItem(
|
||||
leadingContent = {
|
||||
InterestsIcon(topicImageUrl, iconModifier.size(64.dp))
|
||||
},
|
||||
headlineContent = {
|
||||
Text(text = name)
|
||||
},
|
||||
supportingContent = {
|
||||
Text(text = description)
|
||||
},
|
||||
trailingContent = {
|
||||
NiaIconToggleButton(
|
||||
checked = following,
|
||||
onCheckedChange = onFollowButtonClick,
|
||||
icon = {
|
||||
Icon(
|
||||
imageVector = NiaIcons.Add,
|
||||
contentDescription = stringResource(
|
||||
Res.string.core_ui_interests_card_follow_button_content_desc,
|
||||
),
|
||||
)
|
||||
},
|
||||
checkedIcon = {
|
||||
Icon(
|
||||
imageVector = NiaIcons.Check,
|
||||
contentDescription = stringResource(
|
||||
Res.string.core_ui_interests_card_unfollow_button_content_desc,
|
||||
),
|
||||
)
|
||||
},
|
||||
)
|
||||
},
|
||||
colors = ListItemDefaults.colors(
|
||||
containerColor = if (isSelected) {
|
||||
MaterialTheme.colorScheme.surfaceVariant
|
||||
} else {
|
||||
Color.Transparent
|
||||
},
|
||||
),
|
||||
modifier = modifier
|
||||
.semantics(mergeDescendants = true) {
|
||||
selected = isSelected
|
||||
}
|
||||
.clickable(enabled = true, onClick = onClick),
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun InterestsIcon(topicImageUrl: String, modifier: Modifier = Modifier) {
|
||||
if (topicImageUrl.isEmpty()) {
|
||||
Icon(
|
||||
modifier = modifier
|
||||
.background(MaterialTheme.colorScheme.surface)
|
||||
.padding(4.dp),
|
||||
imageVector = NiaIcons.Person,
|
||||
// decorative image
|
||||
contentDescription = null,
|
||||
)
|
||||
} else {
|
||||
DynamicAsyncImage(
|
||||
imageUrl = topicImageUrl,
|
||||
contentDescription = null,
|
||||
modifier = modifier,
|
||||
imageLoader = ImageLoader(LocalPlatformContext.current),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun InterestsCardPreview() {
|
||||
NiaTheme {
|
||||
Surface {
|
||||
InterestsItem(
|
||||
name = "Compose",
|
||||
description = "Description",
|
||||
following = false,
|
||||
topicImageUrl = "",
|
||||
onClick = { },
|
||||
onFollowButtonClick = { },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun InterestsCardLongNamePreview() {
|
||||
NiaTheme {
|
||||
Surface {
|
||||
InterestsItem(
|
||||
name = "This is a very very very very long name",
|
||||
description = "Description",
|
||||
following = true,
|
||||
topicImageUrl = "",
|
||||
onClick = { },
|
||||
onFollowButtonClick = { },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun InterestsCardLongDescriptionPreview() {
|
||||
NiaTheme {
|
||||
Surface {
|
||||
InterestsItem(
|
||||
name = "Compose",
|
||||
description = "This is a very very very very very very very " +
|
||||
"very very very long description",
|
||||
following = false,
|
||||
topicImageUrl = "",
|
||||
onClick = { },
|
||||
onFollowButtonClick = { },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun InterestsCardWithEmptyDescriptionPreview() {
|
||||
NiaTheme {
|
||||
Surface {
|
||||
InterestsItem(
|
||||
name = "Compose",
|
||||
description = "",
|
||||
following = true,
|
||||
topicImageUrl = "",
|
||||
onClick = { },
|
||||
onFollowButtonClick = { },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Preview
|
||||
@Composable
|
||||
private fun InterestsCardSelectedPreview() {
|
||||
NiaTheme {
|
||||
Surface {
|
||||
InterestsItem(
|
||||
name = "Compose",
|
||||
description = "",
|
||||
following = true,
|
||||
topicImageUrl = "",
|
||||
onClick = { },
|
||||
onFollowButtonClick = { },
|
||||
isSelected = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright 2022 The Android Open Source Project
|
||||
|
||||
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.
|
||||
-->
|
||||
<manifest />
|
||||
Loading…
Reference in new issue